System developer, devops, docker container, kubernetes, docker cluster, docker swarm, continuous integration, gitlab, github, docker swarm, automation tools ...

Friday, January 15, 2016

How to config bonding teaming in CentOS 6

4:11 PM Posted by Unknown , , , No comments
This article talk about teaming two ethernet card, with config:

First, you need create a file config call ifcfg-bond0

# vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
IPADDR=172.30.27.42
NETMASK=255.255.255.0
GATEWAY=172.30.27.22
DNS1=210.245.31.145
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
BONDING_OPTS="mode=5 miimon=100"

remember bonding teaming have 6 mode:

  • Mode 0 – Round-robin (RR) policy: Packet transmissions is done in a sequential order. For ex, the first packet is transmitted using the interface and second packet is done through next interface and so on. This mode provides load balancing and fault tolerance.
  • Mode 1 – active-backup policy: Packet transmission happens only through the active interface and the traffic is routed through the backup interface only when the active interface went down.
  • Mode 2 – XOR policy: This mode balances outgoing traffic across the active ports. In this mode the same slave is used for transmission for each destination MAC address. This provides load balancing and fault tolerance.
  • Mode 3 – broadcast policy: This transmits everything on all slave interfaces. This mode provides fault tolerance.
  • Mode 4( 802.3ad ): This mode is used to create the link aggregation groups that share the same speed and duplex settings.
  • Mode 5( balance-tlb ): Adaptive transmit load balancing. The outgoing traffic is distributed based on the current load on each slave. Incoming traffic is received by the current slave. If the current slave fails, then another slave takes over the incoming traffic based on MAC address.
  • Mode 6( balance-alb ): Adaptive load balancing. In this, the incoming traffic is handled to use load balancing across all the slaves. The receive load balancing is done through ARP negotiation.
then, you will config ifcfg-eth0 and eth1 in order to teaming 

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
HWADDR=00:0C:29:5D:68:39
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

# vi /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes

NOTE: you need stop service NetworkManager 

# service NetworkManager stop

# vi /etc/modprobe.d/bonding.conf
alias bond0 bonding 

# modprobe -v bonding
# service network restart

Check bond0 interface bonding:

# cat /proc/net/bonding/bond0
# ifconfig -a

Done!




Thursday, January 14, 2016

How to config sql-relay with ms-sql, high-availability with keep-alived

11:28 AM Posted by Unknown , , 1 comment
Topology




SQL-relay01 & SQL-relay02
       OS: CentOS 6.8 bit final.
       Service: sql-relay, keepalived.

DB01 & DB02
       OS: Windows server 2008 sp2.
       Service: MS-SQL 2012 sp2.

Step 1 - Install sql-relay and keepalived


1. Installation Rudiments

  # Tar vxzf rudiments-0.28.2.tar.gz
  # Cd rudiments-0.28.2
  #. /Configure --prefix =/usr/local/rudiments
  # Make
  # Make install 

  At this point, rudiments end of the installation 

2. Installation of SQL Relay

  # Tar vxzf sqlrelay-0.36.4.tar.gz
  # Cd sqlrelay-0.36.4
  #. /Configure --prefix=/usr/local/sqlrelay --with-rudiments-prefix=/usr/local/rudiments --with-freetds-prefix=/etc
  # Make
  # Make install 

  Installation of the end, the above parameters compiled based on individual needs to set up, because I also need Oracle SQLserver MySQL, this has added so many parameters.

3. Installation Free-TDS

# yum install -y freetds
# vi /etc/freetds.conf
[ServerLocal7]
        host = 172.30.27.7
        port = 1433
        tds version = 7.0
        client charset = GB2312
[ServerLocal11]
        host = 172.30.27.11
        port = 1433
        tds version = 7.0
        client charset = GB2312

Test FreeTDS
# TDSVER=7.0 tsql -H myhostname.mssqlserver.com -p 1433 -U myusername


DB01 and DB02, i install MS-SQL 2012 sp2. FreeTDS is soft establish to database ms-sql, between CentOS and Windows Server 2008.


4. Install and config KeepAlived

# yum install -y keepalived
# vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
        script "/usr/local/bin/check_nginx.sh"
        interval 2
        fall 2
        rise 2
}

vrrp_instance VI_1 {

        interface eth0
        state MASTER
        virtual_router_id 51
        priority 101                    # 101 on master, 100 on backup
        nopreempt

        unicast_src_ip 172.30.27.36

        unicast_peer {
                172.30.27.71
        }

        virtual_ipaddress {

            172.30.27.38
        }
        track_script {
            chk_nginx
        }
        notify /usr/local/bin/keepalived.state.sh

}

# vi /usr/local/bin/check_nginx.sh

#!/bin/bash
HAPROXY_STATUS=$(/bin/ps ax | grep -w [n]ginx)
if [ "$NGINX_STATUS" != "" ]
then
  exit 0
else
  logger "Nginx is NOT running. Setting keepalived state to FAULT."
  exit 1
fi


# vi /usr/local/bin/keepalived.state.sh

#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
echo $STATE > /var/run/keepalived.state


# ip addr show


This command is show server hold virtual ip.

SQL-relay will define servers database at the backend, and connect to servers. Here is sql-relay config, example:

