Attention ceci est mon brouillon avant de faire une belle documentation sur Docker (il y a à boire et à manger).
Je me suis planté complètement avec HAProxy, pour l’instant je n’ai pas compris mes erreurs car je n’ai pas réussi à avoir accès au logs. Mon étape va être de voir si avec l’autre server de répartition de charge : nginx, j’arrive à faire mieux.
On commence par le téléchargement de la version :
[root@localhost ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx efd26ecc9548: Already exists a3ed95caeb02: Pull complete 83f52fbfa5f8: Pull complete fa664caa1402: Pull complete Digest: sha256:12127e07a75bda1022fbd4ea231f5527a1899aad4679e3940482db3b57383b1d Status: Downloaded newer image for nginx:latest
Ensuite on doit faire la configuration, on va faire une configuration en mode « stream » car notre protocole n’est pas du HTTP, HTTPS ou SMTP.
Et c’est partie pour une première version :
[root@localhost ~]# cat nginx.conf
worker_processes 4;
events {
worker_connections 8192;
}
http {
include mime.types;
default_type application/octet-stream;
}
stream {
upstream stream_backend {
server my-server4-1:8080 weight=5;
server my-server4-2:8081;
server my-server4-3:8082;
server my-server4-4:8083;
}
server {
listen 80;
proxy_pass stream_backend;
}
}
[root@localhost ~]# docker run --name mon-nginx-v1 --link my-server4-1 --link my-server4-2 --link my-server4-3 --link my-server4-4 -p 80:80 -v /root/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
95adc730e7b2e1164d1c25aee18b0077e122f02aa8197d52f7ed6c3cfa8ca77a
[root@localhost ~]# docker logs 95adc730e7b2e1164d1c25aee18b0077e122f02aa8197d52f7ed6c3cfa8ca77a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95adc730e7b2 nginx "nginx -g 'daemon off" 12 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp, 443/tcp mon-nginx-v1
78f6bc54152e my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8083->80/tcp my-server4-4
fce6865343f1 my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8082->80/tcp my-server4-3
a75937eb980d my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8081->80/tcp my-server4-2
eacf3e4bc897 my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8080->80/tcp my-server4-1
2fc533c55725 postgres "/docker-entrypoint.s" 14 hours ago Up 14 hours 0.0.0.0:5432->5432/tcp postgres2
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
J’ai l’impression que cela va être long 🙁 Par contre la bonne nouvelle c’est qu’il y a des logs !
[root@localhost ~]# docker logs 95adc730e7b2e1164d1c25aee18b0077e122f02aa8197d52f7ed6c3cfa8ca77a 2016/04/15 10:11:32 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 0.0.0.0:80, upstream: "172.17.0.3:8080", bytes from/to client:0/0, bytes from/to upstream:0/0 2016/04/15 10:11:32 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 0.0.0.0:80, upstream: "172.17.0.4:8081", bytes from/to client:0/0, bytes from/to upstream:0/0 2016/04/15 10:11:32 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 0.0.0.0:80, upstream: "172.17.0.5:8082", bytes from/to client:0/0, bytes from/to upstream:0/0 2016/04/15 10:11:32 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 0.0.0.0:80, upstream: "172.17.0.6:8083", bytes from/to client:0/0, bytes from/to upstream:0/0 [root@localhost ~]# telnet 172.17.0.3 8080 Trying 172.17.0.3... telnet: connect to address 172.17.0.3: Connection refused [root@localhost ~]# telnet 127.0.0.1 8080 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. HELLO IP eth0 QUIT Connection closed by foreign host.
J’ai compris je dois toujours attaquer le port 80 car je dois penser en terme de container. Et à l’intérieur du container c’est le port 80 qui est ouvert.
[root@localhost ~]# cat nginx.conf
worker_processes 4;
events {
worker_connections 8192;
}
http {
include mime.types;
default_type application/octet-stream;
}
stream {
upstream stream_backend {
server my-server4-1:80 weight=5;
server my-server4-2:80;
server my-server4-3:80;
server my-server4-4:80;
}
server {
listen 80;
proxy_pass stream_backend;
}
}
[root@localhost ~]# docker run --name mon-nginx-v2 --link my-server4-1 --link my-server4-2 --link my-server4-3 --link my-server4-4 -p 80:80 -v /root/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
304ce9e61abbd21e413a465018d79c285877b0fcc6bcb229444c8883b7e8e54d
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO
IP
eth0
INTERFACE
172.17.0.3
QUIT
Connection closed by foreign host.
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO
INTERFACE
172.17.0.3
QUIT
Connection closed by foreign host.
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO
INTERFACE
172.17.0.4
QUIT
Connection closed by foreign host.
[root@localhost ~]# docker logs 304ce9e61abbd21e413a465018d79c285877b0fcc6bcb229444c8883b7e8e54d
Champagne ! Cela fonctionne quand je fais un telnet, je tombe de façon aléatoire sur un des serveurs. Par contre dans les logs je n’ai rien si cela fonctionne, il va falloir que je comprenne comment fonctionne les logs sous Docker. Je vais d’abord revenir sur HAProxy afin de voir si c’est pas le mauvais port qui causait un problème.
[root@localhost ~]# cat haproxy.cfg
global
maxconn 400
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 4
maxconn 200
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
bind *:80
log 127.0.0.1 local0
default_backend serveur
backend serveur
mode tcp
balance roundrobin
server server1 my-server4-1:80
server server2 my-server4-2:80
server server3 my-server4-3:80
server server4 my-server4-4:80
[root@localhost ~]# docker build -t my-haproxy-v15 .
Sending build context to Docker daemon 131.1 kB
Step 1 : FROM haproxy:1.5
---> 011e001101c0
Step 2 : COPY ./haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
---> 596df11f08ac
Removing intermediate container 40b11ef3c4bc
Step 3 : EXPOSE 80
---> Running in b955fb86c4b3
---> 921af748062e
Removing intermediate container b955fb86c4b3
Successfully built 921af748062e
[root@localhost ~]# docker run -d --link my-server4-1 --link my-server4-2 --link my-server4-3 --link my-server4-4 -p 80:80 --name mon-haproxy-v15b my-haproxy-v15
1fd7d0db42301042d221e4765a38480cf91281be6ac859069894fa96856f311f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fd7d0db4230 my-haproxy-v15 "/docker-entrypoint.s" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp mon-haproxy-v15b
78f6bc54152e my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8083->80/tcp my-server4-4
fce6865343f1 my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8082->80/tcp my-server4-3
a75937eb980d my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8081->80/tcp my-server4-2
eacf3e4bc897 my-server4 "/sbin/server4" 4 hours ago Up 4 hours 0.0.0.0:8080->80/tcp my-server4-1
2fc533c55725 postgres "/docker-entrypoint.s" 15 hours ago Up 15 hours 0.0.0.0:5432->5432/tcp postgres2
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO
IP
eth0
INTERFACE
172.17.0.3
QUIT
Connection closed by foreign host.
[root@localhost ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HELLO
INTERFACE
172.17.0.4
QUIT
Connection closed by foreign host.
[root@localhost ~]# docker logs 1fd7d0db42301042d221e4765a38480cf91281be6ac859069894fa96856f311f
haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
Super cela fonctionne aussi avec HAProxy, reste maintenant à voir les logs !