Tikejhya: Ashish Nepal

Knowledgebase

Page 2 of 41

Chef [Cron]

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

cron ‘job1’ do
minute ‘*/5’
command “some command here &> /dev/null”
end

Chef Basics [attributes if else, loop ]

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

#replace
Ohai2u tikejhya@web1-prod1!
chef (12.14.89)> node.name.gsub(/.*-/, ”)
=> “prod1″
chef (12.14.89)> node.name.gsub(/-.*/, ”)
=> “web1”
chef (12.14.89)>

#array of packages to install
default[‘dep’][‘packages’] = %w(mysql php-pdo php-ldap php-gd php-pear httpd php-cli php-mysql php-xml php-mbstring php-pecl-memcache php-devel php-common php php-mcrypt php-pecl-apc php-soap vsftpd)

# marking admin value if condition met
node.run_list?(‘role[admin]’) == true ? default[‘admin’] = TRUE : default[‘admin’] = FALSE

# if else
if node[‘admin’]
default[‘php’][‘max_execution_time’] = 30
else
default[‘php’][‘max_execution_time’] = 180
end

# case statement with loop’s
case node.chef_environment
when ‘prod’
default[‘nfs’][‘nfs_mount_point’] = ‘/data/en-UK/media’
when ‘prod2’
default[‘nfs’][‘nfs_mount_point’] = ‘/data/en-UK/media/files’
when ‘prod3’
%w(en-UK de-DE es-ES).each do |sites|
default[“#{sites}”][‘efs_mount_point’] = “/data/#{sites}/media/efs-files”
default[“#{sites}”][‘nfs_mount_point’] = “/data/#{sites}/media/nfs-files”
default[“#{sites}”][‘app_mount_point’] = “/data/#{sites}/media/files”
end

Chef [data bags]

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

#Chef using data bags value

#Lets see data bag called staging for webserver
$ knife data bag show staging webserver
mysql:
hostname: db1.tikeweb.com
username: admin

#load data bag into some holder
config = Chef::DataBagItem.load(‘staging’, ‘webserver’)

#load mysql hostname
node.default[‘mysql-hostname’] = config[‘mysql’][hostname]
or
node.default_unless[‘mysql-hostname’] = config[‘mysql’][hostname]

#This can now be used as variable inside recipe which you want to push via template.

Chef Basics [chef-client]

#Chef-client pull recipe from client
chef-client -o ‘recipe[filebeats]’
chef-client -o ‘recipe[filebeats]’ -l debug

Chef Basics [Knife]

#save cookbook changes
knife cookbook upload tike_web

#Show role
knife role show web

#list all client
knife node list

# show user specific databags
knife data bag show users ashnep

# show environment specific databags
knife data bag show prod prod_data

#Create cookbook
knife cookbook create filebeats

#test configuration
rubocop cookbooks/filebeats

#Upload cookbook
knife cookbook upload filebeats

#show node data
knife node show ashnep-test

#Chef bootstrap
#bootstrap 10.10.10.1
-N NODENAME
-r RUN_LIST, –run-list RUN_LIST
-E ENVIRONMENT, –environment ENVIRONMENT
-x USERNAME, –ssh-user USERNAME
–use-sudo-password (Perform a bootstrap operation with sudo; specify the password with the -P (or –ssh-password) option)

knife bootstrap 10.10.10.1 -N web1.tikejhya.com -r "role[web]" --environment dev -x tikejhya --sudo --use-sudo-password -i ~/.ssh/my_prv

Bash format option [disable autocommenting]

#Bash format option [disable autocommenting]
:set formatoptions-=cro

Magento admin user password hash

# Magento stores user password inside admin_user and hashing is done with some random key
#So here we are resetting password with somepass for user called username

UPDATE admin_user SET password=CONCAT(MD5('qXsomepass'), ':qX') WHERE username = 'username';

Mac OSX port forwarding

Mac OSX port forwarding

echo "rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080" | sudo pfctl -ef -

percona sync simplified to run over huge database

#!/bin/bash
if [ $# -lt 3 ] ; then
echo "USAGE $0 slavename db_name type"
echo "slavename [e.g db2, db3, db4 ]"
echo "db_name [e.g db1 db2 etc... calm down one at a time]"
echo "type [xxl_table_array xl_table_array unwanted_table all_small]"
exit
fi

LOCKFILE="/tmp/pt_sync.lock"
if [ -e "${LOCKFILE}" ]; then
echo "Already running."
exit 99

else

echo $! > "${LOCKFILE}"
chmod 644 "${LOCKFILE}"
slavehost=$1
backup_dir="./$1-table-sync-`date +%Y-%m-%d-%H-%M`"
mkdir -p $backup_dir
table_size=$3
db=$2

get_list_of_tables()

{
case "$table_size" in
xxl_tables)
tbls="xxl_table_name"
;;
xl_tables)
tbls="xl_table_1 xl_table_2"
;;
large_tables)
tbls="large_table_1 large_table_2"
;;
unwanted_table)
tbls="temp_table1 temp_table2"
;;
all_small)
allsmalltable_sync
;;
*)
echo "chow chow; did you miss anything?"
;;
esac
}

