目标
在集群中运行一个haproxy容器,在我的LAN上使用静态IP,如果节点失败,将重新创建它。本质上,它将类似于一个VIP指向haproxy,但不需要外部负载均衡器以外的蜂群。
什么起作用了?当地的macvlan
示例:在两个节点上创建macvlan网络,在node1上运行容器,停止容器,然后在node2上运行容器。haproxy容器是用相同的静态IP创建的,并且可以在LAN上访问。
不起作用的东西-大群
示例:将macvlan网络作用域设置为群集,然后部署堆栈。容器在网络中不可见。
示例中使用的文件
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.201localnet.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 upswarmnet.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 IP的单个容器?
任何帮助都是非常感谢的。
编辑:我已经能够使macvlan地址工作,但停靠群不服从ipv4_address字段来静态容器。我理解这种情况的原因(副本等不是在同一个IP上),但在这种情况下,由于它是一个容器,所以不会发生这种情况。我发现这里讨论过这个问题:https://forums.docker.com/t/docker-swarm-1-13-static-ips-for-containers/28060/
发布于 2020-12-12 19:05:30
我已经知道了如何绑定一个静态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因此,在每个节点上,我构建了网络配置。
码头网络创建-配置专用-子网192.168.0.0/24 -网关192.168.0.1 -ip-范围192.168.0.201/32 -o parent=eth0 vip-201
然后我构建了蜂群接口。
码头网络创建-d macvlan范围群配置从vip-201群vip 201创建
并在适用的docker-compose.yaml文件中添加适当的行:
networks:
swarm-vip-201:
external: true
services:
haproxy:
...
networks:
swarm-vip-201:结果
我的容器现在总是在那个单一的静态IP地址上可用。如果节点失败,它将在具有相同IP的另一个节点上重新启动,实质上为群集创建一个高可用性的VIP。
警告(群麦夫兰虫?)
Docker群会抛出错误,试图将多个macvlan绑定到同一个父接口,如下所示:
"network NETNAME is already using parent interface eth0"在此介绍:
https://github.com/moby/libnetwork/issues/2384
https://github.com/moby/libnetwork/issues/1743
结论
实际上,在集群中创建静态macvlan IP是可能的,但在通过将多个网络绑定到一个单亲接口来修复漏洞之前,它是非常有限的。
如果有人知道如何可靠地解决上述问题,或有更好的方法,请张贴。
https://stackoverflow.com/questions/65229715
复制相似问题