Consul集群里既有Server也有Client。那除了Consul serverleader,其他节点怎么加入这个Consul集群里呢?我目前看到的Docker方案是其他节点使用consul agent -join$CONSUL_PORT_8500_TCP_ADDR加入Consul集群的。那么CONSUL_PORT_8500_TCP_ADDR是怎么设置到其他容器节点的?
root@httpd:/# env
CONSUL_PORT_8300_TCP_PORT=8300
CONSUL_PORT_53_TCP_PORT=53
VAGRANT_CONSUL_1_PORT_8301_UDP_ADDR=172.17.0.2
HOSTNAME=httpd
CONSUL_PORT_8301_TCP_PROTO=tcp
VAGRANT_CONSUL_1_PORT_8302_TCP_PORT=8302
VAGRANT_CONSUL_1_PORT_53_UDP_PROTO=udp
CONSUL_1_PORT_8300_TCP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8301_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8400_TCP=tcp://172.17.0.2:8400
CONSUL_1_PORT_53_UDP=udp://172.17.0.2:53
CONSUL_1_PORT_8300_TCP_PROTO=tcp
APACHE_RUN_USER=www-data
VAGRANT_CONSUL_1_PORT_8301_TCP_PROTO=tcp
CONSUL_PORT_8301_UDP=udp://172.17.0.2:8301
VAGRANT_CONSUL_1_PORT_8400_TCP_ADDR=172.17.0.2
CONSUL_PORT_53_TCP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8301_UDP_PORT=8301
CONSUL_1_PORT_53_TCP=tcp://172.17.0.2:53
CONSUL_1_PORT_8300_TCP_PORT_START=8300
CONSUL_PORT_53_TCP_PROTO=tcp
VAGRANT_CONSUL_1_PORT_8302_TCP=tcp://172.17.0.2:8302
VAGRANT_CONSUL_1_PORT_8300_UDP_END=udp://172.17.0.2:8302
CONSUL_1_NAME=/vagrant_httpd_1/consul_1
CONSUL_1_PORT_8300_UDP_PORT_END=8302
VAGRANT_CONSUL_1_PORT_53_TCP_PROTO=tcp
CONSUL_1_PORT_8500_TCP=tcp://172.17.0.2:8500
CONSUL_PORT_53_UDP_ADDR=172.17.0.2
CONSUL_PORT_8300_TCP_PORT_START=8300
CONSUL_1_PORT_53_TCP_PORT=53
CONSUL_1_PORT_8302_UDP_PORT=8302
VAGRANT_CONSUL_1_PORT_8400_TCP=tcp://172.17.0.2:8400
CONSUL_1_PORT_8300_UDP_END=udp://172.17.0.2:8302
CONSUL_PORT_8302_UDP_PORT=8302
CONSUL_PORT_8302_TCP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8301_UDP_PROTO=udp
CONSUL_1_PORT_8302_TCP_PORT=8302
CONSUL_1_PORT_8301_TCP=tcp://172.17.0.2:8301
CONSUL_ENV_CONSUL_OPTIONS=-bootstrap -client 0.0.0.0
VAGRANT_CONSUL_1_PORT_8302_UDP_PORT=8302
CONSUL_1_PORT_8302_UDP=udp://172.17.0.2:8302
CONSUL_PORT_53_UDP_PORT=53
VAGRANT_CONSUL_1_PORT_8302_UDP_PROTO=udp
VAGRANT_CONSUL_1_PORT_8300_TCP_START=tcp://172.17.0.2:8300
CONSUL_PORT_8302_UDP_ADDR=172.17.0.2
CONSUL_PORT_8300_TCP=tcp://172.17.0.2:8300
CONSUL_1_PORT_8302_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_53_TCP_PROTO=tcp
VAGRANT_CONSUL_1_PORT_53_TCP=tcp://172.17.0.2:53
CONSUL_1_PORT_8302_UDP_PROTO=udp
VAGRANT_CONSUL_1_PORT_8300_TCP=tcp://172.17.0.2:8300
CONSUL_1_PORT_8301_TCP_PROTO=tcp
CONSUL_PORT_8400_TCP=tcp://172.17.0.2:8400
VAGRANT_CONSUL_1_PORT_8500_TCP_PROTO=tcp
CONSUL_PORT_8301_TCP_PORT=8301
VAGRANT_CONSUL_1_PORT_53_UDP_PORT=53
VAGRANT_CONSUL_1_PORT_8300_TCP_PROTO=tcp
CONSUL_1_PORT_8301_UDP_ADDR=172.17.0.2
CONSUL_1_PORT_8300_TCP_START=tcp://172.17.0.2:8300
APACHE_LOG_DIR=/var/log/apache2
CONSUL_PORT_53_TCP=tcp://172.17.0.2:53
VAGRANT_CONSUL_1_PORT_8301_TCP_PORT=8301
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CONSUL_PORT_8500_TCP_ADDR=172.17.0.2
CONSUL_PORT_8301_TCP=tcp://172.17.0.2:8301
CONSUL_PORT_8300_TCP_START=tcp://172.17.0.2:8300
VAGRANT_CONSUL_1_PORT_53_UDP=udp://172.17.0.2:53
CONSUL_PORT_8500_TCP=tcp://172.17.0.2:8500
CONSUL_PORT_8300_TCP_ADDR=172.17.0.2
PWD=/
CONSUL_PORT_53_UDP=udp://172.17.0.2:53
CONSUL_1_PORT_8400_TCP_PROTO=tcp
CONSUL_1_PORT_53_UDP_PROTO=udp
VAGRANT_CONSUL_1_PORT_53_TCP_PORT=53
CONSUL_1_PORT_8301_UDP=udp://172.17.0.2:8301
APACHE_RUN_GROUP=www-data
VAGRANT_CONSUL_1_PORT_8302_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8300_TCP_PORT=8300
CONSUL_PORT_8400_TCP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8302_TCP_PROTO=tcp
CONSUL_PORT_8400_TCP_PROTO=tcp
VAGRANT_CONSUL_1_PORT_8500_TCP_PORT=8500
CONSUL_1_PORT_8301_UDP_PROTO=udp
CONSUL_PORT_8500_TCP_PORT=8500
VAGRANT_CONSUL_1_ENV_CONSUL_OPTIONS=-bootstrap -client 0.0.0.0
CONSUL_1_PORT_8500_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8300_TCP=tcp://172.17.0.2:8300
VAGRANT_CONSUL_1_PORT_8400_TCP_PORT=8400
CONSUL_1_PORT_8301_TCP_ADDR=172.17.0.2
SHLVL=1
HOME=/root
CONSUL_PORT_8302_TCP_PORT=8302
CONSUL_PORT_8300_UDP_PORT_END=8302
VAGRANT_CONSUL_1_PORT_8500_TCP=tcp://172.17.0.2:8500
VAGRANT_CONSUL_1_PORT_53_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8301_UDP_PORT=8301
CONSUL_1_ENV_CONSUL_OPTIONS=-bootstrap -client 0.0.0.0
CONSUL_PORT_8300_TCP_PROTO=tcp
CONSUL_1_PORT_8400_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_53_UDP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8300_TCP_PORT=8300
CONSUL_1_PORT_8302_UDP_ADDR=172.17.0.2
CONSUL_PORT_8500_TCP_PROTO=tcp
CONSUL_PORT_8301_UDP_PORT=8301
VAGRANT_CONSUL_1_PORT_8300_TCP_PORT_START=8300
CONSUL_PORT_8302_TCP=tcp://172.17.0.2:8302
CONSUL_PORT_8300_UDP_END=udp://172.17.0.2:8302
VAGRANT_CONSUL_1_PORT_8400_TCP_PROTO=tcp
VAGRANT_CONSUL_1_PORT_8302_UDP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_8300_UDP_PORT_END=8302
VAGRANT_CONSUL_1_PORT=tcp://172.17.0.2:53
VAGRANT_CONSUL_1_PORT_8301_UDP=udp://172.17.0.2:8301
CONSUL_1_PORT_53_TCP_ADDR=172.17.0.2
DEBIAN_FRONTEND=noninteractive
CONSUL_PORT=tcp://172.17.0.2:53
VAGRANT_CONSUL_1_PORT_8300_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8500_TCP_PORT=8500
CONSUL_PORT_8302_TCP_PROTO=tcp
CONSUL_PORT_8301_TCP_ADDR=172.17.0.2
VAGRANT_CONSUL_1_PORT_53_UDP_ADDR=172.17.0.2
CONSUL_PORT_8302_UDP=udp://172.17.0.2:8302
VAGRANT_CONSUL_1_NAME=/vagrant_httpd_1/vagrant_consul_1
VAGRANT_CONSUL_1_PORT_8500_TCP_ADDR=172.17.0.2
CONSUL_1_PORT_8302_TCP=tcp://172.17.0.2:8302
VAGRANT_CONSUL_1_PORT_8301_TCP=tcp://172.17.0.2:8301
CONSUL_1_PORT_8400_TCP_PORT=8400
CONSUL_1_PORT_53_UDP_PORT=53
VAGRANT_CONSUL_1_PORT_8302_UDP=udp://172.17.0.2:8302
CONSUL_1_PORT=tcp://172.17.0.2:53
CONSUL_PORT_8302_UDP_PROTO=udp
CONSUL_1_PORT_8302_TCP_PROTO=tcp
CONSUL_PORT_8301_UDP_PROTO=udp
CONSUL_PORT_8400_TCP_PORT=8400
CONSUL_NAME=/vagrant_httpd_1/consul
CONSUL_PORT_53_UDP_PROTO=udp
CONSUL_PORT_8301_UDP_ADDR=172.17.0.2
CONSUL_1_PORT_8500_TCP_PROTO=tcp
CONSUL_1_PORT_8301_TCP_PORT=8301
_=/usr/bin/env```
搜了一下[Consul代码](https://github.com/hashicorp/consul),毛都没发现。看到了[Using the host ip in docker compose](/questions/29061026/using-the-host-ip-in-docker-compose),才知道是Docker搞的。
在Consul镜像的Dockerfile中,通过EXPOSE命令向其他容器暴露了如下端口:
EXPOSE 53/udp 53 8300 8302 8302/udp 8400 8500
而在Docker-Compose文件中,其他容器使用links连接第一个启动的Consul服务器容器:
httpd: image: ……/httpd:dev hostname: httpd dns: 127.0.0.1 restart: always …… links: - consul:consul
根据Docker文档,Docker会为所连接的容器暴露出来的端口向其他容器创建上面的环境变量。此外,它还会更新其他容器的/etc/hosts中关于所连接容器的主机条目:
root@httpd:/# cat /etc/hosts 172.17.0.4 httpd 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 172.17.0.2 consul_1 consul vagrant_consul_1 172.17.0.2 vagrant_consul_1 consul 172.17.0.2 consul consul vagrant_consul_1
当所连接的容器重启,环境变量中的主机地址不会像/etc/hosts那样自动更新,所以Docker推荐使用/etc/hosts中的连接容器主机条目而不是环境变量。
### 参考
[Using the host ip in docker compose](/questions/29061026/using-the-host-ip-in-docker-compose)
[https://docs.docker.com/userguide/dockerlinks/](https://docs.docker.com/userguide/dockerlinks/)