Configure a highly available cluster with keepalived-1
Background requirements: use keepalived to configure high availability, monitor NGINX services, and now enterprises use NGINX for load balancing.
Ready to work:
• Set hostname, respectively master and backup
Set hostname on the main
#hostnamectl set-hostname master
set hostname from above
#hostnamectl set-hostname backup
• Two machines, both centos7.3, set the network card ens33 as follows:
master 192.168.71.100
backup 192.168.71.166
• Turn off the firewall
Both master and slave operate
# iptables -F clear rules
# service iptables save save rules
Both master and slave close selinux
# setenforce 0 temporary shutdown
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config will take effect permanently after restart
• Install keepalived on both machines, execute yum install -y keepalived
• Install nginx on both machines, execute yum install -y nginx
• Edit the keepalived configuration file on the master
• Set vip to 192.168.71.110
# > /etc/keepalived/keepalived.conf // Clear the contents of the original configuration file
# vim /etc/keepalived/keepalived.conf //Add the following:
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.71.110
}
track_script {
chk_nginx
}
}
• Edit the master monitoring script
# vim /usr/local/sbin/check_ng.sh //Add the following:
#!/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 check the number of nginx processes again,
#If it is still 0, it means that nginx cannot be started, and keepalived needs to be turned off at this time
if [ $n -eq "0" ]; then
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
be
be
• Give the script 755 permissions
#chmod 755 /usr/local/sbin/check_ng.sh
Start the master service
#systemctl start keepalived
[root@wjh-01 selinux]# ps aux|grep keepalived //keepalived service has been started
root 4307 0.0 0.1 126340 1856 pts/0 S+ 14:39 0:00 vi /etc/keepalived/keepalived.conf
root 25588 1.7 0.1 120212 1464 ? Ss 15:19 0:29 /usr/sbin/keepalived -D
root 25589 0.0 0.3 126824 3300 ? S 15:19 0:00 /usr/sbin/keepalived -D
root 50572 0.1 0.3 131148 3008 ? S 15:43 0:00 /usr/sbin/keepalived -D
root 51002 0.0 0.0 112664 972 pts/1 R+ 15:47 0:00 grep --color=auto kee
[root@wjh-01 selinux]# ps aux|grep nginx //nginx service has been started
root 48392 0.0 0.2 122284 2072 ? Ss 15:41 0:00 nginx: master process nginx
nginx 48394 0.0 0.3 122712 3104 ? S 15:41 0:00 nginx: worker process
root 51064 0.0 0.0 112664 972 pts/1 R+ 15:47 0:00 grep --color=auto nginx
• View defined VIPs are also enabled
Configure a highly available cluster with keepalived-2
• Edit the keepalived configuration file on the backup
# > /etc/keepalived/keepalived.conf // Clear the contents of the original configuration file
# vim /etc/keepalived/keepalived.conf //Add the following:
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#Global configuration, define failure, send email to that mailbox
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
#Define the startup script for detecting nginx
interval 3
#3 seconds to check a service
}
#keepalived CHK module, check if NGINX service is enabled
vrrp_instance VI_1 {
state BACKUP
#Define whether the role is master or slave
interface ens33
#Define the network card and send the VRRP protocol
virtual_router_id 51
#Define routing ID, master and slave are consistent
priority 90
#define weights
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
#Define the authentication mechanism is PASS
virtual_ipaddress {
192.168.71.110
}
#Define VIP (IP that provides services to the outside world)
track_script {
chk_nginx
}
}
• Edit backup monitoring scripts
# vim /usr/local/sbin/check_ng.sh //Add the following:
#!/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 check the number of nginx processes again,
#If it is still 0, it means that nginx cannot be started, and keepalived needs to be turned off at this time
if [ $n -eq "0" ]; then
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
be
be
• Give the script 755 permissions
#chmod 755 /usr/local/sbin/check_ng.sh
• Start the backup service
#systemctl start keepalived
Configure a highly available cluster with keepalived-3
test
Test the web content http://192.168.71.110 on both machines through a browser. To differentiate, we can
Modify the default page of nginx:
execute on master
# echo "master" >/usr/share/nginx/html/index.html
Execute on backup
# echo "backup" >/usr/share/nginx/html/index.html
• First determine the nginx difference between the two machines, for example, you can check the nginx version by curl -I
• Test 1: Shut down the nginx service on the master
• Test 2: Shut down the keepalived service on the master
[root@wjh-01 ~]# curl 192.168.71.110
master
[root@wjh-01 ~]# systemctl stop keepalived
[root@wjh-01 ~]# curl 192.168.71.110
backup
• Test 3: Start the keepalived service on the master
[root@wjh-01 ~]# systemctl start keepalived
[root@wjh-01 ~]# curl 192.168.71.110
master