目的
ELB配下に複数のUbuntuインスタンスを置き、それぞれのNginxに対してConsul+Registratorを使ってコンテナを自動でぶら下げたい
コンテナの制御はSwarm+Composeで実現したい
さて夢は叶うのだろうか
環境
ホスト名 | IPアドレス |
---|---|
docker00 | 10.10.0.49 |
docker01 | 10.10.0.50 |
docker02 | 10.10.0.51 |
dockerman | 10.10.0.110 |
Amazon VPC内にUbuntuで作成した
Docker導入x4
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
$ sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install -y lxc-docker
Docker Remote APIの有効化
DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --insecure-registry=10.10.0.110:5000"
ついでにプライベートなレジストリへのアクセスも許可
再起動
$ sudo service docker restart
API確認
2375/tcpをそれぞれ疎通確認
$ docker -H 10.10.0.49:2375 ps -a
$ docker -H 10.10.0.50:2375 ps -a
$ docker -H 10.10.0.51:2375 ps -a
$ docker -H 10.10.0.110:2375 ps -a
Docker-Compose導入x4
# curl -L https://github.com/docker/compose/releases/download/1.1.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
Swarm導入
トークンの取得
どれか1台で実行して取得
# docker run --rm swarm create
Unable to find image 'swarm:latest' locally
511136ea3c5a: Pull complete
ae115241d78a: Pull complete
f49087514537: Pull complete
fff73787bd9f: Pull complete
97c8f6e912d7: Pull complete
33f9d1e808cf: Pull complete
62860d7acc87: Pull complete
bf8b6923851d: Pull complete
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.
Status: Downloaded newer image for swarm:latest
c8e73fe4b34710e915089846d21aef1f
Swarmエージェントとホストの起動
dockermanにはマネージャを入れ、docker00,docker01,docker02にはエージェントだけを起動させる
# mkdir -p /root/docker-host/boot
# cd /root/docker-host/boot
# nano docker-compose.yml
Docker00用
docker-compose.yml
swarmagent:
image: swarm:latest
command: join --addr=10.10.0.49:2375 token://c8e73fe4b34710e915089846d21aef1f
Docker01用
docker-compose.yml
swarmagent:
image: swarm:latest
command: join --addr=10.10.0.50:2375 token://c8e73fe4b34710e915089846d21aef1f
Docker02用
docker-compose.yml
swarmagent:
image: swarm:latest
command: join --addr=10.10.0.51:2375 token://c8e73fe4b34710e915089846d21aef1f
Dockerman用
registry:
image: registry
environment:
- SETTINGS_FLAVOR=s3
- AWS_BUCKET=private-docker
- AWS_KEY=(access-key)
- AWS_SECRET=(secret-key)
- AWS_REGION=us-east-1
- SEARCH_BACKEND=sqlalchemy
ports:
- "5000:5000"
manager:
image: swarm:latest
command: manage --strategy random token://c8e73fe4b34710e915089846d21aef1f
ports:
- "2380:2375"
S3を使ったDocker Registryも利用する予定。
--strategy
オプションはbinpackingとrandomが選べるのだけどbinpackingが予想通りの動きをしなかったので今回はrandomとした
それぞれを立ち上げよう
Docker00〜Docker02
# docker-compose up -d
Creating boot_swarmagent_1...
# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------
boot_swarmagent_1 /swarm join --addr=10.10.0 ... Up 2375/tcp
Dockerman
# docker-compose up -d
Creating boot_registry_1...
Creating boot_swarmmanager_1...
# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
boot_registry_1 docker-registry Up 0.0.0.0:5000->5000/tcp
boot_swarmmanager_1 /swarm manage --strategy r ... Up 0.0.0.0:2380->2375/tcp
ノードが全部検出されているか確認
Dockermanから各ノードを確認する
# docker -H tcp://localhost:2380 info
Containers: 3
Nodes: 3
ip-10-10-0-49: 10.10.0.49:2375
└ Containers: 1
└ Reserved CPUs: 0 / 1
└ Reserved Memory: 0 B / 1.954 GiB
ip-10-10-0-51: 10.10.0.51:2375
└ Containers: 1
└ Reserved CPUs: 0 / 1
└ Reserved Memory: 0 B / 1.954 GiB
ip-10-10-0-50: 10.10.0.50:2375
└ Containers: 1
└ Reserved CPUs: 0 / 1
└ Reserved Memory: 0 B / 1.954 GiB
素敵じゃないか