わかりやすいように、ごく単純なWebサーバをスケールさせる例です

検証環境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cd hoge
$ ls hoge
docker-compose.yml


$ cat docker-compose.yml
version: '3'

services:

web:
image: nginx:alpine
ports:
- 10080-10089:80

ここでのポイントは、ports10080-89:80の部分です。スケールさせる際に割り当てるポートの範囲を指定しています。
(この場合、実質10台までスケール可能、という事になります)

まずは通常のup

1
2
3
4
5
6
7
8
9
10
11
12
13
// 普通にup
$ docker-compose up -d web
Creating network "hoge_default" with the default driver
Creating hoge_web_1_f88fc20678ee ... done

// ポート確認
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
748617fa2e75 nginx:alpine "/usr/sbin/nginx -g …" 5 seconds ago Up 3 seconds 0.0.0.0:10080->80/tcp hoge_web_1_c74182e5a539

// IP確認
$ docker network inspect hoge_default | jq -r '.[0].Containers[] | [.Name, "-->", .IPv4Address] | @csv' | column -t -s "," | xargs -ICONTAINER echo "- CONTAINER"
- hoge_web_1_c74182e5a539 --> 192.168.0.2/20

5台にスケール

webコンテナを5個まで増やしてみます

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// --scale web=5 を指定
$ docker-compose up -d --scale web=5 web
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Starting hoge_web_1_c74182e5a539 ... done
Creating hoge_web_2_13bb1a332458 ... done
Creating hoge_web_3_225eac9f629c ... done
Creating hoge_web_4_c27993395577 ... done
Creating hoge_web_5_70bc29594f3e ... done

// ポート確認(ポートがymlに指定した範囲内で割り当てられている)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42e5eaa4d2ca nginx:alpine "/usr/sbin/nginx -g …" 12 seconds ago Up 10 seconds 0.0.0.0:10083->80/tcp hoge_web_4_6d1d9b1012c3
6bcb67358dad nginx:alpine "/usr/sbin/nginx -g …" 12 seconds ago Up 9 seconds 0.0.0.0:10084->80/tcp hoge_web_2_182925f26775
84f9deee96b5 nginx:alpine "/usr/sbin/nginx -g …" 12 seconds ago Up 10 seconds 0.0.0.0:10082->80/tcp hoge_web_5_b8faed640b87
9022a4e4e4fc nginx:alpine "/usr/sbin/nginx -g …" 12 seconds ago Up 10 seconds 0.0.0.0:10081->80/tcp hoge_web_3_8eaf171dc2e7
748617fa2e75 nginx:alpine "/usr/sbin/nginx -g …" About a minute ago Up About a minute 0.0.0.0:10080->80/tcp hoge_web_1_c74182e5a539

// IP確認
$ docker network inspect hoge_default | jq -r '.[0].Containers[] | [.Name, "-->", .IPv4Address] | @csv' | column -t -s "," | xargs -ICONTAINER echo "- CONTAINER"
- hoge_web_4_6d1d9b1012c3 --> 192.168.0.5/20
- hoge_web_2_182925f26775 --> 192.168.0.6/20
- hoge_web_1_c74182e5a539 --> 192.168.0.2/20
- hoge_web_5_b8faed640b87 --> 192.168.0.4/20
- hoge_web_3_8eaf171dc2e7 --> 192.168.0.3/20

3台にスケール

今度は、webコンテナを3個まで減らしてみます

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 3台にスケール(新しい物から順に?stopとrmされる)
$ docker-compose up -d --scale web=3 web
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Stopping and removing hoge_web_4_6d1d9b1012c3 ... done
Stopping and removing hoge_web_5_b8faed640b87 ... done
Starting hoge_web_1_c74182e5a539 ... done
Starting hoge_web_2_182925f26775 ... done
Starting hoge_web_3_8eaf171dc2e7 ... done

