Skip to content

Commit 448eb9e

Browse files
committed
[Feature] LoadBalancer
1 parent f944cec commit 448eb9e

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,17 @@ $ docker run -d --label ru.grachevko.dhu="nginx.local:10" nginx
6060
$ ping nginx.local
6161
```
6262
Container with greater priority will be used, by default priority is 0.
63-
If priority are the same then early created container will be used.
63+
If priority are the same then early created container will be used.
64+
65+
Load Balancer
66+
----
67+
In order to pass traffic through loadbalancer you can define container name which ip will be used to record in hosts.
68+
Just add one more colon and container name after.
69+
```bash
70+
$ docker run -d --name lb nginx
71+
$ docker run -d --label ru.grachevko.dhu="nginx1.local:0:lb" nginx
72+
$ docker run -d --label ru.grachevko.dhu="nginx2.local:0:lb" nginx
73+
$ ping nginx1.local // ip of lb
74+
$ ping nginx2.local // ip of lb
75+
```
76+
Keep in mind, loadbalancer container must have fixed name.

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ services:
88
volumes:
99
- /var/run/docker.sock:/var/run/docker.sock
1010
- ./:/usr/local/app
11+
- /etc/hosts:/opt/hosts
12+
13+
lb:
14+
image: nginx:alpine
15+
container_name: lb
1116

1217
nginx:
1318
image: nginx:alpine
@@ -23,3 +28,13 @@ services:
2328
image: nginx:alpine
2429
labels:
2530
ru.grachevko.dhu: 'img.nginx.local:3;api.nginx.local:5'
31+
32+
nginx4:
33+
image: nginx:alpine
34+
labels:
35+
ru.grachevko.dhu: 'nginx4.local:0:lb'
36+
37+
nginx5:
38+
image: nginx:alpine
39+
labels:
40+
ru.grachevko.dhu: 'nginx5.local:0:lb'

main.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,30 @@ def scan():
1616
containers = []
1717
for container in docker.containers.list():
1818
label = container.attrs.get('Config').get('Labels').get(LABEL)
19-
if label:
20-
ip = next(iter(container.attrs.get('NetworkSettings').get('Networks').values())).get('IPAddress')
19+
if not label:
20+
continue
21+
22+
for string in label.split(';'):
23+
priority = 0
24+
lb = container
25+
26+
if ':' in string:
27+
parts = string.split(':')
28+
string = parts[0]
29+
priority = int(parts[1]) if len(parts) >= 2 else priority
30+
lb = docker.containers.get(parts[2]) if len(parts) == 3 else lb
31+
32+
if not lb:
33+
continue
34+
35+
ip = next(iter(lb.attrs.get('NetworkSettings').get('Networks').values())).get('IPAddress')
2136
if ip:
22-
for string in label.split(';'):
23-
if ':' in string:
24-
string, priority = string.split(':')
25-
priority = int(priority)
26-
else:
27-
priority = 0
28-
29-
containers.append({
30-
'ip': ip,
31-
'priority': priority,
32-
'hosts': string_to_array(string),
33-
'createdAt': container.attrs.get('Created')
34-
})
37+
containers.append({
38+
'ip': ip,
39+
'priority': priority,
40+
'hosts': string_to_array(string),
41+
'createdAt': container.attrs.get('Created'),
42+
})
3543

3644
return containers
3745

0 commit comments

Comments
 (0)