1
0
Fork 0

Issue #33 - handle networks

This commit is contained in:
Joao Gilberto 2022-11-10 17:43:59 -03:00
parent 9535413a10
commit 607470a122
3 changed files with 34 additions and 6 deletions

View file

@ -5,6 +5,7 @@
This method will use a docker standalone installation to discover the containers and configure the HAProxy. This method will use a docker standalone installation to discover the containers and configure the HAProxy.
The only requirement is that containers and EasyHAProxy must be in the same docker network. The only requirement is that containers and EasyHAProxy must be in the same docker network.
If not, EasyHAProxy will connect the container with the EasyHAProxy network.
e.g.: e.g.:

View file

@ -3,9 +3,10 @@
## Setup Docker EasyHAProxy ## Setup Docker EasyHAProxy
This method will use a docker swarm installation to discover the containers and configure the HAProxy. This method will use a docker swarm installation to discover the containers and configure the HAProxy.
The advantage of this method is that you can discover containers in other nodes from the cluster. The advantage of this method is that you can discover containers in other nodes from the cluster.
The only requirement is that containers and EasyHAProxy must be in the same docker swarm network. The only requirement is that containers and EasyHAProxy must be in the same docker swarm network.
If not, EasyHAProxy will connect the service with the EasyHAProxy service network.
e.g.: e.g.:

View file

@ -6,6 +6,7 @@ import os
import json import json
import base64 import base64
import docker import docker
import socket
from kubernetes import client, config from kubernetes import client, config
from kubernetes.client.rest import ApiException from kubernetes.client.rest import ApiException
@ -127,11 +128,17 @@ class Docker(ProcessorInterface):
super().__init__() super().__init__()
def inspect_network(self): def inspect_network(self):
ha_proxy_network_name = next(iter(self.client.containers.get(socket.gethostname()).attrs["NetworkSettings"]["Networks"]))
ha_proxy_network = self.client.networks.get(ha_proxy_network_name)
self.parsed_object = {} self.parsed_object = {}
for container in self.client.containers.list(): for container in self.client.containers.list():
ip_address = container.attrs["NetworkSettings"]["IPAddress"] # Issue 32 - Docker container cannot connect to containers in different network.
if not ip_address: if ha_proxy_network_name not in container.attrs["NetworkSettings"]["Networks"].keys():
ip_address = list(container.attrs["NetworkSettings"]["Networks"].values())[0]["IPAddress"] ha_proxy_network.connect(container.name)
container = self.client.containers.get(container.name) # refresh object
ip_address = container.attrs["NetworkSettings"]["Networks"][ha_proxy_network_name]["IPAddress"]
self.parsed_object[ip_address] = container.labels self.parsed_object[ip_address] = container.labels
@ -141,9 +148,28 @@ class Swarm(ProcessorInterface):
super().__init__() super().__init__()
def inspect_network(self): def inspect_network(self):
ha_proxy_service_name = self.client.containers.get(socket.gethostname()).name.split('.')[0]
for endpoint in self.client.services.get(ha_proxy_service_name).attrs['Endpoint']["VirtualIPs"]:
ha_proxy_network_id = endpoint["NetworkID"]
if self.client.networks.get(ha_proxy_network_id).name != 'ingress':
break
self.parsed_object = {} self.parsed_object = {}
for container in self.client.services.list(): for service in self.client.services.list():
self.parsed_object[container.attrs["Endpoint"]["VirtualIPs"][0]["Addr"].split("/")[0]] = container.attrs["Spec"]["Labels"] ip_address = None
network_list = []
for endpoint in service.attrs["Endpoint"]["VirtualIPs"]:
if ha_proxy_network_id == endpoint["NetworkID"]:
ip_address = endpoint["Addr"].split("/")[0]
break
network_list.append(endpoint["NetworkID"])
if ip_address is None:
network_list.append(ha_proxy_network_id)
service.update(networks = network_list)
continue # skip to the next service to give time to update the network
self.parsed_object[ip_address] = service.attrs["Spec"]["Labels"]
class Kubernetes(ProcessorInterface): class Kubernetes(ProcessorInterface):