etcd集群扩展

单节点扩展为 3 节点

1. 安装单节点 etcd

假设单节点 etcd 将安装在 192.168.122.124 上,节点名称为 a8260edb-651e-49b5-a4e5-5145bb99edd3

192.168.122.124 上安装并启动 etcd

1
2
3
4
5
6
7
8
9
10
11
12
13
ETCD_OPTS="--name a8260edb-651e-49b5-a4e5-5145bb99edd3 \
--max-request-bytes 10485760 \
--data-dir /opt/etcd/data/data \
--wal-dir /opt/etcd/data/wal \
--advertise-client-urls http://192.168.122.124:2379,http://127.0.0.1:2379 \
--listen-client-urls http://192.168.122.124:2379,http://127.0.0.1:2379 \
--listen-peer-urls http://192.168.122.124:2380 \
--initial-advertise-peer-urls http://192.168.122.124:2380 \
--initial-cluster-token cluster_token1 \
--initial-cluster a8260edb-651e-49b5-a4e5-5145bb99edd3=http://192.168.122.124:2380 \
--initial-cluster-state new"

etcd $ETCD_OPTS

2. 将 etcd.conf 写入到第二台节点 (192.168.122.23),并启动 etcd

192.168.122.23 上创建 etcd 配置并启动

1
2
3
4
5
6
7
8
9
10
11
12
ETCD_OPTS="--name node2 \
--max-request-bytes 10485760 \
--data-dir /opt/etcd/data/data \
--wal-dir /opt/etcd/data/wal \
--advertise-client-urls http://192.168.122.23:2379,http://127.0.0.1:2379 \
--listen-client-urls http://192.168.122.23:2379,http://127.0.0.1:2379 \
--listen-peer-urls http://192.168.122.23:2380 \
--initial-advertise-peer-urls http://192.168.122.23:2380 \
--initial-cluster a8260edb-651e-49b5-a4e5-5145bb99edd3=http://192.168.122.124:2380,node2=http://192.168.122.23:2380 \
--initial-cluster-state existing"

etcd $ETCD_OPTS

3. 在 node2 中使用 etcdctl 命令,将 node2 添加到集群

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member add node2 --peer-urls=http://192.168.122.23:2380
1
2
3
4
5
6
7
8
9

/opt/etcd/bin/etcdctl --endpoints=http://172.17.0.100:2379 member add test-uid-1 --peer-urls=http://172.17.0.101:2380
/opt/etcd/bin/etcdctl --write-out=table --endpoints=http://172.17.0.100:2379 member list


# remove
/opt/etcd/bin/etcdctl --endpoints=http://172.17.0.100:2379 member remove $(/opt/etcd/bin/etcdctl --endpoints=http://172.17.0.100:2379 member list | grep http://172.17.0.101:2380 | cut -d',' -f1)


4. 将 etcd.conf 写入到第三台节点 (192.168.122.95),并启动 etcd

192.168.122.95 上创建 etcd 配置并启动

1
2
3
4
5
6
7
8
9
10
11
12
ETCD_OPTS="--name node3 \
--max-request-bytes 10485760 \
--data-dir /opt/etcd/data/data \
--wal-dir /opt/etcd/data/wal \
--advertise-client-urls http://192.168.122.95:2379,http://127.0.0.1:2379 \
--listen-client-urls http://192.168.122.95:2379,http://127.0.0.1:2379 \
--listen-peer-urls http://192.168.122.95:2380 \
--initial-advertise-peer-urls http://192.168.122.95:2380 \
--initial-cluster a8260edb-651e-49b5-a4e5-5145bb99edd3=http://192.168.122.124:2380,node2=http://192.168.122.23:2380,node3=http://192.168.122.95:2380 \
--initial-cluster-state existing"

etcd $ETCD_OPTS

5. 在 node3 中使用 etcdctl 命令,将 node3 添加到集群

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member add node3 --peer-urls=http://192.168.122.95:2380

验证集群状态

在任意一个节点上使用 etcdctl 命令验证集群状态,确保所有节点都已成功加入集群:

1
2
3
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member list
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.23:2379 member list
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.95:2379 member list

验证数据一致性

在所有节点上验证存入的键值对:

1
2
3
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 get foo
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.23:2379 get foo
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.95:2379 get foo

三节点到单节点

1. 从 node3 开始移除

node1 (192.168.122.124) 上使用 etcdctl 命令移除 node3

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member remove $(ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member list | grep http://192.168.122.95:2380 | cut -d',' -f1)

验证 node3 是否已移除:

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member list

确认 node3 不在成员列表中。

2. 移除 node2

node1 (192.168.122.124) 上使用 etcdctl 命令移除 node2

1
etcdctl --endpoints=http://192.168.122.124:2379 member remove $(etcdctl --endpoints=http://192.168.122.124:2379 member list | grep http://192.168.122.23:2380 | cut -d',' -f1)

验证 node2 是否已移除:

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member list

确认 node2 不在成员列表中。

3. 更新 node1 配置

node1 上更新 etcd 配置,移除 node2node3 的信息:

192.168.122.124 上更新 etcd 配置并重启 etcd

1
2
3
4
5
6
7
8
9
10
11
12
13
ETCD_OPTS="--name a8260edb-651e-49b5-a4e5-5145bb99edd3 \
--max-request-bytes 10485760 \
--data-dir /opt/etcd/data/data \
--wal-dir /opt/etcd/data/wal \
--advertise-client-urls http://192.168.122.124:2379,http://127.0.0.1:2379 \
--listen-client-urls http://192.168.122.124:2379,http://127.0.0.1:2379 \
--listen-peer-urls http://192.168.122.124:2380 \
--initial-advertise-peer-urls http://192.168.122.124:2380 \
--initial-cluster-token cluster_token1 \
--initial-cluster a8260edb-651e-49b5-a4e5-5145bb99edd3=http://192.168.122.124:2380 \
--initial-cluster-state new"

etcd $ETCD_OPTS

验证最终的单节点状态

使用 etcdctl 验证集群状态

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 member list

确认只有 node1 (a8260edb-651e-49b5-a4e5-5145bb99edd3) 在成员列表中。

验证数据一致性

验证 foo 数据是否存在

1
ETCDCTL_API=3 etcdctl --endpoints=http://192.168.122.124:2379 get foo

etcd集群扩展
https://abrance.github.io/2024/02/04/domain/k8s/etcd集群扩展/
Author
xiaoy
Posted on
February 4, 2024
Licensed under