Chef



Chef server part 1
===================
Enviroments
===============
think of it as development enviroment..development,test,staging,production
etc
model lif stages
may include url,location of your package repository,version of chef configuration

ROLES
=======
-->represents the type of servers in your infrastructure
like load balancer,app server ,monitoring server, DB server,DB cache server etc.

--allows you to define policy,may include list of chef config files (also called run list) to be applied.
--may include data attributes necessary for configuring your infrastructure, eg port that app server listens on,list of app to be deployed

Nodes
=============
nodes represents physical/virtual server in your infrastructure(may be in a public or pvt cloud).
each node will --belong to one organization,one enviroment,
have zero or more role.

Resources
===========
represents piece of system and its desired state eg. a package that should be installed,service to run,
file to be configured, cron job to run,a user which should be managed etc.
~ are fundamental building blocks of chef configuration.
~  are gathered into recipes...recipes ensure system is in desired state.

Recipe
========
collection of resources..help define the policy that should be  applied to node or in any component of  infrastructure,,it is configuration files that describe resources and its desired state
--it can install and configure s/w components.
--~ can manage files,deploy apps,execute other recipes


Cookbooks
==================
multiple recipes are taken and packaged into Cookbook.
recipes are stored in cookbooks.
contains recipes,templates,files,custom.
it makes use of code reuse and modularity.

Runlist
=======
runlist is the thing that is applied to the node.
it is marrying of policy and the node.
run list is composed of recipes and roles(application server role,database server role etc).

Search feature
=================
application server nodes can ask Chef server to search for other available servers with Roles,topology data
can use ip address,hostnames,FQDNs

NIC Bonding

eth2==
          ||||==> bond0---->>[]
eth3==


----------------------------------------------------------------------------------------------------
 [root@server2 network-scripts]#cat  ifcfg-eth2   
DEVICE=eth2
HWADDR=00:0c:29:5b:39:1d
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPV6INIT=no
USERCTL=no
##########################################
MASTER=bond0
SLAVE=yes

##########################################
#IPADDR=192.168.213.112
#NETMASK=255.255.255.0
#GATEWAY=192.168.213.2
#DNS1=8.8.8.8
[root@server2 network-scripts]#
[root@server2 network-scripts]# cat  ifcfg-eth3   
DEVICE=eth3
HWADDR=00:0c:29:5b:39:27
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
USERCTL=no
MASTER=bond0
SLAVE=yes

[root@server2 network-scripts]#
[root@server2 network-scripts]# cat ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPV6INIT=no
USERCTL=no
IPADDR=192.168.213.112
NETMASK=255.255.255.0
GATEWAY=192.168.213.2
DNS1=8.8.8.8

###below are setting for mode0 (mode zero)#####
#mode=0  for round robin and mode=1 for active backup
# miimon = n milliseconds  to check if any eth is down

BONDING_OPTS="mode=0  miimon=100"


 [root@server2 network-scripts]# watch -n .1 cat /proc/net/bonding/bond0

Changing passwords from scripts (docker file)

You can run the passwd command and send it piped input. So, do something like:
echo thePassword | passwd theUsername --stdin
 
You could also use chpasswd:
echo username:new_password | chpasswd   
echo username:newpassword   | chpasswd
so, you change password for user username to new_password.

Docker container commands

check docker version
------------------------
docker version    

Search for image


docker search urgensherpa/lamp6
redhat@test:~/ssh_image$ docker search urgensherpa
NAME                        DESCRIPTION   STARS     OFFICIAL   TRUSTED
urgensherpa/lamp6                                        0                   

Pulling image
you@tutorial:~$ docker pull learn/tutorial
Pulling repository learn/tutorial from https://index.docker.io/v1
Pulling image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c (precise) from ubuntu
Pulling image b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc (12.10) from ubuntu
Pulling image 27cf784147099545 () from tutorial
you@tutorial:~$

Run commands in container   
docker run learn/tutorial echo "hello world"

installing things in the container

docker run learn/tutorial apt-get install -y ping  

