virtual_ipaddress 绑定的ip, 下面简写vipnginx
安装keepalive
master(test-a:134)与backup(centos0: 129)安装keepalivedvim
[root@test-a ~]# yum install -y keepalived [root@centos0 ~]# yum install -y keepalived # 使用yum安装Nginx若是提示找不到对应的安装包,须要安装epel源 [root@centos0 ~]# yum install -y nginx 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.neusoft.edu.cn * updates: mirrors.cqu.edu.cn 没有可用软件包 nginx。 错误:无须任何处理 [root@centos0 ~]# yum install -y nginx # 再安装
master配置启动服务后端
# 先关闭防火墙 [root@test-a ~]# systemctl stop firewalld.service [root@test-a ~]# setenforce 0 # 编辑配置文件 [root@test-a ~]# vim /etc/keepalived/keepalived.conf [root@test-a ~]# cat /etc/keepalived/keepalived.conf global_defs { 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 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass test111 } virtual_ipaddress { 192.168.77.100 } track_script { chk_nginx } } [root@test-a ~]# vim /usr/local/sbin/check_ng.sh # 编写对应的检测脚本 [root@test-a ~]# cat /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 echo "$d nginx check begin" if [ $n -eq "0" ]; then /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 echo "$d nginx restart ok" >> /var/log/check_ng.log fi [root@test-a ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@test-a ~]# systemctl start keepalived.service [root@test-a ~]# ps aux|grep keep root 2841 0.0 0.1 119248 1396 ? Ss 15:13 0:00 /usr/sbin/keepa lived -D root 2842 0.0 0.3 123448 3136 ? S 15:13 0:00 /usr/sbin/keepa lived -D root 2843 0.1 0.2 123516 2532 ? S 15:13 0:00 /usr/sbin/keepa lived -D root 2861 0.0 0.0 112704 972 pts/0 R+ 15:14 0:00 grep --color=au to keep [root@test-a ~]# ps aux|grep nginx root 1688 0.0 0.1 46548 1268 ? Ss 15:00 0:00 nginx: master p rocess /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1694 0.0 0.3 49036 3900 ? S 15:00 0:00 nginx: worker p rocess nobody 1695 0.0 0.3 49036 3900 ? S 15:00 0:00 nginx: worker p rocess root 2911 0.0 0.0 112704 976 pts/0 R+ 15:14 0:00 grep --color=au to nginx [root@test-a ~]# ip add # 查看绑定的vip地址,ifconfig看不见 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stat e UP qlen 1000 link/ether 00:0c:29:b9:56:99 brd ff:ff:ff:ff:ff:ff inet 192.168.77.134/24 brd 192.168.77.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.77.100/32 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.77.139/24 brd 192.168.77.255 scope global secondary eno16777736 :t valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feb9:5699/64 scope link valid_lft forever preferred_lft forever [root@test-a ~]# /etc/init.d/nginx stop # nginx中止后,检测脚本会从新启动 Stopping nginx (via systemctl): [ OK ] [root@test-a ~]# ps aux|grep keep root 4508 0.0 0.1 119248 1396 ? Ss 15:21 0:00 /usr/sbin/keepa lived -D root 4509 0.0 0.3 123448 3120 ? S 15:21 0:00 /usr/sbin/keepa lived -D root 4510 0.0 0.2 123516 2652 ? S 15:21 0:00 /usr/sbin/keepa lived -D root 5402 0.0 0.0 112704 976 pts/0 R+ 15:25 0:00 grep --color=au to keep [root@test-a ~]# ps aux|grep nginx root 5388 0.0 0.1 46548 1264 ? Ss 15:25 0:00 nginx: master p rocess /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 5392 0.0 0.3 49036 3900 ? S 15:25 0:00 nginx: worker p rocess nobody 5393 0.0 0.3 49036 3900 ? S 15:25 0:00 nginx: worker p rocess root 5428 0.0 0.0 112704 976 pts/0 R+ 15:25 0:00 grep --color=au to nginx [root@test-a ~]#
backup配置启动服务centos
[root@centos0 ~]# setenforce 0 [root@centos0 ~]# systemctl stop firewalld [root@centos0 ~]# vim /usr/local/sbin/check_ng.sh [root@centos0 ~]# cat /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 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 [root@centos0 ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@centos0 ~]# vim /etc/keepalived/keepalived.conf [root@centos0 ~]# cat /etc/keepalived/keepalived.conf global_defs { 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 1 priority 90 advert_int 1 authentication { auth_type PASS auth_pass test111 } virtual_ipaddress { 192.168.77.100 } track_script { chk_nginx } } [root@centos0 ~]# ps aux | grep keep root 4219 0.0 0.0 112656 988 pts/0 R+ 15:46 0:00 grep --color=auto keep [root@centos0 ~]# ps aux | grep nginx root 4221 0.0 0.0 112656 988 pts/0 R+ 15:46 0:00 grep --color=auto nginx # 能够看到,keepalived 和 nginx 都没启动起来,这时若是启动keepalived,nginx也启动起来,说明配置成功 [root@centos0 ~]# systemctl start keepalived [root@centos0 ~]# ps aux | grep keep root 4230 0.0 0.1 119100 1376 ? Ss 15:48 0:00 /usr/sbin/keepalived -D root 4231 0.0 0.3 123268 3096 ? S 15:48 0:00 /usr/sbin/keepalived -D root 4232 0.0 0.2 123268 2608 ? S 15:48 0:00 /usr/sbin/keepalived -D root 4262 0.0 0.0 112656 988 pts/0 R+ 15:48 0:00 grep --color=auto keep [root@centos0 ~]# ps aux | grep nginx # nginx也启动起来啦 root 4256 0.0 0.2 125388 2112 ? Ss 15:48 0:00 nginx: master process /usr/sbin/nginx nginx 4257 0.0 0.3 125776 3132 ? S 15:48 0:00 nginx: worker process root 4282 0.0 0.0 112656 992 pts/0 R+ 15:48 0:00 grep --color=auto nginx
浏览器访问测试浏览器
访问master:
bash
访问backup:
服务器
访问绑定的vip:
负载均衡
停掉master上的keepalived,再进行访问测试,发现已经切到backup了oop
[root@test-a ~]# systemctl stop keepalived.service