Showing posts with label DevOps. Show all posts
Showing posts with label DevOps. Show all posts

Sunday, 24 February 2019

How to run Shell Script


For premium DevOps and AWS Courses: https://imojo.in/2cox7em
  1. AWS with the project 
  2. DevOps Real-Time 
  3. DevOps with the project 
  4. AWS Real-Time
  5. Linux Admin


Running shell script


To run our script, enter:

1.     $ ./script.sh
The dot slash (./) denotes the current directory. This is necessary because the current directory is usually not in $PATH.

2.     $ bash script.sh
3.     $ source script.sh
The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.
4.     $ . script.sh
Bash defined source as an alias to the dot command.
Note that sourcing will run the commands in the current shell process while bash executing will run the commands in a new shell process.


For loop - print odd numbers from 1 to 10


Output should look like this:
1 3 5 7 9


Shell 1
#!/bin/bash

for i in 1 3 5 7 9
do
   echo -n "$i "
done
echo



Shell 2
#!/bin/bash

for i in {1..9..2}
do
   echo -n "$i "
done
echo



Shell 3
#!/bin/bash

for ((i=1; i<10; i+=2))
do
   echo -n "$i "
done
echo


Shell 4


#!/bin/bash

for i in {1..10}
do
  out=$(( $i % 2 ))
  if [ $out -eq 1 ]
  then
    echo -n "$i "
  fi
done



Ruby
Using "print" for no-new-line, the "puts" for a new line:


i = 1
loop do
   break if i > 10
   print "#{i} " unless i % 2 == 0
   i += 2
end
puts




Bash - delete the last line of files in a sub directory
We'll use 'vi' in the script:
#!/bin/bash

for file in sub/*
  do
    if [ -f $file ]

    then
      vi -c '$d' -c 'wq' "$file"
    fi
  done


The bash script will remove the last line from all files in a 'sub' directory.




Bash - Replacing a string in multiple files
When we need to replace a string across multiple files, we can use sed and the coding is similar to the one we used in the previous section:
#!/bin/bash

for file in ./*
  do
    if [ -f $file ]
    then
      sed -i 's/2016/2017/g' "$file"
    fi
  done


Note that we may tempted to do it with a single line with the "sed" command like this:
sed -i s/2016/2017/g *


But it won't work when we meet a directory. That's why we need to check if it is a file or not.




Bash - if statement
To be more familiarized with if statement, try the following script:
#!/bin/bash

TWO=2
for (( i=1;i<=3;i++ ))
do
  if [ ${i} -lt $TWO ];
  then
    echo "${i} is less then $TWO"
  elif [ ${i} -eq $TWO ];

  then
    echo "${i} is equal to $TWO"
  else
    echo "${i} is greater than  $TWO"
  fi
done 


Output:
1 is less then 2
2 is equal to 2
3 is greater than  2


To check if a string is not null nor a space:
#!/bin/bash

str1="Not Null"
str2=" "
str3=""
message="is not Null nor a space"

if [ ! -z "$str1" -a "$str1" != " " ]; then
  echo "str1 ${message}"
fi

if [ ! -z "$str2" -a "$str2" != " " ]; then
  echo "str2 ${message}"
fi

if [ ! -z "$str3" -a "$str3" != " " ]; then
  echo "str3 ${message}"
fi


Output:
str1 is not Null nor a space




Here document
The syntax looks like this:
command << delimiter document delimiter



The shell interprets the << operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command.
Example 1:
$ wc -l << EOF
    This is a simple lookup program
    for good (and bad) restaurants
    in Cape Town.
EOF
3
$



Example 2:
cat << EOF > /etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF


The command appends "deb http://apt.kubernetes.io/ kubernetes-xenial main" string to "/etc/apt/sources.list.d/kubernetes.list" file.


Example 3:

$ sed 's/_/-/g' << EOF
    2013_4_5
    2013_4_6
EOF
    2013-4-5
    2013-4-6




Saturday, 23 February 2019

Chapter 1 - High Availability Proxy Servers


High Availability Proxy Servers

For premium DevOps and AWS Courses: https://imojo.in/2cox7em
  1. AWS with the project 
  2. DevOps Real-Time 
  3. DevOps with the project 
  4. AWS Real-Time
  5. Linux Admin






Definition
HAProxy stands for High Availability proxy. HAProxy or High Availability Proxy is TCP and HTTP load balancer and proxy server software. It is a Free and open-source application written in C programming Language. HAProxy application is used as TCP/HTTP Load Balancer and for proxy Solutions. The most common use of the HAProxy application is to distribute the workload across multiple servers e.g., web server, database server, etc thus improving the overall performance and reliability of server environment.



Load Balancer:
HA Proxy: can do health checks, uses load balancing algorithms, sticky session,

Nginx:  Brainless (just forward request)

Architecture



HAProxy Environment Setup
HAProxy Server:
                                 IP Address:  192.168.56.201

                                 Hostname:  HAserver

HAProxy Client:

                                IP Address:  192.168.56.202 , 192.168.56.206
                              
                                Hostname:  HAclient1, HAclient2

Installing Apache on Client Machines


#yum install httpd

verify anyone of the server whether Apache is running by accessing it via IP address in browser.

http://192.168.56.212

Installing HAProxy Server


To install HAProxy on RHEL/CentOS/Fedora run the following command.

#yum install haproxy


Enabling Logging feature and Configure HAProxy


Enable logging feature in HAProxy for future debugging. Open the main HAProxy configuration file ‘/etc/haproxy/haproxy.cfg‘

# mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
#vi /etc/haproxy/haproxy.cfg

# create new
 global
      # for logging section
    log         127.0.0.1 local2 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
      # max per-process number of connections
    maxconn     256
      # process' user and group
    user        haproxy
    group       haproxy
      # makes the process fork into background
    daemon

defaults
      # running mode
    mode               http
      # use global settings
    log                global
      # get HTTP request log
    option             httplog
      # timeout if backends do not reply
    timeout connect    10s
      # timeout on client side
    timeout client     30s
      # timeout on server side
    timeout server     30s

# define frontend ( set any name for "http-in" section )
frontend http-in
      # listen 80
    bind *:80
      # set default backend
    default_backend    backend_servers
      # send X-Forwarded-For header
    option             forwardfor

# define backend
backend backend_servers
      # balance with roundrobin
    balance            roundrobin
      # define backend servers
    server             client2 192.168.56.202:80 check
    server             client4 192.168.56.206:80 check



Configure HAProxy Logs



Configure Rsyslog to get logs for HAProxy
#vi /etc/rsyslog.conf


# line 15,16: uncomment, lne 17: add

$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 127.0.0.1
# line 54: change like follows

*.info;mail.none;authpriv.none;cron.none
,local2.none
   /var/log/messages
local2.*                                                /var/log/haproxy.log


Next, we need to create a separate file ‘haproxy.conf‘ under ‘/etc/rsyslog.d/‘ directory to configure separate log files.


# vi /etc/rsyslog.d/haproxy.conf
Append following line to the newly create file.

local2.*            /var/log/haproxy.log

restart the rsyslog service to update the new changes.

# systemctl restart rsyslog
#systemctl restart haproxy