1、实验环境

两台CentOS-7.5虚拟机
web1:10.0.11.203
web2:10.0.11.204
VIP :10.0.11.210
web类型:nginx
客户端:自用笔记本(win10)

2、准备两台web服务器

1)web1网卡情况
[root@CentOS ~]#
[root@CentOS ~]# ip a
keepalived实现web高可用集群_第1张图片

2)web2网卡情况
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a
keepalived实现web高可用集群_第2张图片

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
keepalived实现web高可用集群

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
keepalived实现web高可用集群

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
keepalived实现web高可用集群_第3张图片
[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
keepalived实现web高可用集群_第4张图片
[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
keepalived实现web高可用集群_第5张图片

拔掉web1服务器的网线,用客户端通过VIP测试访问web:
C:\Users\Administrator>curl 10.0.11.210
keepalived实现web高可用集群_第6张图片

再次将web1服务器的网线接好,用客户端通过VIP测试访问web:
C:\Users\Administrator>curl 10.0.11.210
keepalived实现web高可用集群_第7张图片

4、实验结论:
利用keepalived成功实现了web服务器集群的高可用