在 LAN 上使用静态 IP 在集群中运行单个 haproxy 容器,如果节点发生故障,将重新创建该容器。本质上,它类似于指向 haproxy 的 VIP,但不需要 swarm 外部的外部负载均衡器。
示例:在两个节点上创建 macvlan 网络,在节点 1 上运行容器,停止容器,然后在节点 2 上运行容器。 haproxy 容器是使用相同的静态 IP 创建的,并且可以在 LAN 上访问。
示例:将 macvlan 网络范围设置为 swarm,然后部署堆栈。容器对网络不可见。
docker-compose.yml:
version: "3.2"
networks:
vlan0:
external: true
services:
haproxy:
image: haproxy:2.3.2-alpine
container_name: haproxy
volumes:
- ./data:/usr/local/etc/haproxy:ro
environment:
- TZ=America/Los_Angeles
restart: always
networks:
vlan0:
ipv4_address: 192.168.0.201
localnet.sh(用于停止堆栈/删除网络/将网络重新创建为本地/本地运行容器的脚本):
#!/bin/bash
docker service rm haproxy_haproxy
docker-compose down
docker network rm vlan0
docker network create -o parent=eth0 --subnet 192.168.0.0/24 --gateway 192.168.0.1 --driver macvlan --scope local vlan0
docker-compose up
swarmnet.sh(用于删除容器和网络/将网络重新创建为群/作为群堆栈运行的脚本):
#!/bin/bash
docker service rm haproxy_haproxy
docker-compose down
docker network rm vlan0
docker network create -o parent=eth0 --subnet 192.168.0.0/24 --gateway 192.168.0.1 --driver macvlan --scope swarm vlan0
docker stack deploy -c docker-compose.yml haproxy
如有任何帮助,我们将不胜感激。
编辑:我已经能够使 macvlan 地址正常工作,但是 docker swarm 不遵守 ipv4_address 字段来静态化容器。我理解这样做的原因(副本等不在同一 IP 上),但在这种情况下,由于它是单个容器,因此不会发生这种情况。我发现这里讨论了这个问题:https://forums.docker.com/t/docker-swarm-1-13-static-ips-for-containers/28060/
我已经弄清楚如何绑定单个静态 IP 地址(下面有一个主要警告)。
为此,请在每台主机上使用单个 IP 范围(32 位掩码)创建一个仅配置网络。我在这里创建了一个脚本来自动生成命令。
#!/bin/bash
echo
echo VIP swarm command generator
echo
defint=$(ip route get 8.8.8.8 | head -n1 | awk '{print $5}')
hostip=$(ip addr show dev $defint | grep "inet" | awk 'NR==1{print $2}' | cut -d'/' -f 1)
hostsub=$(ip route | grep "src $hostip" | awk '{print $1}')
hostgate=$(ip route show | grep default | awk '{print $3}')
read -p "Subnet (default "$hostsub"): " sub
sub=${sub:=$hostsub}
read -p 'Gateway (default '$hostgate'): ' gate
gate=${gate:=$hostgate}
read -p 'Vip address: ' ip
last=`echo $ip | cut -d . -f 4`
begin=`echo $ip | cut -d"." -f1-3`
read -p 'Ethernet interface (default '$defint'): ' eth
eth=${eth:=$defint}
read -p 'Docker Network Name: (default vip-'$last'): ' name
name=${name:="vip-"$last}
echo -e "Build the network on each node, \033[0;33mmake sure the physical parent interface (parent=) is set properly on each node if different)\033[0m:"
echo -e " \033[44mdocker network create --config-only --subnet $sub --gateway $gate --ip-range $ip/32 -o parent=$eth $name\033[0m"
echo
echo "Then create the swarm network from any manager node:"
echo -e " \033[44mdocker network create -d macvlan --scope swarm --config-from $name swarm-$name\033[0m"
输出示例:
VIP swarm command generator
Subnet (default 192.168.0.0/24):
Gateway (default 192.168.0.1):
Vip address: 192.168.0.201
Ethernet interface (default eth0):
Docker Network Name: (default vip-201):
Build the network on each node, make sure the physical parent interface (parent=) is set properly on each node if different):
docker network create --config-only --subnet 192.168.0.0/24 --gateway 192.168.0.1 --ip-range 192.168.0.201/32 -o parent=eth0 vip-201
Then create the swarm network from any manager node:
docker network create -d macvlan --scope swarm --config-from vip-201 swarm-vip-201
因此,在每个节点上,我构建网络配置
docker网络创建 --config-only --subnet 192.168.0.0/24 --gateway 192.168.0.1 --ip-range 192.168.0.201/32 -oparent=eth0 vip-201
然后我构建群界面
docker网络创建-d macvlan --scope swarm --config-from vip-201 swarm-vip-201
并在适用的 docker-compose.yaml 文件中,添加适当的行:
networks:
swarm-vip-201:
external: true
services:
haproxy:
...
networks:
swarm-vip-201:
我的容器现在将始终在该单个静态 IP 地址上可用。如果节点发生故障,它将在具有相同 IP 的另一个节点上重新启动,实质上为 swarm 创建高可用性 VIP。
Docker swarm 尝试将多个 macvlan 绑定到同一个父接口时会抛出错误,如下所示:
"network NETNAME is already using parent interface eth0"
此处描述:
https://github.com/moby/libnetwork/issues/2384
https://github.com/moby/libnetwork/issues/1743
实际上可以在 swarm 中创建静态 macvlan IP,这是有希望的,但在修复将多个网络绑定到单个父接口的错误之前,它是非常有限的。
如果有人知道如何可靠地解决上述问题,或者有更好的方法,请发帖。
Stackuser,感谢您分享您的想法,非常好,我认为可以使用 ipvlan 驱动程序来实现