etcd 集群部署

官服部署文档https://coreos.com/etcd/docs/latest/clustering.html#etcd-discovery
下面的部署模式为静态模式,还有一种discovery模式。
1、初始化集群,这里以3个节点开始

1
2
3
4
Name        IP          HostName
etcd1 10.10.12.1 C-SVR01
etcd2 10.10.12.2 C-SVR02
etcd3 10.10.12.3 C-SVR03

2、配置
etcd1:

1
2
3
4
5
6
7
8
9
ETCD_NAME=etcd1
ETCD_DATA_DIR="/var/lib/etcd/etcd1"
ETCD_LISTEN_PEER_URLS="http://10.10.12.1:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.10.12.1:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.10.12.1:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://10.10.12.1:2380,etcd2=http://10.10.12.2:2380,etcd3=http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd"
ETCD_ADVERTISE_CLIENT_URLS="http://10.10.12.1:2379"

etcd2:

1
2
3
4
5
6
7
8
9
ETCD_NAME=etcd2
ETCD_DATA_DIR="/var/lib/etcd/etcd2"
ETCD_LISTEN_PEER_URLS="http://10.10.12.2:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.10.12.2:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.10.12.2:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://10.10.12.1:2380,etcd2=http://10.10.12.2:2380,etcd3=http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd"
ETCD_ADVERTISE_CLIENT_URLS="http://10.10.12.2:2379"

etcd3:

1
2
3
4
5
6
7
8
9
ETCD_NAME=etcd3
ETCD_DATA_DIR="/var/lib/etcd/etcd3"
ETCD_LISTEN_PEER_URLS="http://10.10.12.3:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.10.12.3:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://10.10.12.1:2380,etcd2=http://10.10.12.2:2380,etcd3=http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd"
ETCD_ADVERTISE_CLIENT_URLS="http://10.10.12.3:2379"

注意:ETCD_INITIAL_CLUSTER_STATE 初始化的时候全部为new。ETCD_INITIAL_CLUSTER_TOKEN是集群的识别码,集群中每个节点都需要一样。ETCD_ADVERTISE_CLIENT_URLS集群提供给客户端访问的URL需要暴露出来,不能是127.0.0.1。ETCD_LISTEN_CLIENT_URLS暴露自己的同时最好新增一个127.0.0.1的监听地址,便于etcdctl调用,当然用0.0.0.0也是可以的。ETCD_INITIAL_CLUSTER初始化时各个节点的集群信息。

3、新增节点etcd4

1
2
Name        IP            HostName
etcd4 10.10.12.4 C-SVR04

先在已有集群节点添加新节点etcd4的信息:

1
2
3
4
5
6
$ etcdctl member add etcd4 http://10.10.12.4:2380
Added member named etcd4 with ID 4054n89e532b87bc to cluster

ETCD_NAME="etcd4"
ETCD_INITIAL_CLUSTER="etcd1=http://10.10.12.1:2380,etcd4=http://10.10.12.4:2380,etcd2=http://10.10.12.2:2380,etcd3=http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"

添加新的节点信息后生成了配置项ETCD_NAME、ETCD_INITIAL_CLUSTE、ETCD_INITIAL_CLUSTER_STATE 用于新的etcd4配置文件。
修改etcd4配置文件如下:

1
2
3
4
5
6
7
8
9
ETCD_NAME=etcd4
ETCD_DATA_DIR="/var/lib/etcd/etcd4"
ETCD_LISTEN_PEER_URLS="http://10.10.12.4:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.10.12.4:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.10.10.2.4:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://10.10.12.1:2380,etcd4=http://10.10.12.4:2380,etcd2=http://10.10.12.2:2380,etcd3=http://10.10.12.3:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd"
ETCD_ADVERTISE_CLIENT_URLS="http://10.10.12.4:2379"

启动etcd服务可以查看集群多了一个新的节点etcd4:

1
2
3
4
5
6
$ systemctl start etcd
$ etcdctl member list
202e5d0e00f1xxxx: name=etcd1 peerURLs=http://10.10.12.1:2380 clientURLs=http://10.10.12.1:2379
4054c49e532bxxxx: name=etcd4 peerURLs=http://10.10.12.4:2380 clientURLs=http://10.10.12.4:2379
a153e6ec7852xxxx: name=etcd2 peerURLs=http://10.10.12.2:2380 clientURLs=http://10.10.12.2:2379
b9c75d181ccbxxxx: name=etcd3 peerURLs=http://10.10.12.3:2380 clientURLs=http://10.10.12.3:2379

后面新的节点添加步骤类似,最后将etcd服务加入启动项:

1
$ systemctl enable etcd
----------------本文结束 感谢阅读----------------