docker下用keepalived+Haproxy实现高可用负载均衡集群

时间:2021-10-25 03:18:04

启动keepalived后宿主机无法ping通用keepalived,报错:

[root@localhost ~]# ping 172.18.0.15
PING 172.18.0.15 (172.18.0.15) 56(84) bytes of data.
From 172.18.0.1 icmp_seq=1 Destination Host Unreachable
From 172.18.0.1 icmp_seq=2 Destination Host Unreachable
From 172.18.0.1 icmp_seq=3 Destination Host Unreachable
From 172.18.0.1 icmp_seq=4 Destination Host Unreachable
From 172.18.0.1 icmp_seq=5 Destination Host Unreachable
From 172.18.0.1 icmp_seq=6 Destination Host Unreachable

解决方案:

大多数都是我把配置文件没写对,重写配置文件,重启服务;

这里检查能否ping通,需要看服务内你的配置文件写入的ip有没有出现在docker的网卡上,具体方法是:

进入到docker服务内,不是在宿主机上哦,查看配置文件
root@8351443065ea:/etc/keepalived# cat keepalived.conf
vrrp_instance  VI_1 {
    state  MASTER
    interface  eth0
    virtual_router_id  100
    priority  100
    advert_int  1
    authentication {
        auth_type  PASS
        auth_pass  123456
    }
    virtual_ipaddress {
        172.18.0.201
    }
}
如果配置文件信息都是正确的,通过ip a命令会显示如当前docker服务有的网卡
root@ec688a676a07:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default ;
 
很明显没有我配置文件中的172.18.0.201ip地址,代表配置文件未生效,大多数配置文件错误
 
停掉keepalived服务重新修改编辑后重启
root@ec688a676a07:/# service keepalived stop 
[....] Stopping keepalived: keepalived. ok 
重启服务
root@ec688a676a07:/# service keepalived start
[....] Starting keepalived: keepalived. ok 

再次查看docker服务的ip
root@ec688a676a07:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.18.0.7/16 scope global eth0
       valid_lft forever preferred_lft forever
这里出现了我的配置文件ip地址
    inet 172.18.0.201/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe12:7/64 scope link 
       valid_lft forever preferred_lft forever

在服务内直接ping这个ip,发现能ping通,切换宿主机,也能ping通,解决问题
[root@localhost ~]# ping 172.18.0.201
PING 172.18.0.201 (172.18.0.201) 56(84) bytes of data.
64 bytes from 172.18.0.201: icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from 172.18.0.201: icmp_seq=2 ttl=64 time=0.064 ms