execute_query()
{
for tb in $tbls; do
pt-table-sync h=db1,D=$db,t=$tb h=$slavehost --user=username --password=password --no-check-slave --print >> $backup_dir/$db-$tb.log

done
}
allsmalltable_sync()
{
tbls="$(mysql -Bse "use $db; show tables;" | grep -v 'all_but_shit_tables\|all_but_shit_table_2')"
for tb in $tbls; do
pt-table-sync h=db1,D=$db,t=$tb h=$slavehost --user=username --password=password --no-check-slave --print >> $backup_dir/$db-$tb.log

done
}

get_list_of_tables $2
execute_query
rm -f "${LOCKFILE}"

fi

docker basics

#How to install Docker in centos or similar flavours
yum install docker-io

#Lets start service now
service docker start

#ensure its in boot
chkconfig docker on

#Explore other commands
docker help

# Pull image from docker repository
docker pull centos

# Lets run bash from container.
docker run -i -t centos /bin/bash

#Show me current running docker containers
docker ps

#Show me all container
docker ps -a

#show me latest container
docker ps -l

#Attach to detached container using container ID
docker attach c3852139b652

#List currently running docker images.
[root@173 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
fe4fa1ab8f74 centos "/bin/bash" 2 days ago Up 2 days
serene_turing
990c4b7837c7 centos "/bin/bash" 4 days ago Up 4 days
silly_cray

# Get ip address of most recent docker image
[root@173 ~]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)

172.17.0.2

#Show all images (any status)
[root@173 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe4fa1ab8f74 centos "/bin/bash" 2 days ago Exited (137) 5 minutes ago serene_turing

#Attach certain images using Names

[root@173 ~]# docker start -ai serene_turing

#How to detach docker mode:
Ctrl+p, Ctrl+q

#Start docker container with network using hosts
#This is used as workaround with the issue of centos 6.7
$ docker run -d --net=host -it centos /bin/bash

#Install sshd inside container
$ docker exec -it fe4fa1ab8f74 yum -y install openssh

Problems:
docker cp /etc/ssh/sshd_config serene_turing:/etc/ssh/sshd_config
Error: Path not specified

#Docker CP from host to container is only available from 1.8 onwards

Workaround
$ cat /local/file/path | docker exec -i sh -c 'cat > /inside/docker/file/path'

#Lets see the copied file:
docker exec -it fe4fa1ab8f74 cat /etc/ssh/sshd_config

#How to commit change of container into new container
docker commit fe4fa1ab8f74 tikejhya/base_image:version1

#Docker run and exit
docker run -d tikejhya/base_image:version1

#How to start docker with /bin/bash using IMAGEVersion
docker run -d --net=host -it tikejhya/base_image:version1 /bin/bash

#How to run centos docker container
docker run -i -t centos /bin/bash

#How to expose port 80 and 443 of host machine to guest machine
docker run -i -p80:80 -p443:443 -t nginx /bin/bash

#How to rename docker existing container
docker rename 95f08c928a66 web1_tikejhya_nginx

#Show me latest container
#-l, –latest=false Show the latest created container, include non-running
#-q, –quiet=false Only display numeric IDs
docker ps -q -l

# Resolution inside docker
vi /etc/init.d/docker
DOCKER_OPTS=" --dns 172.25.0.56"

#Docker run forward port Image/name:tag
docker run -p80:80 -p443:443 -t tikejhya/base_image_web1_tikejhya_nginx:version1 /bin/bas

### delete docker images that was on status exited weeks ago
docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

#To expose just one port, this is what you need to do:
docker run -p :

#To expose multiple ports, simply provide multiple -p arguments:
docker run -p : -p :

#pull mysql image from
$ docker pull mysql
#Docker run -name provide some name with mysql root password using latest image
[root@173 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mysql latest 6f8e95fd9237 3 days ago 400.1 MB

$ docker run –name db1.ashishnepal -e MYSQL_ROOT_PASSWORD=mynewpassword -d mysql:latest

# docker exec db1.ashishnepal mysql -V
$ mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper

[root@173 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx latest ae324ca2b946 3 days ago 181.6 MB
mysql latest 6f8e95fd9237 3 days ago 400.1 MB

$ docker run –name web1.ashishnepal –link db1.ashishnepal:mysql -d nginx

[root@173 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f40cadc4b463 nginx “nginx -g ‘daemon of 7 seconds ago Up 6 seconds 80/tcp, 443/tcp web1.ashishnepal
3f366e92bd38 mysql:latest “docker-entrypoint.s 2 days ago Up 2 days 3306/tcp db1.ashishnepal

Page 2 of 41

Powered by WordPress & Theme by Anders Norén