LoginSignup
15

More than 5 years have passed since last update.

Docker Swarm: Docker Swam を始めましょう

Last updated at Posted at 2015-07-04

Docker Swarm を始めましょう

原文:https://docs.docker.com/swarm/install-w-machine/

このページは、ローカルマシン上に Docker Machine と VirtualBox を使い、Docker Swarm でswarm クラスタを作成する方法を紹介します。一度 swarm を作成して利用できれば、クラウドプロバイダや自分のネットワーク上で、テスト的なデプロイもできるようになるでしょう。

覚えておいていただきたいのは、Docker Swarm は現時点ではベータ版のため、内容は変更される場合があります。本番環境での利用も、まだ推奨しません。

必要条件

ローカルのシステム上に VirtualBox がインストールされているか確認します。Mac OS X や Windows で Docker を使っている場合は、既に VirtualBox はインストール済みです。

Docker Swarm の作成

Docker Machine はホスト上で Docker コンテナを実行可能にします。Docker Swarm の各ノードは、イメージの取得やコンテナ実行のために Docker にアクセスする必要があります。Docker Machine は swarm クラスタ上へのプロビジョニング全てを管理します。

docker-machine でクラスタを作成する前に、ディスカバリ・トークン(discovery token)を作成します。Docker Swarm はディスカバリ(ノードの発見)やエージェントの登録のためにトークンを使います。このトークンを使うことで、swarm クラスタの作成を安全に行えます。

  1. システム上のマシン一覧を表示します。
$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev    *        virtualbox   Running   tcp://192.168.99.100:2376   

この例は、boot2docker がインストールされた Mac OS X システムです。boot2docker マシン上に作られた dev という環境が表示されています。

2.システム上に local という名前の VirtualBox マシンを作成します。

$ docker-machine create -d virtualbox local
INFO[0000] Creating SSH key...                          
INFO[0000] Creating VirtualBox VM...                    
INFO[0005] Starting VirtualBox VM...                    
INFO[0005] Waiting for VM to start...                   
INFO[0050] "local" has been created and is now the active machine. 
INFO[0050] To point your Docker client at it, run this in your shell: eval "$(docker-machine env local)" 

3.local マシンへ接続する環境設定を、シェル上に読み込みます。

$ eval "$(docker-machine env local)"

4.Docker Swarm イメージを使いディスカバリ・トークンを生成します。

以下の swarm create コマンドはコンテナ上で実行されます。もし swarm:latest イメージがローカルマシンに上になければ、Docker は(自動的に)取得します。

$ docker run swarm create
Unable to find image 'swarm:latest' locally
latest: Pulling from swarm
de939d6ed512: Pull complete 
79195899a8a4: Pull complete 
79ad4f2cc8e0: Pull complete 
0db1696be81b: Pull complete 
ae3b6728155e: Pull complete 
57ec2f5f3e06: Pull complete 
73504b2882a3: Already exists 
swarm:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aaaf6c18b8be01a75099cc554b4fb372b8ec677ae81764dcdf85470279a61d6f
Status: Downloaded newer image for swarm:latest
fe0cc96a72cf04dba8c1c4aa79536ec3

この swarm crate コマンドは、トークン fe0cc96a72cf04dba8c1c4aa79536ec3 を返しました。

5.安全な場所にトークンを保管します。

作成したトークンを使って、Docker Swarm の作成という次の手順に進みます。

Swarm manager の起動

自分のネットワーク上にある単一のシステムを、Docker Swarm manager と呼ばれています。Swarm Manager はクラスタ全体におけるオーケストレーションやコンテナのスケジュールを行います。Swam Manager はエージェント群(ノードや Docker ノードとも呼ばれます)を統率します。

Swam agent はコンテナのホスティングを担います。これらは通常の Docker デーモンであり、Docker リモート API を使って通信できます。

この章では、Swam Manager と2つのノードを作成します。

  1. VirtualBox に Swarm Manager を作成します。
docker-machine create \
        -d virtualbox \
        --swarm \
        --swarm-master \
        --swarm-discovery token://<TOKEN-FROM-ABOVE> \
        swarm-master

実行例:

$ docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-master
INFO[0000] Creating SSH key...                          
INFO[0000] Creating VirtualBox VM...                    
INFO[0005] Starting VirtualBox VM...                    
INFO[0005] Waiting for VM to start...                   
INFO[0060] "swarm-master" has been created and is now the active machine. 
INFO[0060] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-master)" 

2.VirtualBox マネージャー(PC上の)を開くと、local マシンと、新しい swarm-master マシンがあります。