docker ps -l
docker ps
=== ========== ========= =        ====================
docker commit 698989sdafd learn/ping  
====meaning==
698989sd...= container id
learn/ping = learn -username and ping - image
-----------------------------------------------------------
docker run learn/ping ping google.com      
Find info about docker container
--------------------------------
docker inspect efesdlfk     

docker images   = This will show you which images are currently on your host

docker push learn/ping   

fail2ban on Ubuntu 12.04 LTS

Simple fail2ban config
- - - - -- - - - - -  - --  - -
#sudo apt-get install fail2ban

Now to fine tune default configs

#cd /etc/fail2ban

#sudo vim jail.conf
#it also help protect ssh ftp apache asterisk ...
.
.
bantime  = 989898     ##in seconds
maxretry = 3     ##place your threshold value

#search for jails
#JAILS

[ssh]
enabled=true
port =ssh
filter = sshd
logpath = var/log/auth.log
maxretry = 3

save/exit
................................
#service fail2ban restart

Change language setting os = ubuntu 13.04

# vim /etc/default/locale

 //change to below param's if its like eg. LANG="np_NP"                                    
LANG="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
.
..
...

# sudo locale-gen

logout and login again

Autostart script at runtime /etc/rc.local


The /etc/rc.local file is common to major Linux distributions.
This file contains BASH commands which will be run after run-level specific commands whenever the system is booted.
In a VPS environment, /etc/rc.local is commonly used to store the hostname <SERVER_HOSTNAME>command (where <SERVER_HOSTNAME> is the intended hostname for the VPS) to ensure that the VPS hostname is not reset when the VPS is rebooted.
                                                                         Method 1                                     
redhat@cloud01:/etc/init.d$ nano rc.local 


  GNU nano 2.2.6                                      File: rc.local                                                                                    


        if [ -x /etc/rc.local ]; then

                [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
                /etc/rc.local
                ES=$?
                [ "$VERBOSE" != no ] && log_end_msg $ES
                return $ES
        fi
}

case "$1" in

    start)
        do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
###########################at the end of file >>>>

/root/script/mystartupscript1.sh

/root/script/mystartupscript2.sh  &    #mystartupscript2  & ==the & sends the script to background 
chown wso2:docker /var/run/docker.sock
modprobe aufs
sudo killall dnsmasq


##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

                                                        Method 2  #vim   /root/scripts/mystartupscript1.sh
boot=$(date)
echo "the system boot at $boot " >> /var/log/custombootlog.log

#chmod u+x mystartupscript1.sh

#vim /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#put your script/command here

chown wso2:docker /var/run/docker.sock
modprobe aufs
sudo killall dnsmasq
/root/script/mystartupscript1.sh
/root/script/mystartupscript2.sh &

exit 0


docker1&2 setup note

FLOATING_RANGE=192.168.140.0/27
FIXED_RANGE=10.11.12.0/24
FIXED_NETWORK_SIZE=256
FLAT_INTERFACE=eth1
ADMIN_PASSWORD=g
MYSQL_PASSWORD=g
RABBIT_PASSWORD=g
SERVICE_PASSWORD=g
SERVICE_TOKEN=g
SCHEDULER=nova.scheduler.filter_scheduler.FilterScheduler
VIRT_DRIVER=docker
SCREEN_LOGDIR=$DEST/logs/screen
----------------------------------------------------------------
 cat install_docke0.sh
#!/usr/bin/env bash

# **install_docker.sh** - Do the initial Docker installation and configuration

# install_docker.sh
#
# Install docker package and images
# * downloads a base busybox image and a glance registry image if necessary
# * install the images in Docker's image cache


# Keep track of the current directory
SCRIPT_DIR=$(cd $(dirname "$0") && pwd)
TOP_DIR=$(cd $SCRIPT_DIR/../..; pwd)

# Import common functions
source $TOP_DIR/functions

# Load local configuration
source $TOP_DIR/stackrc

FILES=$TOP_DIR/files

# Get our defaults
source $TOP_DIR/lib/nova_plugins/hypervisor-docker

SERVICE_TIMEOUT=${SERVICE_TIMEOUT:-60}


# Install Docker Service
# ======================

# Stop the auto-repo updates and do it when required here
NO_UPDATE_REPOS=True

