实验环境

两台CentOS-7.5虚拟机
web1:10.0.11.203
web2:10.0.11.204
VIP :10.0.11.210
web类型:nginx
客户端:自用笔记本(win10)
nginx状态检测剧本:ck_nginx.sh

实验一、使用keepalived简朴实现web集群的高可用功效

1、准备两台web服务器

1)web1网卡情形
[root@CentOS ~]#
[root@CentOS ~]# ip a

2)web2网卡情形
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a

3)安装nginx

web1端:
[root@CentOS ~]#
[root@CentOS ~]# yum install nginx
[root@CentOS ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[root@CentOS ~]#
[root@CentOS ~]#
[root@CentOS ~]# > /usr/share/nginx/html/index.html #清空默认网页
[root@CentOS ~]# echo web1 > /usr/share/nginx/html/index.html #让网页显示web1
[root@CentOS ~]# cat /usr/share/nginx/html/index.html
web1
[root@CentOS ~]#
[root@CentOS ~]# systemctl start nginx #启动nginx
[root@CentOS ~]# systemctl enable nginx #设置nginx开机自启动
C:\Users\Administrator>curl 10.0.11.203

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install nginx
[root@CentOS-2 ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[root@CentOS-2 ~]#
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# > /usr/share/nginx/html/index.html
[root@CentOS-2 ~]# echo web2 > /usr/share/nginx/html/index.html
[root@CentOS-2 ~]# cat /usr/share/nginx/html/index.html
web2
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start nginx
[root@CentOS-2 ~]# systemctl enable nginx
C:\Users\Administrator>curl 10.0.11.204

4)安装keepalived
说明:两台web服务器都需要安装keepalived服务

web1端:
[root@CentOS ~]#
[root@CentOS ~]# yum install keepalived
[root@CentOS ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS ~]#

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install keepalived
[root@CentOS-2 ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS-2 ~]#

5)设置两台keepalived并启动

web1端:
[root@CentOS ~]#
[root@CentOS ~]# > /etc/keepalived/keepalived.conf #清空原来的设置
[root@CentOS ~]# vim /etc/keepalived/keepalived.conf #编辑设置文件,自定义keepalived设置文件
[root@CentOS ~]# cat /etc/keepalived/keepalived.conf

[root@CentOS ~]#
[root@CentOS ~]# systemctl start keepalived #启动
[root@CentOS ~]# systemctl enable keepalived #设置开机启动

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# > /etc/keepalived/keepalived.conf
[root@CentOS-2 ~]# vim /etc/keepalived/keepalived.conf
[root@CentOS-2 ~]# cat /etc/keepalived/keepalived.conf

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start keepalived #启动
[root@CentOS-2 ~]# systemctl enable keepalived #设置开机启动

3、测试接见
方式:用客户端通过VIP测试接见web

C:\Users\Administrator>curl 10.0.11.210 #小我私家笔记本通过VIP测试接见web

拔掉web1服务器的网线,用客户端通过VIP测试接见web:
C:\Users\Administrator>curl 10.0.11.210

再次将web1服务器的网线接好,用客户端通过VIP测试接见web:
C:\Users\Administrator>curl 10.0.11.210

4、实验一结论:
1)行使keepalived乐成实现了web服务器集群的高可用
2)虽然可以实现在服务器发生异常高故障的情形下web的高可用,然则遗憾的是此时无法实现针对web服务异常故障所引发的主备高可用切换;由于keepalived无法感知nginx服务的运行状态和康健状态。


实验二、使用keepalived连系nginx监控检测剧本实现加倍天真的高可用集群功效

实验环境:基于实验一的环境,以实验一的环境为基础举行
与实验一的唯一区别:需要准备nginx的康健状态检测剧本

1、编写nginx的康健检测剧本

web1端执行:
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/ck_nginx.sh #编写keepalived检测nginx康健状态的剧本

#!/bin/bash
#检查ngin历程是否存在
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}"="0" ]; then
#实验启动一次ngnx,住手5秒后再次检测
systemctl start nginx
sleep 5
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}"="0" ]; then
#若是启动没乐成,就杀掉keepave触发主备切换
systemctl stop nginx
fi
fi
[root@CentOS ~]#
[root@CentOS ~]# cat /etc/keepalived/ck_nginx.sh #查看剧本内容

[root@CentOS ~]#
[root@CentOS ~]# chmod +x /etc/keepalived/ck_nginx.sh #授予剧本可执行权限
[root@CentOS ~]#

web2端执行:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# vim /etc/keepalived/ck_nginx.sh

#!/bin/bash
#检查ngin历程是否存在
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}"="0" ]; then
#实验启动一次ngnx,住手5秒后再次检测
systemctl start nginx
sleep 5
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}"="0" ]; then
#若是启动没乐成,就杀掉keepave触发主备切换
systemctl stop nginx
fi
fi

[root@CentOS-2 ~]#
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/ck_nginx.sh

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# chmod +x /etc/keepalived/ck_nginx.sh
[root@CentOS-2 ~]#

2、编辑修改nginx的设置文件
web1端:
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/keepalived.conf #自定义设置文件

! Configuration File for keepalived

global_defs {
notification_email {br/>111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.203
virtual_router_id 55
priority 100
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root@CentOS ~]#
[root@CentOS ~]# systemctl restart keepalived #重启keepalived使设置生效
[root@CentOS ~]#

web2端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# vim /etc/keepalived/keepalived.conf #自定义设置文件

! Configuration File for keepalived

global_defs {
notification_email {br/>111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id 1
}

vrrp_script chk_nginx {
script "/etc/keepalived/ck_nginx.sh"
interval 2
weight -5
fail 3
}

vrrp_instance VI_1 {
state MASTER
interface ens32
mcast_src_ip 10.0.11.204
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
    10.0.11.210/24
}

track_script {
     chk_nginx
}

}

[root@CentOS-2 ~]#
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl restart keepalived #重启keepalived使设置生效

测试keepalived功效:
由于剧本内容里边,nginx服务在住手以后会立马自启动,以是我们模拟服务器异常关闭导致nginx服务没有办法实现自启动
[root@CentOS ~]#
[root@CentOS ~]# init 0 #关闭web1服务器

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ping 10.0.11.203 #用web2去pingweb1,发现web1已经被关闭

此时使用客户端去测试验证keepalived功效:
C:\Users\Administrator>curl 10.0.11.210 #用客户端通过VIP去接见web服务器

3、实验二结论
使用keepalived服务连系nginx康健状态检测剧本乐成实现了对web服务器集群的高可用