第二章 初步啟動(dòng)應(yīng)用
下載dockercoins鏡像
根據(jù)此地址https://github.com/jpetazzo/orchestration-workshop/tree/master/dockercoins中的docker鏡像揉抵,在虛擬機(jī)(安裝有harbor)中提前將鏡像下載,并制作所需的服務(wù)鏡像:
- 下載dockercoins文件
git clone https://github.com/jpetazzo/orchestration-workshop.git
- 下載基礎(chǔ)鏡像
sudo docker pull python:alpine
sudo docker pull ruby:alpine
sudo docker pull redis
sudo docker pull node:4-slim
...
sudo docker images
...
- 制作應(yīng)用的服務(wù)鏡像
進(jìn)入orchestration-workshop/dockercoins目錄中,制作出所需的鏡像
sudo docker-compose build
...
sudo docker images
...
- 推送服務(wù)鏡像入harbor中
使用docker tag
命令,修改鏡像名稱后,將這些鏡像送入harbor中(假設(shè)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中查看:
-
項(xiàng)目dockercoins中的鏡像:
-
項(xiàng)目common中的鏡像:
創(chuàng)建本地虛擬機(jī)
使用docker-machine創(chuàng)建出docker虛擬機(jī)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
說(shuō)明:
--driver 虛擬機(jī)的驅(qū)動(dòng)類型
--virtualbox-cpu-count cpu數(shù)量
--virtualbox-disk-size 磁盤大小
--virtualbox-memory 內(nèi)存大小
--virtualbox-boot2docker-url 虛擬機(jī)系統(tǒng)的鏡像地址我擂,因網(wǎng)速原因,建議提前下載到本地缓艳,再設(shè)置成本地地址
--engine-insecure-registry 本地的docker應(yīng)用鏡像倉(cāng)庫(kù)地址校摩,即harbor所在的192.168.0.106
更為詳盡的說(shuō)明,請(qǐng)參考官網(wǎng)docker-machine create
使用docker-machine ls
查看:
搭建swarm集群
設(shè)置node01為swarm的manager
sudo docker-machine ssh node01 "docker swarm init --advertise-addr 192.168.99.100:2377"
查看結(jié)果:
sudo docker-machine ssh node01 "docker node ls"
設(shè)置其他節(jié)點(diǎn)為swarm的worker
sudo docker-machine ssh node02 \
"docker swarm join \
--token SWMTKN-1-3qkohhjezgy21hpsou98idgms6hvpaijkz643fguevzra6s3yy-5avd4fgrnb211hqpylsjxozln \
192.168.99.100:2377"
以此類推阶淘,繼續(xù)添加node03衙吩、node04、node05溪窒。查看結(jié)果:
設(shè)置node02坤塞、node03為manager
sudo docker-machine ssh node01 "docker node promote node02 node03"
啟動(dòng)應(yīng)用
編輯應(yīng)用的配置文件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:
注意:要預(yù)先在node03中設(shè)置目錄/data
把dockercoins.yml文件發(fā)送到node01上:
sudo docker-machine scp dockercoins.yml node01:~
啟動(dòng)dockercoins應(yīng)用:
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"
根據(jù)上圖可知,webui服務(wù)在node05上
查看node05的地址:
sudo docker-machine env node05
訪問(wèn)http://192.168.99.104:8000(端口8000在dockercoins.yml已設(shè)定)
至此澈蚌,在swarm集群上的dockercoins應(yīng)用已被初步啟動(dòng)了摹芙。