keepalived实现web高可用集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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网卡情况
[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:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>curl 10.0.11.210 #个人笔记本通过VIP测试访问web
技术图片

拔掉web1服务器的网线,用客户端通过VIP测试访问web:
C:UsersAdministrator>curl 10.0.11.210
技术图片

再次将web1服务器的网线接好,用客户端通过VIP测试访问web:
C:UsersAdministrator>curl 10.0.11.210
技术图片

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

以上是关于keepalived实现web高可用集群的主要内容,如果未能解决你的问题,请参考以下文章

Keepalived+Nginx+Tomcat 实现高可用Web集群(转)

Nginx+Tomcat+Keepalived实现高可用web集群

Web服务器群集——LVS-DR+Keepalived高可用集群

keepalived实现web高可用集群

LVS+keepalived 实现web高可用负载均衡集群

Keepalived+Lvs_DR模式实现Web服务的HA高可用集群