centos7 keepalived+高可用服务搭建

master服务器ip地址:192.168.43.238
slave服务器ip地址:192.168.43.68
realserver服务器ip地址 :192.168.43.100
realserver服务器ip地址 :192.168.43.128

步骤解析

前期准备

主机yum源更改

下载软件时报错 ,显示找不到

centos7 keepalived+高可用服务搭建

这里我们把yum源换成阿里的源
PS:如果没有wget命令 需要 yum install -y wget 下载

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup  #先做备份工作
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo  #将网页文件下载至指定目录 
yum clean all  #清理缓存并生成新的缓存
yum makecache

更换好后发现下载速度滋溜提升了centos7 keepalived+高可用服务搭建

调试

[root@localhost ~]# rpm -q ipvsadm 
ipvsadm-1.27-8.el7.x86_64
[root@localhost ~]# ipvsadm -A -t 192.168.43.60:80 -s rr


[root@localhost ~]# ipvsadm -a -t 192.168.43.60:80 -r 192.168.43.68:80
[root@localhost ~]# ipvsadm -a -t 192.168.43.60:80 -r 192.168.43.100:80
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.43.60:80 rr
  -> 192.168.43.68:80             Route   1      0          0         
  -> 192.168.43.100:80            Route   1      0          0   

server端和backup端都需要做的事


[root@localhost html]# yum install -y httpd
[root@localhost html]# systemctl start httpd
[root@localhost html]# cd /var/www/html/
[root@localhost html]# echo web2 > index.html #server端可改为web1
[root@localhost html]# curl localhost
web2


yum install -y keepalived

cd /etc/keepalived


编辑配置文件

[root@localhost keepalived]# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
 #  vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
	192.168.43.101 80
    }
}

virtual_server 192.168.43.101 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
 #   persistence_timeout 50
    protocol TCP

    real_server 192.168.43.100 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            delay_before_retry 3
        }
    }
}
  real_server 192.168.43.128 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            delay_before_retry 3
        }
    }
}

PS:backup端到上面的步骤都和主一致
但是配置文件中
state MASTER 需要替换为 state BACKUP
priority 100 需要替换为 priority 50 #(小于主的优先级)

接着开启服务
会发现服务起来一下子了,接着马上dead了
开始实在是不解,结合以前经验和百度都没有效果

[root@localhost keepalived]# systemctl status keepalived.service -l
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Dec 18 18:45:07 localhost.localdomain Keepalived_healthcheckers[13519]: Unknown keyword '}'
Dec 18 18:45:07 localhost.localdomain Keepalived_vrrp[13520]: VRRP_Instance(VI_1) Unknown interface !
Dec 18 18:45:07 localhost.localdomain Keepalived[13518]: Starting VRRP child process, pid=13520
Dec 18 18:45:08 localhost.localdomain Keepalived_healthcheckers[13519]: Activating healthchecker for service [192.168.43.101]:80
Dec 18 18:45:08 localhost.localdomain Keepalived_vrrp[13520]: Stopped
Dec 18 18:45:08 localhost.localdomain Keepalived[13518]: Keepalived_vrrp exited with permanent error CONFIG. Terminating
Dec 18 18:45:08 localhost.localdomain Keepalived[13518]: Stopping
Dec 18 18:45:08 localhost.localdomain Keepalived_healthcheckers[13519]: Removing service [192.168.43.100]:80 from VS [192.168.43.101]:80
Dec 18 18:45:08 localhost.localdomain Keepalived_healthcheckers[13519]: Stopped
Dec 18 18:45:13 localhost.localdomain Keepalived[13518]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2

怀疑是健康检查导致的,把连接超时时间增大了
centos7 keepalived+高可用服务搭建

发现还是不管用

后来注意到 文件中

interface eth0    vrrp 实例绑定的接口,用于发送 VRRP 包。
	默认是eth0 	  但是实际上我的网卡设备名称为ens33!

centos7 keepalived+高可用服务搭建
用ifconfig命令查看自己的网卡名称
现在重启服务


systemctl start httpd
systemctl status httpd
systemctl restart keepalived.service
systemctl status keepalived.service -l
...

111 终于有反应了

[root@localhost keepalived]# ipvsadm -ln #虚拟ip添加成功了
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.43.101:80 rr
You have mail in /var/spool/mail/root

非常之喜人,我们继续配置

centos7 keepalived+高可用服务搭建

在backup端配置
首先通过scp命令 把master端的文件直接拉取过来

[root@localhost html]# scp 192.168.43.238:/etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf
root@192.168.43.238's password: 
keepalived.conf ```

再按照之前说的修改下配置文件

realserver端也需要下载 httpd软件
yum install -y httpd
systemctl start httpd

到这里我我们就搭建完毕了

测试:

  1. 高可用测试:停止 master 上的 keepalived 服务,看 backup 是否接管。
  2. 负载均衡测试:访问 http://192.168.43.101,看到页面在两个 realserver 上切换表示成功!
    你也可以通过 ipvsadm -Lnc 查看详细连接情况!
  3. 故障切换测试:任意关闭 realserver 上的 httpd 服务,Keepalived 监控模块是否能及时发现,
    然后屏蔽故障节点,同时将服务转移到正常节点来执行。
上一篇:Springboot——2021-12-18


下一篇:2021-12-18每日一题练习