$ docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.102" Swarm initialized: current node (th2pw563bvvfzvqkd98tsne52) is now a manager.
To add a worker to this swarm, run the following command:
$ docker-machine ssh myvm2 " docker swarm join --token SWMTKN-1-33l8tomuwv9cyt9ig54cztdpl4h9kyfc5vimlv58mb6zdn4fiw-dn3ic01jklavcvvsb8ltr86bu 192.168.99.102:2377" This node joined a swarm as a worker.
myvm2中执行的命令可以直接复制添加myvm1时返回的命令提示信息
在manager中运行docker node ls可以查看节点信息
1 2 3 4
$ docker-machine ssh myvm1 "docker node ls" ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION th2pw563bvvfzvqkd98tsne52 * myvm1 Ready Active Leader 19.03.1 figr3d9s2dhwff1fjxuzo1jvw myvm2 Ready Active 19.03.1
将应用程序部署到swarm上
关联manager和shell
运行以下指令将当前shell与vm1关联到一起
1 2 3 4 5 6 7
$ docker-machine env myvm1 export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.102:2376" export DOCKER_CERT_PATH="/home/yzh/.docker/machine/machines/myvm1" export DOCKER_MACHINE_NAME="myvm1" # Run this command to configure your shell: # eval $(docker-machine env myvm1)
docker-machine create --driver virtualbox myvm1 # Create a VM (Mac, Win7, Linux) docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1 # Win10 docker-machine env myvm1 # View basic information about your node docker-machine ssh myvm1 "docker node ls"# List the nodes in your swarm docker-machine ssh myvm1 "docker node inspect <node ID>"# Inspect a node docker-machine ssh myvm1 "docker swarm join-token -q worker"# View join token docker-machine ssh myvm1 # Open an SSH session with the VM; type "exit" to end docker node ls # View nodes in swarm (while logged on to manager) docker-machine ssh myvm2 "docker swarm leave"# Make the worker leave the swarm docker-machine ssh myvm1 "docker swarm leave -f"# Make master leave, kill swarm docker-machine ls # list VMs, asterisk shows which VM this shell is talking to docker-machine start myvm1 # Start a VM that is currently not running docker-machine env myvm1 # show environment variables and command for myvm1 eval $(docker-machine env myvm1) # Mac command to connect shell to myvm1 & "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression # Windows command to connect shell to myvm1 docker stack deploy -c <file> <app> # Deploy an app; command shell must be set to talk to manager (myvm1), uses local Compose file docker-machine scp docker-compose.yml myvm1:~ # Copy file to node's home dir (only required if you use ssh to connect to manager and deploy the app) docker-machine ssh myvm1 "docker stack deploy -c <file> <app>"# Deploy an app using ssh (you must have first copied the Compose file to myvm1) eval $(docker-machine env -u) # Disconnect shell from VMs, use native docker docker-machine stop $(docker-machine ls -q) # Stop all running VMs docker-machine rm $(docker-machine ls -q) # Delete all VMs and their disk images