keepalived实现web高可用集群

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网卡情况
[ ~]#
[ ~]# ip a
keepalived实现web高可用集群

2)web2网卡情况
[ ~]#
[ ~]# ip a
keepalived实现web高可用集群

3)安装nginx

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

web2端:
[ ~]#
[ ~]# yum install nginx
[ ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[ ~]#
[ ~]#
[ ~]# > /usr/share/nginx/html/index.html
[ ~]# echo web2 > /usr/share/nginx/html/index.html
[ ~]# cat /usr/share/nginx/html/index.html
web2
[ ~]#
[ ~]# systemctl start nginx
[ ~]# systemctl enable nginx
C:\Users\Administrator>curl 10.0.11.204
keepalived实现web高可用集群

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

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

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

5)配置两台keepalived并启动

web1端:
[ ~]#
[ ~]# > /etc/keepalived/keepalived.conf #清空原来的配置
[ ~]# vim /etc/keepalived/keepalived.conf #编辑配置文件,自定义keepalived配置文件
[ ~]# cat /etc/keepalived/keepalived.conf
keepalived实现web高可用集群
[ ~]#
[ ~]# systemctl start keepalived #启动
[ ~]# systemctl enable keepalived #设置开机启动

web2端:
[ ~]#
[ ~]# > /etc/keepalived/keepalived.conf
[ ~]# vim /etc/keepalived/keepalived.conf
[ ~]# cat /etc/keepalived/keepalived.conf
keepalived实现web高可用集群
[ ~]#
[ ~]# systemctl start keepalived #启动
[ ~]# systemctl enable keepalived #设置开机启动

3、测试访问
方法:用客户端通过VIP测试访问web

C:\Users\Administrator>curl 10.0.11.210 #个人笔记本通过VIP测试访问web
keepalived实现web高可用集群

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

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

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