Docker swarmモードを使ってみる
概要
主な機能
- Docker Engineのクラスタ管理機能
- 分散化の設計
- 宣言型サービスモデル
- スケーリング
- 調整(reconciliation)
- マルチホスト・ネットワーク
- サービス・ディスカバリ
- 負荷分散
- デフォルトでTLSをサポート
- ローリングアップデート
やってみた
構成
環境はGCPを使って構築しました。こんな感じです。
クラスタの初期化
managerの登録をします。
[root@test-docker-swarm-manager1 ~]# docker swarm init --advertise-addr 10.10.0.2 Swarm initialized: current node (yctgpnosj9rofcfury6ky2q6h) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5uoztt547zp0qxd12a9d6voyoheuzvx3j8brnv32psqc7jg3i0-3jzvfiul65w1lp4k94h9fsa1j \ 10.10.0.2:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@test-docker-swarm-manager1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS yctgpnosj9rofcfury6ky2q6h * test-docker-swarm-manager1 Ready Active Leader
ワーカーノードをクラスタに追加
workerの登録をします。
[root@test-docker-swarm-worker1 ~]# docker swarm join --token SWMTKN-1-5uoztt547zp0qxd12a9d6voyoheuzvx3j8brnv32psqc7jg3i0-3jzvfiul65w1lp4k94h9fsa1j 10.10.0.2:2377 This node joined a swarm as a worker.
[root@test-docker-swarm-worker2 ~]# docker swarm join --token SWMTKN-1-5uoztt547zp0qxd12a9d6voyoheuzvx3j8brnv32psqc7jg3i0-3jzvfiul65w1lp4k94h9fsa1j 10.10.0.2:2377 This node joined a swarm as a worker.
workerが登録されていることを確認します。
[root@test-docker-swarm-manager1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 46uom5va7tky70rsgzizxp4vb test-docker-swarm-worker2 Ready Active v5tq6yndcng6p4nvsq8kil39y test-docker-swarm-worker1 Ready Active yctgpnosj9rofcfury6ky2q6h * test-docker-swarm-manager1 Ready Active Leader
サービスのデプロイ
[root@test-docker-swarm-manager1 ~]# docker service create --replicas 1 --name helloworld alpine ping docker.com hhlqb2f55qgimeom3t21xlm2y Since --detach=false was not specified, tasks will be created in the background. In a future release, --detach=false will become the default.
[root@test-docker-swarm-manager1 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS hhlqb2f55qgi helloworld replicated 1/1 alpine:latest
[root@test-docker-swarm-manager1 ~]# docker service inspect --pretty helloworld ID: hhlqb2f55qgimeom3t21xlm2y Name: helloworld Service Mode: Replicated Replicas: 1 Placement: UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first RollbackConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Rollback order: stop-first ContainerSpec: Image: alpine:latest@sha256:644fcb1a676b5165371437feaa922943aaf7afcfa8bfee4472f6860aad1ef2a0 Args: ping docker.com Resources: Endpoint Mode: vip
[root@test-docker-swarm-manager1 ~]# docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS l24daqf7x6pp helloworld.1 alpine:latest test-docker-swarm-manager1 Running Running about a minute ago
サービスのスケール
[root@test-docker-swarm-manager1 ~]# docker service scale helloworld=5 helloworld scaled to 5
[root@test-docker-swarm-manager1 ~]# docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS l24daqf7x6pp helloworld.1 alpine:latest test-docker-swarm-manager1 Running Running 3 minutes ago fv50udxvj3oe helloworld.2 alpine:latest test-docker-swarm-worker2 Running Running 16 seconds ago q2yqx9y4lwyh helloworld.3 alpine:latest test-docker-swarm-worker2 Running Running 16 seconds ago niiyq63hi2o7 helloworld.4 alpine:latest test-docker-swarm-manager1 Running Running 18 seconds ago zorqlh84vyof helloworld.5 alpine:latest test-docker-swarm-worker1 Running Running 17 seconds ago