这个问题搞了我好久,感谢上帝感谢安德鲁!
奉上解决这个问题的链接:
先编写脚本:
#!/bin/bash
# Swarm mode using Docker Machine
managers=3
workers=3
# create manager machines
echo "======> Creating $managers manager machines ...";
for node in $(seq 1 $managers);
do
echo "======> Creating manager$node machine ...";
docker-machine create -d virtualbox manager$node;
done
# create worker machines
echo "======> Creating $workers worker machines ...";
for node in $(seq 1 $workers);
do
echo "======> Creating worker$node machine ...";
docker-machine create -d virtualbox worker$node;
done
# list all machines
docker-machine ls
# initialize swarm mode and create a manager
echo "======> Initializing first swarm manager ..."
docker-machine ssh manager1 "docker swarm init --listen-addr $(docker-machine ip manager1) --advertise-addr $(docker-machine ip manager1)"
# get manager and worker tokens
export manager_token=`docker-machine ssh manager1 "docker swarm join-token manager -q"`
export worker_token=`docker-machine ssh manager1 "docker swarm join-token worker -q"`
echo "manager_token: $manager_token"
echo "worker_token: $worker_token"
# other masters join swarm
for node in $(seq 2 $managers);
do
echo "======> manager$node joining swarm as manager ..."
docker-machine ssh manager$node \
"docker swarm join \
--token $manager_token \
--listen-addr $(docker-machine ip manager$node) \
--advertise-addr $(docker-machine ip manager$node) \
$(docker-machine ip manager1)"
done
# show members of swarm
docker-machine ssh manager1 "docker node ls"
# workers join swarm
for node in $(seq 1 $workers);
do
echo "======> worker$node joining swarm as worker ..."
docker-machine ssh worker$node \
"docker swarm join \
--token $worker_token \
--listen-addr $(docker-machine ip worker$node) \
--advertise-addr $(docker-machine ip worker$node) \
$(docker-machine ip manager1)"
done
# show members of swarm
docker-machine ssh manager1 "docker node ls"
执行脚本完成效果:
leiyuxingdeMacBook-Pro:beginner-tutorial leiyuxing$ ./swarm-node-vbox-setup.sh
======> Creating 3 manager machines ...
======> Creating manager1 machine ...
Running pre-create checks...
Creating machine...
(manager1) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.docker/machine/machines/manager1/boot2docker.iso...
(manager1) Creating VirtualBox VM...
(manager1) Creating SSH key...
(manager1) Starting the VM...
(manager1) Check network to re-create if needed...
(manager1) 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 manager1
======> Creating manager2 machine ...
Running pre-create checks...
Creating machine...
(manager2) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.docker/machine/machines/manager2/boot2docker.iso...
(manager2) Creating VirtualBox VM...
(manager2) Creating SSH key...
(manager2) Starting the VM...
(manager2) Check network to re-create if needed...
(manager2) 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 manager2
======> Creating manager3 machine ...
Running pre-create checks...
Creating machine...
(manager3) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.docker/machine/machines/manager3/boot2docker.iso...
(manager3) Creating VirtualBox VM...
(manager3) Creating SSH key...
(manager3) Starting the VM...
(manager3) Check network to re-create if needed...
(manager3) 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 manager3
======> Creating 3 worker machines ...
======> Creating worker1 machine ...
Running pre-create checks...
Creating machine...
(worker1) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.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
======> Creating worker2 machine ...
Running pre-create checks...
Creating machine...
(worker2) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.docker/machine/machines/worker2/boot2docker.iso...
(worker2) Creating VirtualBox VM...
(worker2) Creating SSH key...
(worker2) Starting the VM...
(worker2) Check network to re-create if needed...
(worker2) 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 worker2
======> Creating worker3 machine ...
Running pre-create checks...
Creating machine...
(worker3) Copying /Users/leiyuxing/.docker/machine/cache/boot2docker.iso to /Users/leiyuxing/.docker/machine/machines/worker3/boot2docker.iso...
(worker3) Creating VirtualBox VM...
(worker3) Creating SSH key...
(worker3) Starting the VM...
(worker3) Check network to re-create if needed...
(worker3) 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 worker3
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager1 - virtualbox Running tcp://192.168.99.106:2376 v18.05.0-ce
manager2 - virtualbox Running tcp://192.168.99.107:2376 v18.05.0-ce
manager3 - virtualbox Running tcp://192.168.99.108:2376 v18.05.0-ce
worker1 - virtualbox Running tcp://192.168.99.109:2376 v18.05.0-ce
worker2 - virtualbox Running tcp://192.168.99.110:2376 v18.05.0-ce
worker3 - virtualbox Running tcp://192.168.99.111:2376 v18.05.0-ce
======> Initializing first swarm manager ...
Swarm initialized: current node (vawqzf1q3ot3nq6sfrc2up669) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2i2986fo5jd5n4j0477hyqw0q3ojgavofiz8urpk2tb04spv6c-cscmmfaf84zgx8b6flim0r47p 192.168.99.106:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
manager_token: SWMTKN-1-2i2986fo5jd5n4j0477hyqw0q3ojgavofiz8urpk2tb04spv6c-3dsgxzjnea7yg52h871kxwpq6
worker_token: SWMTKN-1-2i2986fo5jd5n4j0477hyqw0q3ojgavofiz8urpk2tb04spv6c-cscmmfaf84zgx8b6flim0r47p
======> manager2 joining swarm as manager ...
This node joined a swarm as a manager.
======> manager3 joining swarm as manager ...
This node joined a swarm as a manager.
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
vawqzf1q3ot3nq6sfrc2up669 * manager1 Ready Active Leader 18.05.0-ce
jfnzdlgjr5rkx345m4xoklujb manager2 Ready Active Reachable 18.05.0-ce
npkizmb8biqm77vjhvf4tb7le manager3 Ready Active Reachable 18.05.0-ce
======> worker1 joining swarm as worker ...
This node joined a swarm as a worker.
======> worker2 joining swarm as worker ...
This node joined a swarm as a worker.
======> worker3 joining swarm as worker ...
This node joined a swarm as a worker.
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
vawqzf1q3ot3nq6sfrc2up669 * manager1 Ready Active Leader 18.05.0-ce
jfnzdlgjr5rkx345m4xoklujb manager2 Ready Active Reachable 18.05.0-ce
npkizmb8biqm77vjhvf4tb7le manager3 Ready Active Reachable 18.05.0-ce
s1mfw2gr84jk5x19c084wnux5 worker1 Ready Active 18.05.0-ce
gb6y6427sw0yp24nfx1d19btc worker2 Ready Active 18.05.0-ce
2xoetpqolee4w8l2cmq7u30t1 worker3 Ready Active 18.05.0-ce