docker部署方式(etcd可以单实例也可以集群)

sudo docker run -d -it \
--name etcd_test \
-e ETCDCTL_API=3 \
-p 2379:2379 \
-p 2380:2380 \
quay.io/coreos/etcd:v3.3.9 \
etcd \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-client-urls http://0.0.0.0:2379

如果需要添加挂载目录,则追加属性--volume=etcd-data:/etcd-data以及--data-dir=/etcd-data;根据电脑架构选择合适的镜像(arm架构:e2eteam/etcd:3.4.13-0-linux-arm64)

基本操作

进入服务

sudo docker exec -it etcd_test sh

基本命令

## 增加
/ # etcdctl --endpoints="127.0.0.1:2379"  put /test_key1/sub1 "key111"
OK
## 查询
/ # etcdctl --endpoints="127.0.0.1:2379"  get /test_key1/sub1
/test_key1/sub1
key111
## 删除
/ # etcdctl --endpoints="127.0.0.1:2379"  del /test_key1/sub1
1
## watch监听某个key 被操作的记录
/ # etcdctl --endpoints="127.0.0.1:2379"  watch /test_key1/sub1
PUT
/test_key1/sub1
123
PUT
/test_key1/sub1
1234
DELETE
/test_key1/sub1
## 查看当前endpoint的状态
/ # etcdctl endpoint --cluster=true status -w table
+---------------------+------------------+---------+---------+-----------+-----------+------------+
|      ENDPOINT       |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+---------------------+------------------+---------+---------+-----------+-----------+------------+
| http://0.0.0.0:2379 | 8e9e05c52164694d |   3.3.9 |   20 kB |      true |         4 |         18 |
+---------------------+------------------+---------+--
## 查看集群状态
/ # etcdctl --endpoints="127.0.0.1:2379" endpoint status -w table
+----------------+------------------+---------+---------+-----------+-----------+------------+
|    ENDPOINT    |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+----------------+------------------+---------+---------+-----------+-----------+------------+
| 127.0.0.1:2379 | 8e9e05c52164694d |   3.3.9 |   20 kB |      true |         4 |         18 |
+----------------+------------------+---------+---------+-----------+-----------+------------+

本地命令配置

  1. 配置etcd服务镜像开机启动
    默认镜像并不会开机启动,但往往我们需要服务开机自启,所以通过在/etc/profile中添加以下内容来实现开机启动etcd容器
# etcd
docker start etcd_test
  1. 为命令设置别名
    因为etcdctl命令在容器内部,每次想要执行都要进到容器里面执行或者使用docker exec etcd_test etcdctl。这样太过于麻烦,我们可以通过别名,简化执行etcdctl的操作。

~/.zshrc或者~/.bashrc文件中添加下列内容

#
# docker etcd
#
alias d_etcdctl='docker exec etcd_test etcdctl'
Last modification:November 2nd, 2021 at 05:17 pm