mycat 容器中安装 keepalived
- 假设为双 mycat
keeaplived 配置
看下 docker 的网络
> docker network ls
NETWORK ID NAME DRIVER SCOPE
25fed3e4d30a bridge bridge local
7b6fce5125a3 docker_gwbridge bridge local
36508b1e58ae host host local
dd6163d8125d none null local
看下 docker_gwbridge 的 ip
ip addr
-- other
4: docker_gwbridge: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:3a:55:fc:08 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker_gwbridge
valid_lft forever preferred_lft forever
配置
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
# docker 的桥接网络 docker_gwbridge
interface docker_gwbridge
# 主备组可以保持一致
virtual_router_id 51
priority 100
# nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
# docker_gwbridge 同网段 172.18.0.1 的虚拟ip
172.18.0.200
}
}
- 启动 keepalive
宿主机
- swarm 网络内的虚拟 ip 无法被外网访问
- 所以要把容器中的虚拟ip映射到宿主机,由宿主机负责接收外网请求,转发给虚拟ip
可以用负载均衡组件进行请求转发
keepalived 方式
- 配置
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface docker_gwbridge
# 主备保持一致
virtual_router_id 51
priority 100
# nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
# 面向外网的虚拟 ip
192.168.1.212
}
}
# 转发请求给 mycat 数据端口
virtual_server 192.168.1.212 8066 {
# 心跳检测 s
delay_loop 3
# 轮询
lb_algo rr
# NAT 模式
lb_kind NAT
# 超时时间
persistence timeout 50
protocol TCP
# 转发给容器的虚拟 ip
real_server 172.18.0.200 8066 {
weight 1
}
}
# 转发请求给 mycat 控制端口
virtual_server 192.168.1.212 9066 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence timeout 50
protocol TCP
real_server 172.18.0.200 9066 {
weight 1
}
}