// ポート確認
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bcb67358dad nginx:alpine "/usr/sbin/nginx -g …" About a minute ago Up About a minute 0.0.0.0:10084->80/tcp hoge_web_2_182925f26775
9022a4e4e4fc nginx:alpine "/usr/sbin/nginx -g …" About a minute ago Up About a minute 0.0.0.0:10081->80/tcp hoge_web_3_8eaf171dc2e7
748617fa2e75 nginx:alpine "/usr/sbin/nginx -g …" 2 minutes ago Up 2 minutes 0.0.0.0:10080->80/tcp hoge_web_1_c74182e5a539

11台にスケール

冒頭でymlに指定した、portsの範囲外までスケールさせてみるとどうでしょうか

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ docker-compose up -d --scale web=11 web
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Starting hoge_web_1_c74182e5a539 ... done
Starting hoge_web_2_182925f26775 ... done
Starting hoge_web_3_8eaf171dc2e7 ... done
Starting hoge_web_4_e08618e39e12 ... done
Starting hoge_web_5_71f2ef02ddad ... done
Starting hoge_web_6_66bf84908909 ... done
Starting hoge_web_7_46f71cd3488a ... done
Starting hoge_web_8_29a1dbcd2d85 ... done
Starting hoge_web_9_dd345e23b434 ... done
Starting hoge_web_10_d34088bf19cd ... done
Creating hoge_web_11_2e0fb2efdd37 ... error

ERROR: for hoge_web_11_2e0fb2efdd37 Cannot start service web: driver failed programming external connectivity on endpoint hoge_web_11_2dd5a27ceb24 (e72dbf1c2fac14fb47fdf3cfd702ebb1dac849a0ed6c6569511793562a6c5103): all ports are allocated

ERROR: for web Cannot start service web: driver failed programming external connectivity on endpoint hoge_web_11_2dd5a27ceb24 (e72dbf1c2fac14fb47fdf3cfd702ebb1dac849a0ed6c6569511793562a6c5103): all ports are allocated
ERROR: Encountered errors while bringing up the project.

all ports are allocatedとなり、11台目のコンテナは起動を中断しました

down

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ docker-compose down
Stopping hoge_web_10_d34088bf19cd ... done
Stopping hoge_web_6_66bf84908909 ... done
Stopping hoge_web_8_29a1dbcd2d85 ... done
Stopping hoge_web_5_71f2ef02ddad ... done
Stopping hoge_web_9_dd345e23b434 ... done
Stopping hoge_web_7_46f71cd3488a ... done
Stopping hoge_web_4_e08618e39e12 ... done
Stopping hoge_web_2_182925f26775 ... done
Stopping hoge_web_3_8eaf171dc2e7 ... done
Stopping hoge_web_1_c74182e5a539 ... done
Removing hoge_web_11_2dd5a27ceb24 ... done
Removing hoge_web_10_d34088bf19cd ... done
Removing hoge_web_6_66bf84908909 ... done
Removing hoge_web_8_29a1dbcd2d85 ... done
Removing hoge_web_5_71f2ef02ddad ... done
Removing hoge_web_9_dd345e23b434 ... done
Removing hoge_web_7_46f71cd3488a ... done
Removing hoge_web_4_e08618e39e12 ... done
Removing hoge_web_2_182925f26775 ... done
Removing hoge_web_3_8eaf171dc2e7 ... done
Removing hoge_web_1_c74182e5a539 ... done
Removing network hoge_default

docker-compose scaleは非推奨

docker-compose scaleもありますが、現在は、非推奨(deprecated) となっていて、使わないほうが良いみたいです(かわりにup --scaleを使うようにとの事)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ docker-compose scale --help
Set number of containers to run for a service.

Numbers are specified in the form `service=num` as arguments.
For example:

$ docker-compose scale web=2 worker=3

This command is deprecated. Use the up command with the `--scale` flag
instead.

Usage: scale [options] [SERVICE=NUM...]

Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)

スケールさせたコンテナにアクセスを分散する

docker-composeでscaleしたnginxをhaproxyでラウンドロビン振り分けするという記事を書きました