3.Swarm ノードを作成します。

    docker-machine create \
    -d virtualbox \
    --swarm \
    --swarm-discovery token://<TOKEN-FROM-ABOVE> \
    swarm-agent-00

実行例:

$ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-00
INFO[0000] Creating SSH key...                          
INFO[0000] Creating VirtualBox VM...                    
INFO[0005] Starting VirtualBox VM...                    
INFO[0006] Waiting for VM to start...                   
INFO[0066] "swarm-agent-00" has been created and is now the active machine. 
INFO[0066] To point your Docker client at it, run this in your shell: eval "$(docker-machine env swarm-agent-00)" 

4.swam-agent-01 という名前の別のエージェントを追加します。

$ docker-machine create -d virtualbox --swarm --swarm-discovery token://fe0cc96a72cf04dba8c1c4aa79536ec3 swarm-agent-01

VirtualBox マネージャーに、2つのエージェントが表示されます。

Swam に直接つなぐ

このステップでは、Swarm マシンに接続し、Swarm クラスタに関連する情報を表示し、Swam クラスタ上でイメージを起動します。

  1. Swarm マスタが実行しているマシンの Docker 環境変数を指定します。
$ eval $(docker-machine env --swarm swarm-master)

2.docker コマンドを使い、新たに作った Swarm クラスタの情報を取得します。

$ docker info
Containers: 4
Strategy: spread
Filters: affinity, health, constraint, port, dependency
Nodes: 3
 swarm-agent-00: 192.168.99.105:2376
    └ Containers: 1
    └ Reserved CPUs: 0 / 8
    └ Reserved Memory: 0 B / 1.023 GiB
 swarm-agent-01: 192.168.99.106:2376
    └ Containers: 1
    └ Reserved CPUs: 0 / 8
    └ Reserved Memory: 0 B / 1.023 GiB
 swarm-master: 192.168.99.104:2376
    └ Containers: 2
    └ Reserved CPUs: 0 / 8

各エージェントとマスタ全てが、ポート 2376 を公開していることが分かります。Swam クラスタを作成するときは、任意のポートを使えますし、ノード毎に異なったポートも使えます。各 Swam ノードは Swam エージェントのコンテナを実行しています。

マスタは Swarm Manager と Swam エージェント・コンテナの両方で動作しています。この状態はエージェントのフェイルオーバーに関する問題があるため、本番環境での利用は推奨しません。しかし、今回のように動作環境を学ぶためには非常に役立ちます。

  1. Swam クラスタ上で実行中のイメージを確認します。
$ docker ps  -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                     NAMES
78be991b58d1        swarm:latest        "/swarm join --addr    3 minutes ago       Up 2 minutes        2375/tcp                                  swarm-agent-01/swarm-agent        
da5127e4f0f9        swarm:latest        "/swarm join --addr    6 minutes ago       Up 6 minutes        2375/tcp                                  swarm-agent-00/swarm-agent        
ef395f316c59        swarm:latest        "/swarm join --addr    16 minutes ago      Up 16 minutes       2375/tcp                                  swarm-master/swarm-agent          
45821ca5208e        swarm:latest        "/swarm manage --tls   16 minutes ago      Up 16 minutes       2375/tcp, 192.168.99.104:3376->3376/tcp   swarm-master/swarm-agent-master 

2.Swam クラスタ上で Docker の hello-world テストイメージを実行します。

$ docker run hello-world
Hello from Docker.
This message shows that your installation appears to be working correctly.


To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (Assuming it was not already locally available.)
 3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.


To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash


For more examples and ideas, visit:
 http://docs.docker.com/userguide/

3.docker ps コマンドを使って、どのノードで何のコンテナが動いているか確認します。

$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS                                     NAMES
54a8690043dd        hello-world:latest   "/hello"               22 seconds ago      Exited (0) 3 seconds ago                                             swarm-agent-00/modest_goodall     
78be991b58d1        swarm:latest         "/swarm join --addr    5 minutes ago       Up 4 minutes               2375/tcp                                  swarm-agent-01/swarm-agent        
da5127e4f0f9        swarm:latest         "/swarm join --addr    8 minutes ago       Up 8 minutes               2375/tcp                                  swarm-agent-00/swarm-agent        
ef395f316c59        swarm:latest         "/swarm join --addr    18 minutes ago      Up 18 minutes              2375/tcp                                  swarm-master/swarm-agent          
45821ca5208e        swarm:latest         "/swarm manage --tls   18 minutes ago      Up 18 minutes              2375/tcp, 192.168.99.104:3376->3376/tcp   swarm-master/swarm-agent-master 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15