Docker 17.06.0-ce Swarm集群实战(二)
第二章 初步启动应用
下载dockercoins镜像
根据此地址https://github.com/jpetazzo/orchestration-workshop/tree/master/dockercoins中的docker镜像,在虚拟机(安装有harbor)中提前将镜像下载,并制作所需的服务镜像:
- 下载dockercoins文件
git clone https://github.com/jpetazzo/orchestration-workshop.git
- 下载基础镜像
sudo docker pull python:alpine
sudo docker pull ruby:alpine
sudo docker pull redis
sudo docker pull node:4-slim
...
sudo docker images
...
- 制作应用的服务镜像
进入orchestration-workshop/dockercoins目录中,制作出所需的镜像
sudo docker-compose build
...
sudo docker images
...
- 推送服务镜像入harbor中
使用docker tag
命令,修改镜像名称后,将这些镜像送入harbor中(假设harbor建立在192.168.0.106中)
sudo docker push 192.168.0.106/dockercoins/dockercoins_webui:1.0
sudo docker push 192.168.0.106/dockercoins/dockercoins_rng:1.0
sudo docker push 192.168.0.106/dockercoins/dockercoins_hasher:1.0
sudo docker push 192.168.0.106/dockercoins/dockercoins_worker:1.0
sudo docker push 192.168.0.106/common/redis:3.2
在harbor中查看:
-
项目dockercoins中的镜像:
-
项目common中的镜像:
创建本地虚拟机
使用docker-machine创建出docker虚拟机node01、node02、node03、node04和node05
sudo docker-machine create \
--driver virtualbox \
--virtualbox-cpu-count "1" \
--virtualbox-disk-size "20000" \
--virtualbox-memory "1536" \
--virtualbox-boot2docker-url "file://$HOME/tool/docker/machine/iso/boot2docker.iso" \
--engine-insecure-registry 192.168.0.106 \
node01
...
node02
...
node03
...
node04
...
node05
说明:
--driver 虚拟机的驱动类型
--virtualbox-cpu-count cpu数量
--virtualbox-disk-size 磁盘大小
--virtualbox-memory 内存大小
--virtualbox-boot2docker-url 虚拟机系统的镜像地址,因网速原因,建议提前下载到本地,再设置成本地地址
--engine-insecure-registry 本地的docker应用镜像仓库地址,即harbor所在的192.168.0.106
更为详尽的说明,请参考官网docker-machine create
使用docker-machine ls
查看:
搭建swarm集群
设置node01为swarm的manager
sudo docker-machine ssh node01 "docker swarm init --advertise-addr 192.168.99.100:2377"
查看结果:
sudo docker-machine ssh node01 "docker node ls"
设置其他节点为swarm的worker
sudo docker-machine ssh node02 \
"docker swarm join \
--token SWMTKN-1-3qkohhjezgy21hpsou98idgms6hvpaijkz643fguevzra6s3yy-5avd4fgrnb211hqpylsjxozln \
192.168.99.100:2377"
以此类推,继续添加node03、node04、node05。查看结果:
设置node02、node03为manager
sudo docker-machine ssh node01 "docker node promote node02 node03"
启动应用
编辑应用的配置文件dockercoins.yml
version: "3"
services:
webui:
image: 192.168.0.106/dockercoins/dockercoins_webui:1.0
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 100M
restart_policy:
condition: on-failure
ports:
- "8000:80"
networks:
- webnet
hasher:
image: 192.168.0.106/dockercoins/dockercoins_hasher:1.0
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 100M
restart_policy:
condition: on-failure
networks:
- webnet
rng:
image: 192.168.0.106/dockercoins/dockercoins_rng:1.0
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 100M
restart_policy:
condition: on-failure
networks:
- webnet
worker:
image: 192.168.0.106/dockercoins/dockercoins_worker:1.0
deploy:
replicas: 1
resources:
limits:
cpus: "0.2"
memory: 100M
restart_policy:
condition: on-failure
networks:
- webnet
redis:
image: 192.168.0.106/common/redis:3.2
volumes:
- /data:/data
deploy:
placement:
constraints: [node.hostname==node03]
networks:
- webnet
networks:
webnet:
注意:要预先在node03中设置目录/data
把dockercoins.yml文件发送到node01上:
sudo docker-machine scp dockercoins.yml node01:~
启动dockercoins应用:
sudo docker-machine ssh node01 "docker stack deploy -c dockercoins.yml dockercoins"
sudo docker-machine ssh node01 "docker stack ls"
sudo docker-machine ssh node01 "docker stack ps dockercoins"
sudo docker-machine ssh node01 "docker service ls"
根据上图可知,webui服务在node05上
查看node05的地址:
sudo docker-machine env node05
访问http://192.168.99.104:8000(端口8000在dockercoins.yml已设定)
至此,在swarm集群上的dockercoins应用已被初步启动了。