Docker 下 基于动态发现部署Etcd集群

img

下载 etcd

mkdir -p ~/storage/app/etcd
cd ~/storage/app/etcd
wget https://jansora.oss-cn-shanghai.aliyuncs.com/lib/microservice/etcd-v3.4.14-linux-amd64.tar.gz
tar xvf etcd-v3.4.14-linux-amd64.tar.gz
export ETCD_HOME=/root/storage/app/etcd/etcd-v3.4.14-linux-amd64
cd $ETCD_HOME

服务发现

服务发现可以采用公共的或者私有的, 怎么选择呢?
能访问公网就采用公有的 https://discovery.etcd.io , 不能访问公网就采用私有发现

获取服务发现token

命令行访问 [[curl https://discovery.etcd.io/new?size=3]] (size 是集群个数) 会返回下面这段结果.
https://discovery.etcd.io/3cb0e9b91e2d05b42da73ec60babeb4d
其中返回的 token 是 3cb0e9b91e2d05b42da73ec60babeb4d, 这个需要记住后面会用到

也可以采用私有的服务发现获取token

padding

创建网卡

配置一个桥接网络, 以后所有依赖 etcd 的应用都可以使用此网络, 比如 dubbo

docker network create -d bridge --subnet=192.168.0.0/24 --gateway=192.168.0.254 --ip-range=192.168.0.0/24 app

配置 Dockerfile

FROM jansora/base:v1
COPY . /app
RUN mkdir -p /data
WORKDIR /app

打包镜像

docker build -t jansora/etcd:v1 $ETCD_HOME -f $ETCD_HOME/Dockerfile

配置启动容器

vim /root/storage/docker/etcd/start.sh

docker run -d  --network=app --name $ETCD_NODE --hostname $ETCD_NODE --ip $ETCD_IP \
-v /root/storage/docker/etcd/data/$ETCD_NODE/data:/data \
jansora/etcd:v1 \
 ./etcd 
--name $ETCD_NODE \
--data-dir /data/data \
--listen-client-urls http://$ETCD_IP:2379 \
--advertise-client-urls http://$ETCD_IP:2379 \
--listen-peer-urls http://$ETCD_IP:2380 \
--initial-advertise-peer-urls http://$ETCD_IP:2380 \
--discovery https://discovery.etcd.io/3cb0e9b91e2d05b42da73ec60babeb4d 

启动容器

共五个节点

export ETCD_NODE=etcd1 && export ETCD_IP=192.168.0.31 && bash /root/storage/docker/etcd/start.sh
export ETCD_NODE=etcd2 && export ETCD_IP=192.168.0.32 && bash /root/storage/docker/etcd/start.sh
export ETCD_NODE=etcd3 && export ETCD_IP=192.168.0.33 && bash /root/storage/docker/etcd/start.sh

日志

root@jansora:~/storage/docker/etcd# docker logs etcd1 
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-12-20 08:34:24.579860 I | etcdmain: etcd Version: 3.4.14
2020-12-20 08:34:24.579895 I | etcdmain: Git SHA: 8a03d2e96
2020-12-20 08:34:24.579898 I | etcdmain: Go Version: go1.12.17
2020-12-20 08:34:24.579903 I | etcdmain: Go OS/Arch: linux/amd64
2020-12-20 08:34:24.579907 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-12-20 08:34:24.580117 I | embed: name = etcd1
2020-12-20 08:34:24.580125 I | embed: data dir = /data/data
2020-12-20 08:34:24.580128 I | embed: member dir = /data/data/member
2020-12-20 08:34:24.580131 I | embed: heartbeat = 100ms
2020-12-20 08:34:24.580133 I | embed: election = 1000ms
2020-12-20 08:34:24.580135 I | embed: snapshot count = 100000
2020-12-20 08:34:24.580138 I | embed: discovery URL= https://discovery.etcd.io/3cb0e9b91e2d05b42da73ec60babeb4d
2020-12-20 08:34:24.580147 I | embed: advertise client URLs = http://192.168.0.31:2379
2020-12-20 08:34:29.603463 N | discovery: found self 67b96d3e5806b00d in the cluster
2020-12-20 08:34:29.603485 N | discovery: found 1 peer(s), waiting for 2 more
2020-12-20 08:35:01.881048 N | discovery: found peer 7c2d78663bb34acb in the cluster
2020-12-20 08:35:01.881067 N | discovery: found 2 peer(s), waiting for 1 more
2020-12-20 08:35:03.579717 N | discovery: found peer d71bc683c74bc046 in the cluster
2020-12-20 08:35:03.579738 N | discovery: found 3 needed peer(s)
2020-12-20 08:35:03.585223 I | etcdserver: starting member 67b96d3e5806b00d in cluster 8245612289a25c94
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=()
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d became follower at term 0
raft2020/12/20 08:35:03 INFO: newRaft 67b96d3e5806b00d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d became follower at term 1
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997)
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997 8947940415138646731)
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997 8947940415138646731 15500200811812339782)
2020-12-20 08:35:03.589186 W | auth: simple token is not cryptographically signed
2020-12-20 08:35:03.594150 I | rafthttp: starting peer 7c2d78663bb34acb...
2020-12-20 08:35:03.594185 I | rafthttp: started HTTP pipelining with peer 7c2d78663bb34acb
2020-12-20 08:35:03.594916 I | rafthttp: started streaming with peer 7c2d78663bb34acb (writer)
2020-12-20 08:35:03.595383 I | rafthttp: started streaming with peer 7c2d78663bb34acb (writer)
2020-12-20 08:35:03.595649 I | rafthttp: started peer 7c2d78663bb34acb
2020-12-20 08:35:03.595681 I | rafthttp: added peer 7c2d78663bb34acb
2020-12-20 08:35:03.595696 I | rafthttp: starting peer d71bc683c74bc046...
2020-12-20 08:35:03.595720 I | rafthttp: started HTTP pipelining with peer d71bc683c74bc046
2020-12-20 08:35:03.596232 I | rafthttp: started streaming with peer 7c2d78663bb34acb (stream Message reader)
2020-12-20 08:35:03.596385 I | rafthttp: started streaming with peer 7c2d78663bb34acb (stream MsgApp v2 reader)
2020-12-20 08:35:03.596983 I | rafthttp: peer 7c2d78663bb34acb became active
2020-12-20 08:35:03.597024 I | rafthttp: established a TCP streaming connection with peer 7c2d78663bb34acb (stream Message reader)
2020-12-20 08:35:03.597177 I | rafthttp: established a TCP streaming connection with peer 7c2d78663bb34acb (stream MsgApp v2 reader)
2020-12-20 08:35:03.597307 I | rafthttp: started streaming with peer d71bc683c74bc046 (writer)
2020-12-20 08:35:03.597949 I | rafthttp: started streaming with peer d71bc683c74bc046 (writer)
2020-12-20 08:35:03.598929 I | rafthttp: started peer d71bc683c74bc046
2020-12-20 08:35:03.599054 I | rafthttp: added peer d71bc683c74bc046
2020-12-20 08:35:03.599080 I | etcdserver: starting server... [version: 3.4.14, cluster version: to_be_decided]
2020-12-20 08:35:03.599176 I | rafthttp: started streaming with peer d71bc683c74bc046 (stream Message reader)
2020-12-20 08:35:03.599476 I | rafthttp: started streaming with peer d71bc683c74bc046 (stream MsgApp v2 reader)
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997 8947940415138646731 15500200811812339782)
2020-12-20 08:35:03.600284 I | etcdserver/membership: added member 67b96d3e5806b00d [http://192.168.0.31:2380] to cluster 8245612289a25c94
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997 8947940415138646731 15500200811812339782)
2020-12-20 08:35:03.600396 I | etcdserver/membership: added member 7c2d78663bb34acb [http://192.168.0.32:2380] to cluster 8245612289a25c94
raft2020/12/20 08:35:03 INFO: 67b96d3e5806b00d switched to configuration voters=(7474125171130281997 8947940415138646731 15500200811812339782)
2020-12-20 08:35:03.600504 I | etcdserver/membership: added member d71bc683c74bc046 [http://192.168.0.33:2380] to cluster 8245612289a25c94
2020-12-20 08:35:03.601528 I | embed: listening for peers on 192.168.0.31:2380
2020-12-20 08:35:03.601846 I | rafthttp: established a TCP streaming connection with peer 7c2d78663bb34acb (stream MsgApp v2 writer)
2020-12-20 08:35:03.602001 I | rafthttp: established a TCP streaming connection with peer 7c2d78663bb34acb (stream Message writer)
2020-12-20 08:35:04.365687 I | rafthttp: peer d71bc683c74bc046 became active
2020-12-20 08:35:04.365719 I | rafthttp: established a TCP streaming connection with peer d71bc683c74bc046 (stream MsgApp v2 writer)
2020-12-20 08:35:04.367075 I | rafthttp: established a TCP streaming connection with peer d71bc683c74bc046 (stream Message writer)
2020-12-20 08:35:04.371822 I | rafthttp: established a TCP streaming connection with peer d71bc683c74bc046 (stream MsgApp v2 reader)
2020-12-20 08:35:04.373574 I | rafthttp: established a TCP streaming connection with peer d71bc683c74bc046 (stream Message reader)
2020-12-20 08:35:04.400919 I | etcdserver: 67b96d3e5806b00d initialized peer connection; fast-forwarding 8 ticks (election ticks 10) with 2 active peer(s)
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d is starting a new election at term 1
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d became candidate at term 2
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d received MsgVoteResp from 67b96d3e5806b00d at term 2
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d [logterm: 1, index: 3] sent MsgVote request to 7c2d78663bb34acb at term 2
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d [logterm: 1, index: 3] sent MsgVote request to d71bc683c74bc046 at term 2
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d received MsgVoteResp from d71bc683c74bc046 at term 2
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d has received 2 MsgVoteResp votes and 0 vote rejections
raft2020/12/20 08:35:04 INFO: 67b96d3e5806b00d became leader at term 2
raft2020/12/20 08:35:04 INFO: raft.node: 67b96d3e5806b00d elected leader 67b96d3e5806b00d at term 2
2020-12-20 08:35:04.406535 I | etcdserver: setting up the initial cluster version to 3.4
2020-12-20 08:35:04.406721 I | etcdserver: published {Name:etcd1 ClientURLs:[http://192.168.0.31:2379]} to cluster 8245612289a25c94
2020-12-20 08:35:04.406734 I | embed: ready to serve client requests
2020-12-20 08:35:04.407232 N | embed: serving insecure client requests on 192.168.0.31:2379, this is strongly discouraged!
2020-12-20 08:35:04.408119 N | etcdserver/membership: set the initial cluster version to 3.4
2020-12-20 08:35:04.408211 I | etcdserver/api: enabled capabilities for version 3.4

评论栏