keepalived ============================= 架构 ----------------------------- 配置容器的静态IP ----------------------------- .. code-block:: bash vim /etc/network/interfaces 原始文件内容: .. code-block:: text auto eth0 iface eth0 inet dhcp hostname $(hostname) 需要修改成: .. code-block:: text auto eth0 hostname $(hostname) iface eth0 inet static address 10.196.126.12 netmask 255.255.255.0 gateway 10.196.126.1 dns-nameservers 8.8.8.8 8.8.4.4 .. code-block:: bash /etc/init.d/networking restart # 或者 # 重启lxc容器。 nginx-1 ----------------------------- IP: 10.196.126.12 .. code-block:: apk add nginx service nginx start apk add keepalived mkdir -p /etc/keepalived vi /etc/keepalived/keepalived.conf .. code-block:: vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass my_secret_password } virtual_ipaddress { 10.196.126.14 } } 解释: .. code-block:: vrrp_instance VI_1 { state MASTER # 指定此节点为主节点 interface eth0 # 监控的网络接口 virtual_router_id 51 # VRID(虚拟路由器ID),在所有节点上必须相同 priority 100 # 优先级,数字越大,优先级越高 advert_int 1 # 通告间隔时间(秒),各节点间同步信息的时间间隔 authentication { auth_type PASS # 认证类型 auth_pass my_secret_password # 认证密码,确保所有节点一致 } virtual_ipaddress { 10.196.126.14 # 虚拟IP地址(VIP),要在节点间切换的IP } } .. code-block:: service keepalived start nginx-2 ----------------------------- IP: 10.196.126.13 .. code-block:: apk add nginx service nginx start apk add keepalived mkdir -p /etc/keepalived vi /etc/keepalived/keepalived.conf .. code-block:: vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass my_secret_password } virtual_ipaddress { 10.196.126.14 } } 解释: .. code-block:: vrrp_instance VI_1 { state BACKUP # 指定此节点为备节点 interface eth0 # 监控的网络接口 virtual_router_id 51 # VRID(虚拟路由器ID),在所有节点上必须相同 priority 90 # 优先级,数字越大,优先级越高 advert_int 1 # 通告间隔时间(秒),各节点间同步信息的时间间隔 authentication { auth_type PASS # 认证类型 auth_pass my_secret_password # 认证密码,确保所有节点一致 } virtual_ipaddress { 10.196.126.14 # 虚拟IP地址(VIP),要在节点间切换的IP } } .. code-block:: service keepalived start