准备工作
分别在主从上安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭主从上的防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置主机
查看主机ip
[root@lynn-04 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.130.116 netmask 255.255.255.0 broadcast 192.168.130.255inet6 fe80::181c:4847:3f62:dc4c prefixlen 64 scopeid 0x20<link>ether 00:0c:29:60:ed:4a txqueuelen 1000 (Ethernet)RX packets 11701 bytes 10394449 (9.9 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 13559 bytes 972155 (949.3 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 86 bytes 6720 (6.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 86 bytes 6720 (6.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
清空主上keepalived配置文件/etc/keepalived/keepalived.conf
[root@lynn-04 ~]# > /etc/keepalived/keepalived.conf
编辑主上配置文件/etc/keepalived/keepalived.conf
[root@lynn-04 ~]# vim /etc/keepalived/keepalived.conf
加入以下内容:
global_defs {notification_email {aming@aminglinux.com}notification_email_from root@aminglinux.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL
}
vrrp_script chk_nginx {script "/usr/local/sbin/check_ng.sh"interval 3
}
vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass aminglinux>com}virtual_ipaddress {192.168.130.100}track_script {chk_nginx}
}
定义主上监控脚本/usr/local/sbin/check_ng.sh
[root@lynn-04 ~]# vim /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无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then/etc/init.d/nginx startn2=`ps -C nginx --no-heading|wc -l`if [ $n2 -eq "0" ]; thenecho "$d nginx down,keepalived will stop" >> /var/log/check_ng.logsystemctl stop keepalivedfi
fi
更改主上/usr/local/sbin/check_ng.sh权限
[root@lynn-04 ~]# chmod 755 /usr/local/sbin/check_ng.sh
启动主上keepalived
[root@lynn-04 ~]# systemctl start keepalived
[root@lynn-04 ~]# ps aux |grep keepalived
root 1741 0.0 0.1 120740 1404 ? Ss 10:46 0:00 /usr/sbin/keepalived -D
root 1742 0.0 0.3 127476 3260 ? S 10:46 0:00 /usr/sbin/keepalived -D
root 1745 0.0 0.3 131780 3116 ? S 10:46 0:00 /usr/sbin/keepalived -D
root 1799 0.0 0.0 112676 988 pts/0 S+ 10:47 0:00 grep --color=auto keepalived
为了区分主从的nginx需要把nginx的默认页做个标记
[root@lynn-04 ~]# echo master > /data/wwwroot/default/index.html
在另一台机器使用从ip访问
得到下面结果
[root@lynn-02 ~]# curl 192.168.130.116
master
配置从机
查看从机ip
[root@lynn-06 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.130.128 netmask 255.255.255.0 broadcast 192.168.130.255inet6 fe80::fef6:fd37:2682:fabc prefixlen 64 scopeid 0x20<link>ether 00:0c:29:af:03:e8 txqueuelen 1000 (Ethernet)RX packets 45666 bytes 48434560 (46.1 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 14753 bytes 1287938 (1.2 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 80 bytes 6256 (6.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 80 bytes 6256 (6.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
清空从上配置文件/etc/keepalived/keepalived.conf
[root@lynn-06 ~]# > /etc/keepalived/keepalived.conf
编辑从上配置文件/etc/keepalived/keepalived.conf
[root@lynn-06 ~]# vim /etc/keepalived/keepalived.conf
加入下面内容
global_defs {notification_email {aming@aminglinux.com}notification_email_from root@aminglinux.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL
}
vrrp_script chk_nginx {script "/usr/local/sbin/check_ng.sh"interval 3
}
vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 90advert_int 1authentication {auth_type PASSauth_pass aminglinux>com}virtual_ipaddress {192.168.130.100}track_script {chk_nginx}
}
定义从上监控脚本/usr/local/sbin/check_ng.sh
[root@lynn-06 ~]# vim /usr/local/sbin/check_ng.sh
加入下面内容
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; thensystemctl start nginxn2=`ps -C nginx --no-heading|wc -l`if [ $n2 -eq "0" ]; thenecho "$d nginx down,keepalived will stop" >> /var/log/check_ng.logsystemctl stop keepalivedfi
fi
更改从上/usr/local/sbin/check_ng.sh权限
[root@lynn-06 ~]# chmod 755 /usr/local/sbin/check_ng.sh
启动从上keepalived
[root@lynn-06 ~]# systemctl start keepalived
[root@lynn-06 ~]# ps aux |grep keepalived
root 3589 0.0 0.1 120740 1400 ? Ss 11:03 0:00 /usr/sbin/keepalived -D
root 3590 0.0 0.3 127476 3260 ? S 11:03 0:00 /usr/sbin/keepalived -D
root 3591 0.0 0.3 131780 3124 ? S 11:03 0:00 /usr/sbin/keepalived -D
root 3680 0.0 0.0 112676 988 pts/0 S+ 11:03 0:00 grep --color=auto keepalived
为了区分主从的nginx需要把nginx的默认页做个标记
[root@lynn-06 ~]# echo backup > /usr/share/nginx/html/index.html
在另一台机器使用从ip访问
得到下面结果
[root@lynn-02 ~]# curl 192.168.130.128
backup
测试高可用
首先在主从keepalived都开启的情况下查看得到vip在主机上 使用另一台机器通过vip访问得到master
然后在主机keepalived关闭的情况下查看得到vip在从机上 使用另一台机器通过vip访问得到backup
再次开启主机的keepalived情况下查看得到vip又回到主机上 使用另一台机器通过vip访问得到master
转载于:https://blog.51cto.com/10963213/2095995