# Set up home repo
curl https://get.docker.io/gpg | sudo apt-key add -
install_package python-software-properties && \
    sudo sh -c "echo deb $DOCKER_APT_REPO docker main > /etc/apt/sources.list.d/docker.list"
apt_get update
install_package --force-yes lxc-docker-${DOCKER_PACKAGE_VERSION} socat
###############################################################################################
cat install_docke1.sh
#!/usr/bin/env bash

# **install_docker.sh** – Do the initial Docker installation and configuration

# install_docker.sh
#
# Install docker package and images
# * downloads a base busybox image and a glance registry image if necessary
# * install the images in DockerÂ’s image cache

# Keep track of the current directory
SCRIPT_DIR=$(cd $(dirname $0?) && pwd)
TOP_DIR=$(cd $SCRIPT_DIR/../..; pwd)

# Import common functions
source $TOP_DIR/functions

# Load local configuration
source $TOP_DIR/stackrc

FILES=$TOP_DIR/files

# Get our defaults
source $TOP_DIR/lib/nova_plugins/hypervisor-docker

SERVICE_TIMEOUT=${SERVICE_TIMEOUT:-60}

# Install Docker Service
# ======================

# Stop the auto-repo updates and do it when required here
NO_UPDATE_REPOS=True

# Start the daemon – restart just in case the package ever auto-starts…
#restart_service docker

echo Waiting for docker daemon to start
DOCKER_GROUP=$(groups | cut -d' ' -f1)
CONFIGURE_CMD="while ! /bin/echo -e 'GET /v1.3/version HTTP/1.0\n\n' | socat - unix-connect:$DOCKER_UNIX_SOCKET | grep -q '200 OK'; do

echo failure
sleep 1
done"
if ! timeout $SERVICE_TIMEOUT sh -c "$CONFIGURE_CMD"; then
die $LINENO docker did not start
fi

Changing Priority Values for process/scripts

Changing priority of process/or running script :
-------------------------------------------------
Use top or ps
------------------------------------------------
eg. ./myscript.sh
#ps -ef | grep myscript
get the process id...
eg.4545
#ps -o pid,comm,nice -p 4545            //pid,,comm=command,nice=nice value of process , -p = processid, -o = outputformat

#ps -efl     <displays nice values>
....
......
#renice -15 -p 4545

// / nice value ranges from -20 to 19 ..negative value have higher priority(eg.-20 is highest priority and 19 have lowest , default priority is 0)
==============================================
To set nice value/ priority for process/script/cmd to be started
======================================================
#nice -n 10 apt-get upgrade
#nice -n -11 ./myscript.sh

user del mod etc..

Display list of shell
Code:
less  /etc/shells
cat  /etc/shells
more /etc/shells
Change the user login shell to /bin/ksh for foo user
Code:
chsh -s /bin/ksh foo
chsh -s /bin/bash username

-----------------
Adding user
-----------------
Syntax: # useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName


-s SHELL : Login shell for the user.
-m : Create user’s home directory if it does not exist.
-d HomeDir : Home directory of the user.
-g Group : Group name or number of the user.
UserName : Login id of the user.

Create Linux User with Custom Configurations Using useradd Command
-----------------------------------------------------------------------------
The following example creates an account (lebron) with home directory /home/king, default shell as /bin/csh and with comment “LeBron James”.

# useradd -s /bin/csh -m -d /home/king -c "LeBron James" -g root lebron

# grep lebron /etc/passwd

lebron:x:513:0:LeBron James:/home/king:/bin/csh
 -------------------------
Verify user information:
-------------------------
# finger david

Output:

Login: david                      Name: david gilmour
Directory: /iscsi/user/vivek                Shell: /bin/bash
Last login Thu Sep 13 07:58 2007 (IST) on pts/1 from 10.16.15.2
No mail.
No Plan.
------------------------------------------------------------------------------------------------------
You can use the usermod command. The following (run as root) will change the user smith's 
home directory to /tmp/smith and move all of their files with it.

Code:
usermod -m -d   /tmp/smith smith


Add an Existing User to a Group
Next we’ll add a user to the group, using this syntax:
usermod -a -G <groupname> username
eg. usermod -a -G dockergroup  stackuser