File tree Expand file tree Collapse file tree 3 files changed +52
-16
lines changed Expand file tree Collapse file tree 3 files changed +52
-16
lines changed Original file line number Diff line number Diff line change @@ -60,4 +60,17 @@ $ docker run -d --label ru.grachevko.dhu="nginx.local:10" nginx
60
60
$ ping nginx.local
61
61
```
62
62
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.
Original file line number Diff line number Diff line change @@ -8,6 +8,11 @@ services:
8
8
volumes :
9
9
- /var/run/docker.sock:/var/run/docker.sock
10
10
- ./:/usr/local/app
11
+ - /etc/hosts:/opt/hosts
12
+
13
+ lb :
14
+ image : nginx:alpine
15
+ container_name : lb
11
16
12
17
nginx :
13
18
image : nginx:alpine
@@ -23,3 +28,13 @@ services:
23
28
image : nginx:alpine
24
29
labels :
25
30
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'
Original file line number Diff line number Diff line change @@ -16,22 +16,30 @@ def scan():
16
16
containers = []
17
17
for container in docker .containers .list ():
18
18
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' )
21
36
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
+ })
35
43
36
44
return containers
37
45
You can’t perform that action at this time.
0 commit comments