etcd集群安装

etcd是一个应用在分布式环境下的 key/value 存储服务。利用etcd的特性,应用程序可以在集群中共享信息、配置或作服务发现,etcd会在集群的各个节点中复制这些数据并保证这些数据始终正确。etcd无论是在 CoreOS还是Kubernetes体系中都是不可或缺的一环。

rpm安装

安装:

1
2
yum install -y etcd-3.3.11-2.el7.centos.x86_64
#yum install etcd-3.1.9-2.el7.x86_64

版本:3.3.11

配置

etcd0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tee /etc/etcd/etcd.conf << EOF
#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="master"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.80.94:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.80.94:2379,http://192.168.80.94:4001"
ETCD_INITIAL_CLUSTER="master=http://192.168.80.94:2380,node1=http://192.168.80.97:2380,node2=http://192.168.80.99:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

# 查看是否正确
grep -v ^# /etc/etcd/etcd.conf

etcd1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tee /etc/etcd/etcd.conf << EOF
#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="node1"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.80.97:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.80.97:2379,http://192.168.80.97:4001"
ETCD_INITIAL_CLUSTER="master=http://192.168.80.94:2380,node1=http://192.168.80.97:2380,node2=http://192.168.80.99:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

etcd2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tee /etc/etcd/etcd.conf << EOF
#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="node2"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.80.99:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.80.99:2379,http://192.168.80.99:4001"
ETCD_INITIAL_CLUSTER="master=http://192.168.80.94:2380,node1=http://192.168.80.97:2380,node2=http://192.168.80.99:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

启动

1
2
3
4
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl status etcd

测试

1
2
etcdctl cluster-health
etcdctl --endpoints "http://192.168.80.94:2379,http://192.168.80.97:2379,http://192.168.80.99:2379" member list

基于已有集群的服务发现

获取集群标识 size 代表要创建的集群大小:

1
2
3
curl -w "\n" 'https://discovery.etcd.io/new?size=3'
# 返回如下
https://discovery.etcd.io/f6a252c5240cc89b91fa00dac95d5732

设置集群标识,删除掉 ETCD_INITIAL_CLUSTER 字段,添加:

1
ETCD_DISCOVERY="https://discovery.etcd.io/f6a252c5240cc89b91fa00dac95d5732"

也可以通过已有的集群自动发现:
首先需要在已经搭建的etcd中创建用于发现的url

1
2
3
curl -X PUT http://192.168.10.16:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
#返回:
{"action":"set","node":{"key":"/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size","value":"3","modifiedIndex":170010,"createdIndex":170010}}

其中192.168.10.16为另外的etcd集群环境。

如上表示创建一个集群大小为3的etcd发现url,创建成功后按如下配置启动各节点,可以参考手动启动的命令:

1
2
3
4
5
./etcd --name infra0 --initial-advertise-peer-urls http://192.168.10.6:2380 \
--listen-peer-urls http://192.168.10.6:2380 \
--listen-client-urls http://192.168.10.6:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.10.6:2379 \
--discovery http://192.168.10.16:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83

参考

https://mritd.me/2016/09/01/Etcd-%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/
http://blog.csdn.net/u010511236/article/details/52386229