keepalived高可用的部署方案

  • 需要使用主机的host网络和privileged权限

docker配置keepalived详细方法

  • docker-compose.yaml 文件
version: "3.9"
services:
  keepm:
    image: osixia/keepalived:2.0.20
    restart: always
    network_mode: host
    container_name: keepm
    volumes:
      - "./keepalived.conf:/usr/local/etc/keepalived/keepalived.conf" 
    privileged: true
  • 主keepalived.conf
global_defs {
  default_interface ens33
}
vrrp_instance VI_1 {
  interface ens33
  state MASTER
  virtual_router_id 60
  priority 200
  advert_int 2  
  unicast_peer {
    192.168.1.20
  }
  virtual_ipaddress { 
    192.168.1.90
  }
  authentication { 
    auth_type PASS
    auth_pass d0cker
  }
  notify "/container/service/keepalived/assets/notify.sh"
}
  • 备keepalived.conf
global_defs {
  default_interface ens33
}
vrrp_instance VI_1 {
  interface ens33
  state MASTER
  virtual_router_id 60
  priority 200
  advert_int 2  
  unicast_peer {
    192.168.1.10
  }
  virtual_ipaddress { 
    192.168.1.90
  }
  authentication { 
    auth_type PASS
    auth_pass d0cker
  }
  notify "/container/service/keepalived/assets/notify.sh"
}

iptables打开vrrp允许

#打开防火墙允许通知
iptables -A INPUT -p vrrp -j ACCEPT  
#或者:
iptables -A INPUT -m pkttype --pkt-type multicast -j ACCEPT