Docker 搭建 etcd 集群及管理

环境 

host1 10.1.99.13

host2 10.1.99.14

host3 10.1.99.15

host4 10.1.99.12(用于测试添加删除节点)

 

初始化集群

host1 

$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
 --name etcd quay.io/coreos/etcd \
 etcd \
 --name etcd0 \
 --advertise-client-urls http://10.1.99.13:2379,http://10.1.99.13:4001 \
 --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
 --initial-advertise-peer-urls http://10.1.99.13:2380 \
 --listen-peer-urls http://0.0.0.0:2380 \
 --initial-cluster-token etcd-cluster-1 \
 --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \
 --initial-cluster-state new  

host2

$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
 --name etcd quay.io/coreos/etcd \
 etcd \
 --name etcd1 \
 --advertise-client-urls http://10.1.99.14:2379,http://10.1.99.14:4001 \
 --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
 --initial-advertise-peer-urls http://10.1.99.14:2380 \
 --listen-peer-urls http://0.0.0.0:2380 \
 --initial-cluster-token etcd-cluster-1 \
 --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \
 --initial-cluster-state new

host3

$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
 --name etcd quay.io/coreos/etcd \
 etcd \
 -name etcd2 \
 --advertise-client-urls http://10.1.99.15:2379,http://10.1.99.15:4001 \
 --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
 --initial-advertise-peer-urls http://10.1.99.15:2380 \
 --listen-peer-urls http://0.0.0.0:2380 \
 --initial-cluster-token etcd-cluster-1 \
 --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \
 --initial-cluster-state new

验证

#选择任意一个节点 进入 etcd shell
$ docker exec -it etcd bin/sh

# 查看节点状态
$ etcdctl member list

52a25183c1fa5a39: name=etcd0 peerURLs=http://10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false
69aa775a244f2954: name=etcd1 peerURLs=http://10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true
f18991cb367747f3: name=etcd2 peerURLs=http://10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false

#查看集群状态
etcdctl cluster-health

member 52a25183c1fa5a39 is healthy: got healthy result from http://10.1.99.13:2379
member 69aa775a244f2954 is healthy: got healthy result from http://10.1.99.14:2379
member f18991cb367747f3 is healthy: got healthy result from http://10.1.99.15:2379
cluster is healthy

#插入一条记录
$ etcdctl set /home/etcdtest value
value
#读取插入记录 在其他节点执行获取到相同结果
$ etcdctl get /home/etcdtest 
value

 

集群管理在集群中加入一个节点

#在现有的集群中执行以下命令
$ etcdctl member add etcd3 http://10.1.99.12:2380

Added member named etcd3 with ID 7b33d7070ed90c25 to cluster

ETCD_NAME="etcd3"
ETCD_INITIAL_CLUSTER="etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"

#在对应的待加入的节点上
$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
 --name etcd quay.io/coreos/etcd \
 etcd \
 --name etcd3 \
 --advertise-client-urls http://10.1.99.12:2379,http://10.1.99.12:4001 \
 --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
 --initial-advertise-peer-urls http://10.1.99.12:2380 \
 --listen-peer-urls http://0.0.0.0:2380 \
 --initial-cluster-token etcd-cluster-1 \
 --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380 \
 --initial-cluster-state existing
注意事项:
1 使用上面生成的配置内容分别修改对应的属性
2  设置 --initial-cluster-state existing


删除节点,
#在现有的集群中执行以下命令
#显示当前集群中的所用节点信息
$ etcdctl member list
52a25183c1fa5a39: name=etcd0 peerURLs=http://10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false
69aa775a244f2954: name=etcd1 peerURLs=http://10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true
7b33d7070ed90c25: name=etcd3 peerURLs=http://10.1.99.12:2380 clientURLs=http://10.1.99.12:2379,http://10.1.99.12:4001 isLeader=false
f18991cb367747f3: name=etcd2 peerURLs=http://10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false 

#在集群中剔除待删除节点
$ etcdctl member remove 7b33d7070ed90c25

 

posted @ 2016-12-07 09:46 Eamon13 阅读(...) 评论(...) 编辑 收藏