Keepalived实现廉价HA

最近在dockerize各种home lab上的服务,其中遇到的一个问题就是如何实现HA。最方便的实现方式是打开eBay.com搜索F5 Big IP 购买硬件Load Balancer。最廉价的方式是多个设备间跑VRRP协议实现自动Fail over切换。Keepalived 就是一个精细生活VRRP的软件实现。

具体用例为两台Docker node上各跑了一个container运行unbound提供recursive DNS服务。任意一台机器下线(不管是container下线或是node下线)都由另一台机器在同一个IP地址下继续提供服务。切换期间网络不不会中断。

既然已经有了两台运转正常的Docker node,显然Keepalived跑在Container里是最经济且便于管理的。这里用到的docker image是 https://github.com/osixia/docker-keepalived

需要注意的是两台机器的sysctl需要设置 net.ipv4.ip_nonlocal_bind=1 在Ubuntu上可以通过修改 /etc/sysctl.conf 并运行 sysctl -p /etc/sysctl.conf 实现。在RancherOS上需要修改cloud-config.yml 具体参考官方文档

并且这两个container需要跑在host network上且赋予 CAP_NET_ADMIN (--cap-add NET_ADMIN

以下是用docker-composer 和直接运行docker run的两种配置。

# docker-composer.yml

keepalived:
     container_name: keepalived
     image: arcts/keepalived:latest
     environment:
       - KEEPALIVED_AUTOCONF=true
       - KEEPALIVED_STATE=MASTER
       - KEEPALIVED_INTERFACE=eth0
       - KEEPALIVED_VIRTUAL_ROUTER_ID=2
       - KEEPALIVED_UNICAST_SRC_IP=10.2.1.10
       - KEEPALIVED_UNICAST_PEER_0=10.2.1.11
       - KEEPALIVED_TRACK_INTERFACE_1=eth0
       - KEEPALIVED_VIRTUAL_IPADDRESS_1="10.2.1.12/24 dev eth0"
     network_mode: "host"
     cap_add:
       - NET_ADMIN
# docker run command
docker run -d --name keepalived --net=host --cap-add NET_ADMIN \
-e KEEPALIVED_AUTOCONF=true               	\
-e KEEPALIVED_STATE=BACKUP           		\
-e KEEPALIVED_INTERFACE=eth0                \
-e KEEPALIVED_VIRTUAL_ROUTER_ID=2           \
-e KEEPALIVED_UNICAST_SRC_IP=10.2.1.11    	\
-e KEEPALIVED_UNICAST_PEER_0=10.2.1.10     \
-e KEEPALIVED_TRACK_INTERFACE_1=eth0        \
-e KEEPALIVED_VIRTUAL_IPADDRESS_1="10.2.1.12/24 dev eth0" \
arcts/keepalived

至此 IP地址 10.2.1.12 就由 10.2.1.10 为master ,10.2.1.10下线 则10.2.1.11 自动由backup转为master继续提供服务

Leave a Reply

Your email address will not be published. Required fields are marked *