-
-
Save rafabene/79b0bc0a8b4e8ee2a9d5 to your computer and use it in GitHub Desktop.
master: | |
image: centos | |
hostname: master | |
net: my-swarm-network | |
command: /bin/sh -c "while true; do echo executing master; sleep 1; done" | |
slave: | |
image: centos | |
net: my-swarm-network | |
command: /bin/sh -c "while true; do echo executing slave; sleep 1; done" | |
$ ./swarm-create.sh | |
$ eval "$(docker-machine env --swarm swarm-master)" | |
$ docker network create --driver overlay my-swarm-network | |
$ docker-compose up -d | |
$ docker exec -ti swarm-node-02/rafaelbenevides_master_1 ping master | |
PING master (10.0.0.3) 56(84) bytes of data. | |
64 bytes from master (10.0.0.3): icmp_seq=1 ttl=64 time=0.032 ms | |
64 bytes from master (10.0.0.3): icmp_seq=2 ttl=64 time=0.048 ms | |
$ docker exec -ti swarm-node-02/rafaelbenevides_master_1 ping rafaelbenevides_slave_1 | |
PING rafaelbenevides_slave_1 (10.0.0.2) 56(84) bytes of data. | |
64 bytes from rafaelbenevides_slave_1 (10.0.0.2): icmp_seq=1 ttl=64 time=1.41 ms | |
$ docker exec -ti swarm-node-01/rafaelbenevides_slave_1 sh -c 'HOST=$(hostname); ping $HOST' | |
PING 6ff0763e7495 (10.0.0.2) 56(84) bytes of data. | |
64 bytes from 6ff0763e7495 (10.0.0.2): icmp_seq=1 ttl=64 time=0.031 ms | |
cho "Creating Multi Host Keystore" | |
docker-machine create -d virtualbox mh-keystore | |
eval "$(docker-machine env mh-keystore)" | |
echo "Starting Consul at Keystore Machine" | |
docker run -d -p "8500:8500" -h "consul" progrium/consul -server -bootstrap | |
echo "Creating Swarm master ..." | |
docker-machine create -d virtualbox --swarm --swarm-master --swarm-strategy "spread" --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-master | |
echo "Creating Swarm node 01 ..." | |
docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-01 | |
echo "Creating Swarm node 02 ..." | |
docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-02 |
That's right! The generated internal hostname doesn't follow the "docker-compose" convention.
If you force the hostname="slave" on slaves and scale it, there will be rafaelbenevides_slave_1, rafaelbenevides_slave_2 and rafaelbenevides_slave_3 for master, but all of them will call itself as "slave". Which makes sense because we forced a hostname.
To check what was the generated hostname for the slave instance, run the following:
$ docker exec -ti swarm-node-02/rafaelbenevides_master_1 cat /etc/hosts
You will see something like this:
10.0.0.3 master
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.0.0.2 rafaelbenevides_slave_1
10.0.0.2 rafaelbenevides_slave_1.my-swarm-network
Now that you found that the generated hostname for slave is rafaelbenevides_slave_1, try to ping that hostname address from the slave instance itself:
$ docker exec -ti swarm-node-01/rafaelbenevides_slave_1 ping rafaelbenevides_slave_1
The command above fails because /etc/hosts of the slave instance does not have the generated hostname entry the same way as in master's.
That is the issue.
Try to ping the slave instance from inside the slave container, giving the hostname generated by Swarm to the slave instance inside the master instance.
In other words:
/etc/hosts inside master will show most likely that the hostname of slave is "rafaelbenevides_slave_1".
You can ping rafaelbenevides_slave_1 from master.
You cannot ping rafaelbenevides_slave_1 from slave itself.