接上篇:我是上篇(docker-machine hyper-v)
安装好docker-machine并创建hyper-v虚拟机之后,下面记一下基本使用:
.\docker-machine.exe create -d hyperv --hyperv-virtual-switch "Virtual Switch(Main)" manager1
.\docker-machine.exe create -d hyperv --hyperv-virtual-switch "Virtual Switch(Main)" worker1
.\docker-machine.exe create -d hyperv --hyperv-virtual-switch "Virtual Switch(Main)" worker2
创建好虚拟机之后, 使用docker-machine来管理虚拟机节点,并创建服务service,进入docker容器查看。
查看当前虚拟机节点:
PS D:\docker\machine\bin> .\docker-machine.exe ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager1 - hyperv Running tcp://192.168.72.26:2376 v18.09.6
worker1 - hyperv Running tcp://192.168.72.27:2376 v18.09.6
worker2 - hyperv Running tcp://192.168.72.28:2376 v18.09.6
连接manager1节点,并初始化swarm集群, 打印的信息可以看到提示加入集群所需执行的命令。docker info可以查看集群信息
PS D:\docker\machine\bin> .\docker-machine.exe ssh manager1
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@manager1:~$ docker -v
Docker version 18.09.6, build 481bc77
docker@manager1:~$ docker swarm init --advertise-addr 192.168.72.26
Swarm initialized: current node (y1xeo1k4opnq4eea9qeazcyk8) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4zzopt74o78yci6imcma2or4w8jsunh3uai4j3taqpdfvwsgbp-3l1r0uhk7bayrz4rtb4yly5s2 192.168.72.26:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
退出manager1, 分别连接到worker1和worker2,将他们加入到集群中,docker swarm join --token xxxx ip:port
docker@manager1:~$ exit
logout
PS D:\docker\machine\bin> .\docker-machine.exe ssh worker1
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@worker1:~$ docker -v
Docker version 18.09.6, build 481bc77
docker@worker1:~$ docker swarm join --token SWMTKN-1-4zzopt74o78yci6imcma2or4w8jsunh3uai4j3taqpdfvwsgbp-3l1r0uhk7bayrz4rtb4yly5s2 192.168.72.26:2377
This node joined a swarm as a worker.
重新进入manager1,查看集群信息
PS D:\docker\machine\bin> .\docker-machine.exe ssh manager1
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
y1xeo1k4opnq4eea9qeazcyk8 * manager1 Ready Active Leader 18.09.6
zqgb9fcdmxptulfkanczyf0i0 worker1 Ready Active 18.09.6
99y1cqxlbfsunzsrwwipp6i60 worker2 Ready Active 18.09.6
创建一个ping服务,并查看服务运行状态,--replicas 启动副本数,--name 服务名称, alpine代表镜像名称,后面的是执行的命令。alpine镜像需要存在,所以要先pull下来,然后再进行后续操作
docker@manager1:~$ docker search alpine
...
docker@manager1:~$ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:ca1c944a4f8486a153024d9965aafbe24f5723c1d5c02f4964c045a16d19dc54
Status: Downloaded newer image for alpine:latest
docker@manager1:~$ docker service create --replicas 2 --name hello alpine ping baidu.com
zapayrx2ksozadjo7w52yowtk
overall progress: 1 out of 1 tasks
1/1: running
verify: Waiting 4 seconds to verify that tasks are stable...
verify: Service converged
docker@manager1:~$ docker service ps hello
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
5l2e1ej5cov5 hello.1 alpine:latest worker1 Running Running 11 seconds ago
uiyag7vxfcgy hello.2 alpine:latest manager1 Running Running 11 seconds ago
docker@manager1:~$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
5tmd76toz3nr hello replicated 2/2 alpine:latest
可以连接到worker1,通过docker ps 查看容器中运行的进程。
服务的弹性扩容和收缩,到manager1管理节点执行命令
docker@manager1:~$ docker service update --replicas 3 hello
hello
overall progress: 2 out of 3 tasks
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
docker@manager1:~$ docker service ps hello
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
5l2e1ej5cov5 hello.1 alpine:latest worker1 Running Running 15 minutes ago
uiyag7vxfcgy hello.2 alpine:latest manager1 Running Running 15 minutes ago
2vchohxlhzhm hello.3 alpine:latest worker2 Running Running 9 seconds ago
docker@manager1:~$ docker service update --replicas 1 hello
hello
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
将woker2退出集群, ssh到worker2节点,执行docker swarm leave退出集群,ssh manger1查看集群节点信息,worker2状态为down, 重新加入,执行docker swarm join操作即可。
PS D:\docker\machine\bin> .\docker-machine.exe ssh worker2
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@worker2:~$ docker swarm leave
Node left the swarm.
docker@worker2:~$ exit
logout
PS D:\docker\machine\bin> .\docker-machine.exe ssh manager1
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
y1xeo1k4opnq4eea9qeazcyk8 * manager1 Ready Active Leader 18.09.6
zqgb9fcdmxptulfkanczyf0i0 worker1 Ready Active 18.09.6
99y1cqxlbfsunzsrwwipp6i60 worker2 Down Active 18.09.6
更多命令解释:docker-machine command line