# vi /usr/local/sqlrelay/etc/sqlrelay.conf
<?xml version="1.0"?>
<!DOCTYPE instances SYSTEM "sqlrelay.dtd">

<instances>
        <instance id="master" port="9001" socket="/tmp/master.socket" dbase="freetds" connections="10" maxconnections="20" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="5" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass">
                <users>
                        <user user="mssql" password="password"/>
                </users>
                <connections>
                        <connection connectionid="master" string="server=ServerLocal7;db=Data;user=sa;password=password;" metric="1"/>
                        <connection connectionid="slave" string="server=ServerLocal11;db=Data;user=sa;password=password;" metric="1"/>
                </connections>
        </instance>
</instances>

# export PATH=$PATH:/usr/local/sqlrelay/bin 
# service sqlrelay start
# sqlr-start -id master
          This topology have two sql-relay server running keepalived for high-availability, sure for service sql-relay running 24/7. And use FreeTDS establish connection to ms-sql running on Windows server 2008 r2.






Wednesday, January 13, 2016

How to config HAproxy, KeepAlived, Nginx, PHP-FPM

This docs talk about high-availability service by Keepalived, HAproxy define port listen at the front-end and forward request into web server backend.


Topology
       I have a topology include: 
               - Two server running HAproxy, KeepAlived service with a virtual IP address.
               - Two server running Nginx - web server backend.

Server information:
 - HAproxy01
          OS:             CentOS 6.8 64bit final.
          IP addr:       192.168.22.54/24
          Port:  TCP 80.

 - HAproxy02:
          OS:              CentOS 6.8 64bit final.
          IP addr:        192.168.22.56/24
          Port:  TCP 80.

Virtual IP addr
          192.168.22.55 
           port: TCP 80

 - Webserver01
          OS:               CentOS 6.8 64bit final
          IP addr:         192.168.22.59/24
          Port: TCP:     8080

 - Webserver02
          OS:               CentOS 6.8 64bit final
          IP addr:         192.168.22.60/24
          Port: TCP:     8080


First, i config nginx running for a php site, so i will config php-fpm more. Here is nginx config:

# vi /etx/nginx/nginx.conf
server {
        listen 8080;
        server_name   example-site.org;
        index index.php;
        root /home/nginx/example-site.org;


        access_log off;

        error_log   /var/log/nginx/example-site-error.log error crit;
        
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_send_timeout 300;
            fastcgi_connect_timeout 300;
            fastcgi_read_timeout 300;
            fastcgi_buffer_size 32k;
            fastcgi_busy_buffers_size 32k;
            fastcgi_buffers 8 16k;
            fastcgi_temp_file_write_size 32k;
            fastcgi_intercept_errors on;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
}

php-fpm config:

# vi /etc/php-fpm.d/www.conf
[www]
listen = /var/run/php-fpm/php-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
pm = dynamic
pm.max_children = 13
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500
pm.status_path = /php_status
request_terminate_timeout = 120s
request_slowlog_timeout = 4s
slowlog = /home/nginx/logs/php-fpm-slow.log
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
env[HOSTNAME] = \$HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
php_admin_value[error_log] = /home/nginx/logs/php-fpm-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files

php_value[session.save_path] = /var/lib/php/session


Nginx use unix socket in order to improve performance, faster than call IP:port.
Next, i config HAproxy service on the HAPROXY01 and HAPROXY02 in order to forward request from client.

HAproxy config:

defaults
 mode http
 option http-server-close
 timeout client 20s
 timeout server 20s
 timeout connect 4s
frontend ft_app
 bind 192.168.22.54:80 name app
 default_backend bk_app
backend bk_app
 server s1 192.168.22.59:80 check
 server s2 192.168.22.60:80 check backup

Finally, i config KeepAlived service for HA two server running HAproxy. I need a virtual IP address, between two server HAPROXY have channel called "heartbeat" with responsibility communicate servers. And when server HAProxy is down, or service HAproxy stop, it will make HAPROXY02 become MASTER state.

KeepAlived config:

# vi /etc/keepalived/keepalived.conf

vrrp_script chk_nginx {

        script "/usr/local/bin/check_nginx.sh"
        interval 2
        fall 2
        rise 2
}

vrrp_instance VI_1 {

        interface eth0
        state MASTER
        virtual_router_id 51
        priority 101                    # 101 on master, 100 on backup
        nopreempt

        unicast_src_ip 192.168.22.54

        unicast_peer {
                192.168.22.56
        }

        virtual_ipaddress {

            192.168.22.55
        }
        track_script {
            chk_nginx
        }
        notify /usr/local/bin/keepalived.state.sh

}

# vi /usr/local/bin/check_nginx.sh
#!/bin/bash
HAPROXY_STATUS=$(/bin/ps ax | grep -w [n]ginx)
if [ "$NGINX_STATUS" != "" ]
then
  exit 0
else
  logger "Nginx is NOT running. Setting keepalived state to FAULT."
  exit 1
fi


# vi /usr/local/bin/keepalived.state.sh

#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
echo $STATE > /var/run/keepalived.state


# ip addr show

This command is show server hold virtual ip: 192.168.22.55

If you get error about php-fpm, nginx, maybe you forget set permission for session php, try:

# mkdir -p /var/log/nginx
# chown -R nginx:nginx /var/log/nginx
# chown -R nginx:nginx /var/lib/php/session