etcd集群搭建

环境准备

创建3台虚拟机
OS:ubuntu 18.04

192.168.1.4 node1
192.168.1.5 node2
192.168.1.6 node3

搭建过程

1.安装etcd
apt-get -y install etcd
2.修改配置文件/etc/default/etcd

ETCD_NAME="node1"  #其他节点为node2、node3
ETCD_DATA_DIR="/var/lib/etcd"  #数据存储目录
ETCD_LISTEN_PEER_URLS="http://192.168.1.4:2380"  #和其他节点交互的地址端口
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"  #客户端访问的地址端口
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.4:2380"
ETCD_INITIAL_CLUSTER="node1=http://192.168.1.4:2380,node2=http://192.168.1.5:2380,node3=http://192.168.1.6:2380"  #初始化时集群信息
ETCD_INITIAL_CLUSTER_TOKEN="my-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"

3.启动etcd服务

systemctl daemon-reload && systemctl enable etcd && systemctl start etcd

4.查看状态

root@i-oek1awig:~# etcdctl member list
199c86602717fd2: name=node2 peerURLs=http://192.168.1.5:2380 clientURLs=http://0.0.0.0:2379 isLeader=true
d43635dc1c5f8ff6: name=node3 peerURLs=http://192.168.1.6:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
fec8d1cb15f03818: name=node1 peerURLs=http://192.168.1.4:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
root@i-oek1awig:~# etcdctl cluster-health
member 199c86602717fd2 is healthy: got healthy result from http://0.0.0.0:2379
member d43635dc1c5f8ff6 is healthy: got healthy result from http://0.0.0.0:2379
member fec8d1cb15f03818 is healthy: got healthy result from http://0.0.0.0:2379
cluster is healthy

注册中心

docker可以使用etcd作为注册中心

root@i-oek1awig:~# grep ExecStart /lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store etcd://192.168.1.4:2379,192.168.1.5:2379,192.168.1.6:2379 --cluster-advertise 192.168.1.4:2375
root@i-oek1awig:~# systemctl daemon-reload
root@i-oek1awig:~# systemctl restart docker

推荐阅读更多精彩内容