两台CentOS7,如下规划:
192.168.202.128 MASTER
192.168.202.129 BACKUP
两台虚拟机准备,关闭防火墙,下载所需包,本次实验对象是给nginx使用高可用(keepalived)
# getenforce Disabled //如若不是disabled# setenforce 0 //先暂时关闭selinx# yum install -y keepalived # yum install -y nginx
安装nginx时报错,提示找不到该包,就修改下载源,就可以成功安装nginx
vi /etc/yum.repos.d/nginx.repo # nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1
修改master的keepalived配置文件
vi /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 MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { 192.168.202.100 } track_script { chk_nginx } }
修改master脚本
# vi /usr/local/sbin/check_ng.sh #!/bin/bash #时间变量,用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` #计算nginx进程数量 n=`ps -C nginx --no-heading|wc -l` #如果进程为0,则启动nginx,并且再次检测nginx进程数量, #如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $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 fi fi
修改master脚本权限
# chmod 755 /usr/local/sbin/check_ng.sh
启动master的keepalived
# systemctl start keepalived
修改backup的keepalived配置文件
# vi /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 eno16777736 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { 192.168.202.100 } track_script { chk_nginx } }
修改backup脚本
# vi /usr/local/sbin/check_ng.sh #!/bin/bash #时间变量,用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` #计算nginx进程数量 n=`ps -C nginx --no-heading|wc -l` #如果进程为0,则启动nginx,并且再次检测nginx进程数量, #如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $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 fi fi
修改backup脚本权限
# chmod 755 /usr/local/sbin/check_ng.sh
启动backup的keepalived
# systemctl start keepalived
测试1:通过curl ip查看状态
[root@202-128 ~]# curl 192.168.202.128 Master [root@202-128 ~]# curl 192.168.202.129Backup [root@202-128 ~]# curl 192.168.202.100Master
这里显示结果Master、Backup,修改yum安装的nginx的默认index文件,
# vi /usr/share/nginx/html/index.html Master
测试2:关闭master上的nginx,结果显示nginx依然会启动是因为配置文件 /usr/local/sbin/check_ng.sh的脚本
[root@202-128 ~]# systemctl stop nginx [root@202-128 ~]# ps aux |grep nginx root 12703 0.0 0.0 48532 976 ? Ss 22:25 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 12704 0.0 0.1 48932 1928 ? S 22:25 0:00 nginx: worker process root 12709 0.0 0.0 112644 948 pts/0 R+ 22:25 0:00 grep --color=auto nginx
测试3:关闭master上的keepalived、nginx,显示backup起作用
[root@202-128 ~]# systemctl stop keepalived [root@202-128 ~]# systemctl stop nginx [root@202-128 ~]# curl 192.168.202.128curl: (7) Failed connect to 192.168.202.128:80; Connection refused [root@202-128 ~]# curl 192.168.202.129Backup [root@202-128 ~]# curl 192.168.202.100Backup
测试4:开启master的keepalived,master重新起作用,backup作冗余。
[root@202-128 ~]# systemctl start keepalived [root@202-128 ~]# curl 192.168.202.128 Master [root@202-128 ~]# curl 192.168.202.129Backup [root@202-128 ~]# curl 192.168.202.100Master