Linux Cluster Architecture (Cluster Introduction, Keeping Alive Introduction, Configuring High Availability Clusters with Keeping Alive)

Posted by matjaz on Fri, 17 May 2019 03:48:37 +0200

Cluster introduction

Overview of Linux Cluster

According to the function, it can be divided into two categories: high availability and load balancing.

High availability clusters usually consist of two servers, one working and the other serving as redundancy. When the serving machine goes down, redundancy will take over and continue to serve.

Open source software for high availability includes heartbeat (maintenance is not timely), keepalived (relatively more recommended)

Load balancing cluster needs a server as distributor, which is responsible for distributing users'requests to the back-end server for processing. In this cluster, besides distributors, it is servers that provide services to users. The number of these servers is at least 2.

Open source software for load balancing includes LVS, keepalived, haproxy, nginx, and commercial F5, Netscaler.

Introduction and Construction of Microsoft Service Framework

Introduction to keepalived

keepalived achieves high availability through VRRP (Virtual Router Redundancy Protocl) virtual routing redundancy protocol.

In this protocol, multiple routers with the same function will be organized into a group, in which there will be a master role and N (N > = 1) backup role.

Master will send VRRP packets to each backup in the form of multicast. When the backup can't receive VRRP packets from the master, it will think that the master is down. At this point, we need to decide who will become the new mater according to the priority of each backup.

Keepalived has three modules: core, check and vrrp. The core module is the core of keeping alived. It is responsible for the initiation, maintenance of the main process and loading and parsing of the global configuration file. The check module is responsible for the health check. The VRRP module is used to implement the VRRP protocol.

Configuring High Availability Clusters with Keeping Alive

Prepare two machines for experiment

yolks1 machine to do master: 192.168.248.128 Yum installation nginx yolks2 machine backup: 192.168.248.129 binary package installation nginx

1. Manual installation of keepalived yum is required

yum install -y keepalived

2. Need two machines to install nginx, if not installed, you can refer to the previous blog to operate, or use yum to quickly install

yum install -y nginx

Startup mode is

systemctl start nginx

3. Edit the configuration file of keepalived on master: / etc/keepalived/keepalived.conf, and overwrite the original file according to the following template. If you are not sure, backup the original file before you operate.

