通过docker部署consul集群。最新版本:1.8.3
官方Dockerfile:https://github.com/hashicorp/docker-consul/blob/master/0.X/Dockerfile
FROM alpine:3.12 LABEL org.opencontainers.image.authors="Consul Team" ENV CONSUL_VERSION=1.8.3 ENV HASHICORP_RELEASES=https://releases.hashicorp.com RUN addgroup consul && \ adduser -S -G consul consul RUN set -eux && \ apk add --no-cache ca-certificates curl dumb-init gnupg libcap openssl su-exec iputils jq libc6-compat && \ gpg --keyserver pgp.mit.edu --recv-keys 91A6E7F85D05C65630BEF18951852D87348FFC4C && \ mkdir -p /tmp/build && \ cd /tmp/build && \ apkArch="$(apk --print-arch)" && \ case "${apkArch}" in \ aarch64) consulArch='arm64' ;; \ armhf) consulArch='armhfv6' ;; \ x86) consulArch='386' ;; \ x86_64) consulArch='amd64' ;; \ *) echo >&2 "error: unsupported architecture: ${apkArch} (see ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/)" && exit 1 ;; \ esac && \ wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_${consulArch}.zip && \ wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_SHA256SUMS && \ wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_SHA256SUMS.sig && \ gpg --batch --verify consul_${CONSUL_VERSION}_SHA256SUMS.sig consul_${CONSUL_VERSION}_SHA256SUMS && \ grep consul_${CONSUL_VERSION}_linux_${consulArch}.zip consul_${CONSUL_VERSION}_SHA256SUMS | sha256sum -c && \ unzip -d /bin consul_${CONSUL_VERSION}_linux_${consulArch}.zip && \ cd /tmp && \ rm -rf /tmp/build && \ gpgconf --kill all && \ apk del gnupg openssl && \ rm -rf /root/.gnupg && \ consul version RUN mkdir -p /consul/data && \ mkdir -p /consul/config && \ chown -R consul:consul /consul RUN test -e /etc/nsswitch.conf || echo 'hosts: files dns' > /etc/nsswitch.conf VOLUME /consul/data EXPOSE 8300 EXPOSE 8301 8301/udp 8302 8302/udp EXPOSE 8500 8600 8600/udp COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh ENTRYPOINT ["docker-entrypoint.sh"] CMD ["agent", "-dev", "-client", "0.0.0.0"]
consul容器启动时,agent将从 /consul/config
中读取配置JSON文件,数据将保留在 /consul/data
中,可以此进行存储持久化。
这里启动4个Consul Agent。其中3个Server(选举出一个leader),1个Client。
- 主机说明:
系统 | ip | 角色 |
---|---|---|
CentOS 7.8 | 192.168.30.128 | Server |
CentOS 7.8 | 192.168.30.129 | Server |
CentOS 7.8 | 192.168.30.130 | Server |
CentOS 7.8 | 192.168.30.131 | Client |
- 拉取镜像:
所有节点拉取镜像,
mkdir -p /consul/{config,data}docker pull consul
- 集群部署:
第一个server启动,
docker run -d --name=consul-server1 --net=host -e CONSUL_BIND_INTERFACE=ens33 -v /consul/config:/consul/config -v /consul/data:/consul/data consul:latest agent -server -node=agent128 -bootstrap-expect=3 -client=0.0.0.0 -ui
docker exec consul-server1 consul members Node Address Status Type Build Protocol DC Segment agent128 192.168.30.128:8301 alive server 1.8.3 2 dc1 <all>
剩下两个Server启动并加入集群,
docker run -d --name=consul-server2 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -server -node=agent129 -client=0.0.0.0 -join 192.168.30.128 docker run -d --name=consul-server3 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -server -node=agent130 -client=0.0.0.0 -join 192.168.30.128
Client启动并加入集群,
docker run -d --name=consul-client1 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -node=agent131 -client=0.0.0.0 -join 192.168.30.128
- 查看集群:
docker exec consul-server1 consul members Node Address Status Type Build Protocol DC Segment agent128 192.168.30.128:8301 alive server 1.8.3 2 dc1 <all>agent129 192.168.30.129:8301 alive server 1.8.3 2 dc1 <all>agent130 192.168.30.130:8301 alive server 1.8.3 2 dc1 <all>agent131 192.168.30.131:8301 alive client 1.8.3 2 dc1 <default>
- 访问ui:
访问192.168.30.128:8500/ui
,
consul集群部署完成。