docker-composeでコンテナをスケールさせる方法のメモ でやった、docker-composeの --scale
オプションで用意した複数のNginxコンテナにラウンドロビン形式でバランシングを行う方法のメモ
ソース nobiki/ha-proxy-sample
docker-compose.yml haproxy イメージを使います
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 version: '3' services: # Webサーバ web: image: nginx:alpine # ロードバランサ haproxy: image: haproxy:2.1.7-alpine ports: - 80:80 volumes: - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg depends_on: - web
haproxy.cfg webサーバを3台スケール --scale web=3
とする前提でcfgファイルを作成しました。詳細なマニュアルはこのあたり にあります
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 defaults timeout connect 5s timeout client 5s timeout server 30s frontend web_proxy bind *:80 use_backend web-server backend web-server balance roundrobin server web-server1 ha-proxy_web_1:80 check inter 2s server web-server2 ha-proxy_web_2:80 check inter 2s server web-server3 ha-proxy_web_3:80 check inter 2s mode http option forwardfor
COMPOSE_PROJECT_NAME 1 2 3 server web-server1 ha-proxy_web_1:80 check inter 2s server web-server2 ha-proxy_web_2:80 check inter 2s server web-server3 ha-proxy_web_3:80 check inter 2s
haproxy.cfg
で、server
を上記のように設定したので、 COMPOSE_PROJECT_NAME=ha-proxy
とします(デフォルトはディレクトリ名 )
1 export COMPOSE_PROJECT_NAME=ha-proxy
up 1 2 3 4 5 6 7 // webをscaleさせる $ docker-compose up -d --scale web=3 Creating network "ha-proxy_default" with the default driver Creating ha-proxy_web_1 ... done Creating ha-proxy_web_2 ... done Creating ha-proxy_web_3 ... done Creating ha-proxy_haproxy_1 ... done
upした後、ブラウザで適当にF5連打してログを観察します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // 振り分けされているのをログで確認 $ docker-compose logs -f Attaching to ha-proxy_haproxy_1, ha-proxy_web_3, ha-proxy_web_1, ha-proxy_web_2 haproxy_1 | [NOTICE] 166/074055 (1) : New worker #1 (6) forked web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:24 +0000] "GET / HTTP/1.1" 200 612 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:25 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:25 +0000] "GET / HTTP/1.1" 304 0 "-" web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:26 +0000] "GET / HTTP/1.1" 304 0 "-" web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:27 +0000] "GET / HTTP/1.1" 304 0 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:27 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:27 +0000] "GET / HTTP/1.1" 304 0 "-" web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:27 +0000] "GET / HTTP/1.1" 304 0 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:27 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:28 +0000] "GET / HTTP/1.1" 304 0 "-" web_1 | 172.23.0.5 - - [15/Jun/2020:07:41:28 +0000] "GET / HTTP/1.1" 304 0 "-" web_2 | 172.23.0.5 - - [15/Jun/2020:07:41:28 +0000] "GET / HTTP/1.1" 304 0 "-" web_3 | 172.23.0.5 - - [15/Jun/2020:07:41:28 +0000] "GET / HTTP/1.1" 304 0 "-"