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网卡情况
[[email protected] ~]#
[[email protected] ~]# ip a
2)web2网卡情况
[[email protected] ~]#
[[email protected] ~]# ip a
3)安装nginx
web1端:
[[email protected] ~]#
[[email protected] ~]# yum install nginx
[[email protected] ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# > /usr/share/nginx/html/index.html #清空默认网页
[[email protected] ~]# echo web1 > /usr/share/nginx/html/index.html #让网页显示web1
[[email protected] ~]# cat /usr/share/nginx/html/index.html
web1
[[email protected] ~]#
[[email protected] ~]# systemctl start nginx #启动nginx
[[email protected] ~]# systemctl enable nginx #设置nginx开机自启动
C:\Users\Administrator>curl 10.0.11.203
web2端:
[[email protected] ~]#
[[email protected] ~]# yum install nginx
[[email protected] ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# > /usr/share/nginx/html/index.html
[[email protected] ~]# echo web2 > /usr/share/nginx/html/index.html
[[email protected] ~]# cat /usr/share/nginx/html/index.html
web2
[[email protected] ~]#
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl enable nginx
C:\Users\Administrator>curl 10.0.11.204
4)安装keepalived
说明:两台web服务器都需要安装keepalived服务
web1端:
[[email protected] ~]#
[[email protected] ~]# yum install keepalived
[[email protected] ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[[email protected] ~]#
web2端:
[[email protected] ~]#
[[email protected] ~]# yum install keepalived
[[email protected] ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[[email protected] ~]#
5)配置两台keepalived并启动
web1端:
[[email protected] ~]#
[[email protected] ~]# > /etc/keepalived/keepalived.conf #清空原来的配置
[[email protected] ~]# vim /etc/keepalived/keepalived.conf #编辑配置文件,自定义keepalived配置文件
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
[[email protected] ~]#
[[email protected] ~]# systemctl start keepalived #启动
[[email protected] ~]# systemctl enable keepalived #设置开机启动
web2端:
[[email protected] ~]#
[[email protected] ~]# > /etc/keepalived/keepalived.conf
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
[[email protected] ~]#
[[email protected] ~]# systemctl start keepalived #启动
[[email protected] ~]# 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、实验结论:
利用keepalived成功实现了web服务器集群的高可用
原文地址:https://blog.51cto.com/14783377/2486373