mycat 容器中安装 keepalived

  • 假设为双 mycat

keeaplived 配置

看下 docker 的网络

  1. > docker network ls
  2. NETWORK ID NAME DRIVER SCOPE
  3. 25fed3e4d30a bridge bridge local
  4. 7b6fce5125a3 docker_gwbridge bridge local
  5. 36508b1e58ae host host local
  6. dd6163d8125d none null local

看下 docker_gwbridge 的 ip

ip addr

  1. -- other
  2. 4: docker_gwbridge: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  3. link/ether 02:42:3a:55:fc:08 brd ff:ff:ff:ff:ff:ff
  4. inet 172.18.0.1/16 brd 172.18.255.255 scope global docker_gwbridge
  5. valid_lft forever preferred_lft forever

配置

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived.conf

  1. vrrp_instance VI_1 {
  2. state MASTER
  3. # docker 的桥接网络 docker_gwbridge
  4. interface docker_gwbridge
  5. # 主备组可以保持一致
  6. virtual_router_id 51
  7. priority 100
  8. # nopreempt
  9. advert_int 1
  10. authentication {
  11. auth_type PASS
  12. auth_pass 123456
  13. }
  14. virtual_ipaddress {
  15. # docker_gwbridge 同网段 172.18.0.1 的虚拟ip
  16. 172.18.0.200
  17. }
  18. }
  • 启动 keepalive

宿主机

  • swarm 网络内的虚拟 ip 无法被外网访问
    • 所以要把容器中的虚拟ip映射到宿主机,由宿主机负责接收外网请求,转发给虚拟ip

可以用负载均衡组件进行请求转发

keepalived 方式

  • 配置

    mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived.conf

  1. vrrp_instance VI_1 {
  2. state MASTER
  3. interface docker_gwbridge
  4. # 主备保持一致
  5. virtual_router_id 51
  6. priority 100
  7. # nopreempt
  8. advert_int 1
  9. authentication {
  10. auth_type PASS
  11. auth_pass 123456
  12. }
  13. virtual_ipaddress {
  14. # 面向外网的虚拟 ip
  15. 192.168.1.212
  16. }
  17. }
  18. # 转发请求给 mycat 数据端口
  19. virtual_server 192.168.1.212 8066 {
  20. # 心跳检测 s
  21. delay_loop 3
  22. # 轮询
  23. lb_algo rr
  24. # NAT 模式
  25. lb_kind NAT
  26. # 超时时间
  27. persistence timeout 50
  28. protocol TCP
  29. # 转发给容器的虚拟 ip
  30. real_server 172.18.0.200 8066 {
  31. weight 1
  32. }
  33. }
  34. # 转发请求给 mycat 控制端口
  35. virtual_server 192.168.1.212 9066 {
  36. delay_loop 3
  37. lb_algo rr
  38. lb_kind NAT
  39. persistence timeout 50
  40. protocol TCP
  41. real_server 172.18.0.200 9066 {
  42. weight 1
  43. }
  44. }

启动 keepalive