我正在尝试使用带有 docker swarm 的 nginx 容器部署 CDN。目前,我在主机中有管理器,并且在 VM 上使用 docker-machine 创建了一名工作人员:
pierpaolo@pierpaolo-Lenovo-G580:~$ docker-machine create -d virtualbox worker1
Running pre-create checks...
Creating machine...
(worker1) Copying /home/pierpaolo/.docker/machine/cache/boot2docker.iso to /home/pierpaolo/.docker/machine/machines/worker1/boot2docker.iso...
(worker1) Creating VirtualBox VM...
(worker1) Creating SSH key...
(worker1) Starting the VM...
(worker1) Check network to re-create if needed...
(worker1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env worker1
pierpaolo@pierpaolo-Lenovo-G580:~$ docker-machine ssh worker1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.12.5, build HEAD : fc49b1e - Fri Dec 16 12:44:49 UTC 2016
Docker version 1.12.5, build 7392c3b
我在主机上运行管理器:
pierpaolo@pierpaolo-Lenovo-G580:~$ docker swarm init
Swarm initialized: current node (99rr4i1w7wgn5usak9s20fikb) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-0tfvpvdlmugavn3fyqtjn9wkd3oeqze6nmw547shp07y73whix-4dqc404txwg58zibnz3lmp30w \
192.168.1.101:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
然后,在worker1上,我运行:
docker@worker1:~$ docker swarm join \
> --token SWMTKN-1-0tfvpvdlmugavn3fyqtjn9wkd3oeqze6nmw547shp07y73whix-4dqc40
4txwg58zibnz3lmp30w \
> 192.168.1.101:2377
This node joined a swarm as a worker.
在主机上,我创建了覆盖网络:
pierpaolo@pierpaolo-Lenovo-G580:~$ docker network create -d overlay mynetwork
cb9plmvxn6twjt5lm7xgnq4ha
并创建了我的服务,有 10 个副本:
docker service create --name nginx-cdn --network mynetwork --replicas 10 -p 9999:80 nginx-new:latest
dt1rfrix71cawpr8czzy4jje4
但是,如下图所示:
pierpaolo@pierpaolo-Lenovo-G580:~$ docker service ls
ID NAME REPLICAS IMAGE COMMAND
dt1rfrix71ca nginx-cdn 0/10 nginx-new:latest
pierpaolo@pierpaolo-Lenovo-G580:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a64270ac470 nginx-new:latest "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx-cdn.6.dhtgs9ilcj6jngxify7ko05e9
4a230d618e81 nginx-new:latest "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx-cdn.4.7pnxf19kpyhf3ex31zs39q884
01a76327729e nginx-new:latest "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx-cdn.5.b12cp73a078mjw55gsr0x6hfn
3a940f28c532 nginx-new:latest "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx-cdn.10.8grve8nw9fmk3tydhimh1xgng
d0e1970380d5 nginx-new:latest "nginx -g 'daemon off" 5 minutes ago Up 5 minutes 80/tcp, 443/tcp nginx-cdn.1.aval6w0ol357x8obxgjzlxnxv
在管理器(主机)上,10 个副本中只有 5 个,而在 worker1 上
docker@worker1:~$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
没有容器。
为什么?
如果我 cmddocker service ps nginx-cdn
有一些像这样的行:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
aval6w0ol357x8obxgjzlxnxv nginx-cdn.1 nginx-new:latest pierpaolo-Lenovo-G580 Shutdown Shutdown 7 seconds ago
48ovjs0rvh6xceaydfjo1nx7j nginx-cdn.2 nginx-new:latest worker1 Shutdown Preparing 58 seconds ago
ayhw38h74o4m503q08gbbg3v1 \_ nginx-cdn.2 nginx-new:latest worker1 Shutdown Rejected about a minute ago "No such image: nginx-new:late…"
但是我创建了一个名为“nginx-new:latest”的图像,如果我从这个图像创建了一个容器,它就可以工作。