单机部署一个ETCD集群
文章发布较早,内容可能过时,阅读注意甄别。
# 1,快速部署。
下载就不多说了,可以直接去 github 进行下载。
单机通过不同端口,部署一个简易集群。
node-1.sh
$ cat /mnt/etcd/node-1.sh
etcd --name infra1 --initial-advertise-peer-urls http://192.168.0.122:2381 \
--listen-peer-urls http://192.168.0.122:2381 \
--listen-client-urls http://192.168.0.122:2379 \
--advertise-client-urls http://192.168.0.122:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra1=http://192.168.0.122:2381,infra2=http://192.168.0.122:2382,infra3=http://192.168.0.122:2383 \
--initial-cluster-state new &> nohup1.out &
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
node-2.sh
$ cat /mnt/etcd/node-2.sh
etcd --name infra2 --initial-advertise-peer-urls http://192.168.0.122:2382 \
--listen-peer-urls http://192.168.0.122:2382 \
--listen-client-urls http://192.168.0.122:2378 \
--advertise-client-urls http://192.168.0.122:2378 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra1=http://192.168.0.122:2381,infra2=http://192.168.0.122:2382,infra3=http://192.168.0.122:2383 \
--initial-cluster-state new &> nohup2.out &
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
node-3.sh
$ cat /mnt/etcd/node-3.sh
etcd --name infra3 --initial-advertise-peer-urls http://192.168.0.122:2383 \
--listen-peer-urls http://192.168.0.122:2383 \
--listen-client-urls http://192.168.0.122:2377 \
--advertise-client-urls http://192.168.0.122:2377 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra1=http://192.168.0.122:2381,infra2=http://192.168.0.122:2382,infra3=http://192.168.0.122:2383 \
--initial-cluster-state new &> nohup3.out &
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
逐一运行如上脚本,即可启动一个集群。
# 2,查看状态。
使用如下命令可以简单检查集群状态:
$ etcdctl --write-out=table --endpoints=http://192.168.0.122:2379,http://192.168.0.122:2377,http://192.168.0.122:2378 endpoint status
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
| http://192.168.0.122:2379 | 13a3eecc5a072589 | 3.3.18 | 20 kB | true | 3 | 11 |
| http://192.168.0.122:2377 | 2b3234939251977f | 3.3.18 | 20 kB | false | 3 | 11 |
| http://192.168.0.122:2378 | ca4d20bca4cb0ff0 | 3.3.18 | 20 kB | false | 3 | 11 |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
各个信息很清晰可以看到。
# 3,简单使用。
通过如下命令,可以设置一条信息。
$ export ETCD_CLUSTER="http://192.168.0.122:2379,http://192.168.0.122:2377,http://192.168.0.122:2378"
$ etcdctl --endpoints=$ETCD_CLUSTER put eryajf "test"
No help topic for 'put'
1
2
3
2
3
看到失败了,报错 No help topic for 'put'
,解决方法是设置如下环境变量:
$ export ETCDCTL_API=3
1
然后再执行如上命令,发现就可以成功了。
获取如上信息:
$ etcdctl --endpoints=$ETCD_CLUSTER get eryajf
eryajf
test
1
2
3
2
3
上次更新: 2024/11/19, 23:11:42