Fix Swarm networking
This commit is contained in:
parent
aa5f8055f5
commit
c0eb766339
3 changed files with 31 additions and 75 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1,4 +1,5 @@
|
||||||
# To Install
|
# To Install
|
||||||
|
# docker network create --driver overlay --attachable easyhaproxy
|
||||||
# docker stack deploy -c easyhaproxy.yml easyhaproxy
|
# docker stack deploy -c easyhaproxy.yml easyhaproxy
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -6,7 +7,7 @@ version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
haproxy:
|
haproxy:
|
||||||
image: byjg/easy-haproxy:4.3.1-rc1
|
image: byjg/easy-haproxy:4.3.1-rc2
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
- ./certs:/certs/haproxy
|
- ./certs:/certs/haproxy
|
||||||
|
|
@ -25,6 +26,12 @@ services:
|
||||||
- "80:80/tcp"
|
- "80:80/tcp"
|
||||||
- "443:443/tcp"
|
- "443:443/tcp"
|
||||||
- "1936:1936/tcp"
|
- "1936:1936/tcp"
|
||||||
|
networks:
|
||||||
|
- easyhaproxy
|
||||||
|
|
||||||
|
networks:
|
||||||
|
easyhaproxy:
|
||||||
|
external: true
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
certs_letsencrypt:
|
certs_letsencrypt:
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ class ProcessorInterface:
|
||||||
|
|
||||||
def __init__(self, filename = None):
|
def __init__(self, filename = None):
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
|
self.label = ContainerEnv.read()['lookup_label']
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -156,23 +157,38 @@ class Swarm(ProcessorInterface):
|
||||||
|
|
||||||
def inspect_network(self):
|
def inspect_network(self):
|
||||||
ha_proxy_service_name = self.client.containers.get(socket.gethostname()).name.split('.')[0]
|
ha_proxy_service_name = self.client.containers.get(socket.gethostname()).name.split('.')[0]
|
||||||
|
ha_proxy_network_id = None
|
||||||
|
swarm_ingress_id = None
|
||||||
|
|
||||||
|
# Get the HAProxy network and the ingress network
|
||||||
for endpoint in self.client.services.get(ha_proxy_service_name).attrs['Endpoint']["VirtualIPs"]:
|
for endpoint in self.client.services.get(ha_proxy_service_name).attrs['Endpoint']["VirtualIPs"]:
|
||||||
|
network_name = self.client.networks.get(endpoint["NetworkID"]).name
|
||||||
|
if swarm_ingress_id is None and network_name == 'ingress':
|
||||||
|
swarm_ingress_id = endpoint["NetworkID"]
|
||||||
|
if ha_proxy_network_id is None and network_name != 'ingress':
|
||||||
ha_proxy_network_id = endpoint["NetworkID"]
|
ha_proxy_network_id = endpoint["NetworkID"]
|
||||||
if self.client.networks.get(ha_proxy_network_id).name != 'ingress':
|
if ha_proxy_network_id is not None and swarm_ingress_id is not None:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# Check if the service is attached to the HAProxy network
|
||||||
self.parsed_object = {}
|
self.parsed_object = {}
|
||||||
for service in self.client.services.list():
|
for service in self.client.services.list():
|
||||||
|
if not any(self.label in key for key in service.attrs["Spec"]["Labels"]):
|
||||||
|
continue
|
||||||
|
|
||||||
ip_address = None
|
ip_address = None
|
||||||
|
network_list = []
|
||||||
for endpoint in service.attrs["Endpoint"]["VirtualIPs"]:
|
for endpoint in service.attrs["Endpoint"]["VirtualIPs"]:
|
||||||
if ha_proxy_network_id == endpoint["NetworkID"]:
|
if ha_proxy_network_id == endpoint["NetworkID"]:
|
||||||
ip_address = endpoint["Addr"].split("/")[0]
|
ip_address = endpoint["Addr"].split("/")[0]
|
||||||
break
|
break
|
||||||
|
elif swarm_ingress_id != endpoint["NetworkID"]:
|
||||||
|
network_list.append(endpoint["NetworkID"])
|
||||||
|
|
||||||
# add the network ha_proxy_network_id to the service object
|
# Attach the service to the HAProxy network
|
||||||
if ip_address is None:
|
if ip_address is None:
|
||||||
network_attachment = docker.types.NetworkAttachmentConfig(target=ha_proxy_network_id)
|
network_list.append(ha_proxy_network_id)
|
||||||
service.update(networks = [network_attachment])
|
service.update(networks = network_list)
|
||||||
continue # skip to the next service to give time to update the network
|
continue # skip to the next service to give time to update the network
|
||||||
|
|
||||||
self.parsed_object[ip_address] = service.attrs["Spec"]["Labels"]
|
self.parsed_object[ip_address] = service.attrs["Spec"]["Labels"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue