Kubernetes(k8s) etcd配置多节点集群方法

Etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现,它通过Raft一致性算法处理日志复制以保证强一致性,可以理解它为一个高可用强一致性的服务发现存储仓库。本文主要介绍Kubernetes(k8s) etcd配置多节点集群方法。

示例服务器:

etcd1:etcd-41 192.168.31.41
etcd2:etcd-42 192.168.31.42
etcd3:etcd-43 192.168.31.43

1、etcd单节点安装配置

参考文档Kubernetes(k8s) etcd 安装与配置单节点集群方法

2、配置多节点集群

1)参考下面安装etcd,然后删除etcd1现有的集群

systemctl stop etcd
rm -rf /var/lib/etcd/*

2)修改etcd1和etcd2的配置文件

vim /etc/etcd/etcd.conf

etcd1:

ETCD_DATA_DIR="/var/lib/etcd/cluster.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.31.41:2380,http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.31.41:2379,http://localhost:2379"
ETCD_NAME="etcd-41"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.31.41:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379,http://192.168.31.41:2379"
ETCD_INITIAL_CLUSTER="etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.31.42:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

注意:其中,ETCD_DATA_DIR定义了数据存放位置;ETCD_INITIAL_CLUSTER设置了集群中的节点数量;ETCD_NAME定义了本etcd的名称;ETCD_INITIAL_CLUSTER_TOKEN需要确保所有节点相同;ETCD_INITIAL_CLUSTER_STATE可以设置为 new 或者 existingnew表示初始化集群,而existing则是加入现有集群。

etcd2:

TA_DIR="/var/lib/etcd/cluster.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.31.42:2380,http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.31.42:2379,http://localhost:2379"
ETCD_NAME="etcd-42"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.31.42:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379,http://192.168.31.42:2379"
ETCD_INITIAL_CLUSTER="etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.31.42:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

3)启动etcd1和etcd2上etcd服务

systemctl start etcd
systemctl enable etcd

4)查看测试集群状态

在etcd1查看集群信息:

etcdctl member list

在etcd1上创建一个目录,并在etcd2上查看是否已经同步:

[root@etcd1 etcd]# etcdctl put k8s cjavapy
OK
[root@etcd2 etcd]# etcdctl get k8s
k8s
cjavapy

3、etcd集群中添加节点

参考上面文件在etcd3上安装etcd,并设置etcdctl版本为3。然后在etcd1上将etcd3添加入集群,命令如下:

etcdctl member add etcd-43 --peer-urls="http://192.168.31.43:2380"

修改etcd3的配置文件如下:

vim /etc/etcd/etcd.conf

修改内容如下:

TA_DIR="/var/lib/etcd/cluster.etcd"

ETCD_LISTEN_PEER_URLS="http://192.168.31.43:2380,http://localhost:2380"

ETCD_LISTEN_CLIENT_URLS="http://192.168.31.43:2379,http://localhost:2379"

ETCD_NAME="etcd-43"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.31.43:2380"

ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379,http://192.168.31.43:2379"

ETCD_INITIAL_CLUSTER="etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.0.42:2380,etcd-43=http://192.168.31.43:2380"

ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"

ETCD_INITIAL_CLUSTER_STATE="existing"

然后启动etcd3上的etcd服务:

systemctl start etcd

在etcd1上查看etcd3是否成功加入:

[root@etcd1 etcd]# etcdctl member list
243ed279a613f16b, started, etcd-41, http://192.168.31.41:2380, http://192.168.31.41:2379,http://localhost:2379
6929dce9748956d9, started, etcd-43, http://192.168.31.43:2380, http://192.168.31.43:2379,http://localhost:2379
341f7d323462e38f, started, etcd-42, http://192.168.31.42:2380, http://192.168.31.42:2379,http://localhost:2379

4、etcd集群中删除节点

1)在etcd1上删除etcd3节点

etcdctl member remove 6929dce9748956d9

2)在etcd1上查看节点信息

[root@etcd1 etcd]# etcdctl member list
903cd449c601f13a, started, etcd-41, http://192.168.31.41:2380, http://192.168.31.41:2379,http://localhost:2379
876vf3e12sdfe45b, started, etcd-42, http://192.168.31.42:2380, http://192.168.31.42:2379,http://localhost:2379

3)停止删除节点的服务并删除数据

systemctl stop etcd
rm -rf /var/lib/etcd/*


推荐阅读
cjavapy编程之路首页