global_defs { #Global Configuration Definition Tag
   notification_email {  #Email alert in case of abnormality
     aming@aminglinux.com
   }
   notification_email_from root@aminglinux.com  #Mailbox from account
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx { #Check whether the service is normal
    script "/usr/local/sbin/check_ng.sh" #The content of the script is probably to check whether the service is normal or restart.
    interval 3 #Detection time, unit s
}

vrrp_instance VI_1 {  #Define master settings
    state MASTER #The role is master and the slave server is backup
    interface ens33 #Network card
    virtual_router_id 51 #Define router id
    priority 100 #weight
    advert_int 1
    authentication { #Authentication-related information
        auth_type PASS
        auth_pass aminglinux>com #Define password
    }
    virtual_ipaddress { #vip
        192.168.248.100
    }

    track_script { #Load
        chk_nginx
    }

}

vip: If the host machine hangs up, the two machines will be served from the server. vip specifies the address to be resolved by the standby machine, and this address should be shared by both master and slave.

4. The master machine edits the monitoring script file (/ usr/local/sbin/check_ng.sh), referring to the following code:

#!/bin/bash
#Time variable for logging
d=`date --date today +%Y%m%d_%H:%M:%S`
#Calculate the number of nginx processes
n=`ps -C nginx --no-heading|wc -l`
#If the process is 0, start nginx and detect the number of nginx processes again.
#If it's still 0, it means that nginx can't start, and you need to turn off keepalived
if [ $n -eq "0" ]; then
        # /etc/init.d/nginx start
		systemctl start nginx
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

5. Permissions to modify monitoring scripts written by master machines

chmod 755 /usr/local/sbin/check_ng.sh

6. Start the master machine keepalived

systemctl start keepalived

Check that both nginx service and keepalived are in good condition

[root@yolks-001 keepalived]# ps aux |grep nginx
root       2213  0.0  0.1 120752  2096 ?        Ss   00:21   0:00 nginx: master process /usr/sbin/nginx
nginx      2214  0.0  0.1 123220  3544 ?        S    00:21   0:00 nginx: worker process
root       2216  0.0  0.0 112676   984 pts/0    R+   00:22   0:00 grep --color=auto nginx
[root@yolks-001 keepalived]# ps aux |grep keepalived
root       2249  0.0  0.0 118608  1384 ?        Ss   00:22   0:00 /usr/sbin/keepalived -D
root       2250  0.0  0.1 129532  3340 ?        S    00:22   0:00 /usr/sbin/keepalived -D
root       2251  0.1  0.1 131556  2864 ?        S    00:22   0:00 /usr/sbin/keepalived -D
root       2281  0.0  0.0 112676   980 pts/0    R+   00:22   0:00 grep --color=auto keepalived

Testing whether the script written above is wrong, by manually stopping nginx found that there is no restart, at this time manual sh execution monitoring script found errors

[root@yolks-001 keepalived]# sh /usr/local/sbin/check_ng.sh 
/usr/local/sbin/check_ng.sh:Row 9: /etc/init.d/nginx: No file or directory

Look at line 9 of the file, which shows / etc/init.d/nginx start, because of the yum installation, at this point we modify the start command to system CTL start nginx

Re-execute the machine that has stopped normally, and stop nginx manually, the process of nginx will be executed automatically according to script.

  • If there is a problem with this step, both the master and slave machines need to check the following options in advance
    • 1. Whether SELinux is turned off
    • 2. Whether firewalld is turned off
    • 3. Is the script permission 755?

7. Configure backup to keep alived from the machine and add the following code from the machine configuration file **/etc/keepalived/keepalived.conf**:

global_defs {
   notification_email {
     aming@aminglinux.com
   }
   notification_email_from root@aminglinux.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/sbin/check_ng.sh"
    interval 3
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.248.100
    }

    track_script {
        chk_nginx
    }

}

8. Write a monitoring script **/usr/local/sbin/check_ng.sh** and add the following code:

#Time variable for logging
d=`date --date today +%Y%m%d_%H:%M:%S`
#Calculate the number of nginx processes
n=`ps -C nginx --no-heading|wc -l`
#If the process is 0, start nginx and detect the number of nginx processes again.
#If it's still 0, it means that nginx can't start, and you need to turn off keepalived
if [ $n -eq "0" ]; then
        ## systemctl start nginx
		/etc/init.d/nginx start
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi

9. Modify backup's monitoring script permissions from the machine

chmod 755 /usr/local/sbin/check_ng.sh

10. Start keeping alived from the machine

systemctl start keepalived

11.master is installed in yum, so the default access page is in / usr/share/nginx/html/index.html. We changed the default page to master.

[root@yolks-001 ~]# cat !$
cat /usr/share/nginx/html/index.html
this is master nginx default page .

Access Tips on windows

12. The default prompt for the backup machine is: This is a default site.

14.vip Address We use windows Access: 192.168.248.100

15. Testing High Availability

  • Test 1: Close the nginx service on master
  • Test 2: Adding iptabls rules to master
    • iptables -I OUTPUT -p vrrp -j DROP
  • Test 3: Close the keepalived service on master
  • Test 4: Start the keepalived service on master

Expand

Comparison of heartbeat and keepalived http://blog.csdn.net/yunhua_lee/article/details/9788433
Working Principle and Configuration of DRBD http://502245466.blog.51cto.com/7559397/1298945
mysql+keepalived http://lizhenliang.blog.51cto.com/7876557/1362313

Topics: Nginx yum Windows Linux