etcd是一个高可用的分布式键值存储系统,是CoreOS(现在隶属于Red Hat)公司开发的一个开源项目。它提供了一个简单的接口来存储和检索键值对数据,并使用Raft协议实现了分布式一致性。etcd广泛应用于Docker、Kubernetes等分布式系统中,用于存储配置信息、服务发现、领导者选举等方面。
主机名 | ip地址 | cpu数量 | 内存数量 |
etcd-1 | 10.220.43.206 | 2 | 4 |
etcd-2 | 10.220.43.207 | 2 | 4 |
etcd-3 | 10.220.43.208 | 2 | 4 |
$ wget https://github.com/etcd-io/etcd/releases/download/v3.4.27/etcd-v3.4.27-linux-amd64.tar.gz
$ tar xvf etcd-v3.4.27-linux-amd64.tar.gz
$ mv etcd-v3.4.27-linux-amd64/etcd* /usr/local/bin/ ##复制etcd命令文件
$ mkdir -p /var/lib/etcd/ ##创建数据存放目录
$ mkdir -p /etc/etcd ##创建配置文件存放目录
?三个节点设置etcd配置文件,三个节点的防火墙要放行2379及2380端口
#etcd节点1
$ cat /etc/etcd/etcd.conf
ETCD_NAME=etcd-1
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://10.220.43.206:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://10.220.43.206:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.220.43.206:2380"
ETCD_INITIAL_CLUSTER="etcd-1=http://10.220.43.206:2380,etcd-2=http://10.220.43.207:2380,etcd-3=http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="singless"
ETCD_ADVERTISE_CLIENT_URLS="http://10.220.43.206:2379"
#etcd节点2
$ cat /etc/etcd/etcd.conf
ETCD_NAME=etcd-2
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://10.220.43.207:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://10.220.43.207:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.220.43.207:2380"
ETCD_INITIAL_CLUSTER="etcd-2=http://10.220.43.206:2380,etcd-2=http://10.220.43.207:2380,etcd-3=http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="singless"
ETCD_ADVERTISE_CLIENT_URLS="http://10.220.43.207:2379"
#etcd节点3
$ cat /etc/etcd/etcd.conf
ETCD_NAME=etcd-3
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://10.220.43.208:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://10.220.43.208:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.220.43.207:2380"
ETCD_INITIAL_CLUSTER="etcd-2=http://10.220.43.206:2380,etcd-2=http://10.220.43.207:2380,etcd-3=http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="singless"
ETCD_ADVERTISE_CLIENT_URLS="http://10.220.43.208:2379"
参数:
配置systemd管理etcd,三个节点配置相同
$ cat /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
三个节点上启动服务
$ systemctl enable etcd
$ systemctl start etcd
报错:
check file permission: directory "/var/lib/etcd" exist, but the permission is "drwxr-xr-x". The recommended permission is "-rwx------" to prevent possible unprivileged access to the data.
解决方案:
$ chmod 700 /var/lib/etcd
$ etcdctl member list
3fccfd9e945d331b, started, etcd-3, http://10.220.43.208:2380, http://10.220.43.208:2379, false
5d19435c38496e2c, started, etcd-2, http://10.220.43.207:2380, http://10.220.43.207:2379, false
9f23d9fd9d308b96, started, etcd-1, http://10.220.43.206:2380, http://10.220.43.206:2379, false
$ etcdctl -w table endpoint status --endpoints=10.220.43.206:2379,10.220.43.207:2379,10.220.43.208:2379
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 10.220.43.206:2379 | 9f23d9fd9d308b96 | 3.4.27 | 20 kB | true | false | 96 | 9 | 9 | |
| 10.220.43.207:2379 | 5d19435c38496e2c | 3.4.27 | 20 kB | false | false | 96 | 9 | 9 | |
| 10.220.43.208:2379 | 3fccfd9e945d331b | 3.4.27 | 16 kB | false | false | 96 | 9 | 9 | |
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
?结果显示etcd-1是leader节点。
$ etcdctl -w table endpoint health --endpoints=10.220.43.206:2379,10.220.43.207:2379,10.220.43.208:2379
+--------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+--------------------+--------+------------+-------+
| 10.220.43.206:2379 | true | 1.740078ms | |
| 10.220.43.207:2379 | true | 2.115924ms | |
| 10.220.43.208:2379 | true | 2.174842ms | |
+--------------------+--------+------------+-------+
假设etcd3出现了异常。
etcd-1节点操作:
$ etcdctl member list
3fccfd9e945d331b, started, etcd-3, http://10.220.43.208:2380, http://10.220.43.208:2379, false
5d19435c38496e2c, started, etcd-2, http://10.220.43.207:2380, http://10.220.43.207:2379, false
9f23d9fd9d308b96, started, etcd-1, http://10.220.43.206:2380, http://10.220.43.206:2379, false
$ etcdctl member remove 3fccfd9e945d331b
Member 3fccfd9e945d331b removed from cluster cee4a9895463ca7d
$ etcdctl member list
5d19435c38496e2c, started, etcd-2, http://10.220.43.207:2380, http://10.220.43.207:2379, false
9f23d9fd9d308b96, started, etcd-1, http://10.220.43.206:2380, http://10.220.43.206:2379, false
etcd-3节点操作:
$ rm -rf /var/lib/etcd
cat /etc/etcd/etcd.conf
ETCD_NAME=etcd-3
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://10.220.43.208:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://10.220.43.208:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER="etcd-1=http://10.220.43.206:2380,etcd-2=http://10.220.43.207:2380,etcd-3=http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER_STATE="existing" ##修改为existing
ETCD_INITIAL_CLUSTER_TOKEN="singless"
ETCD_ADVERTISE_CLIENT_URLS="http://10.220.43.208:2379"
etcd-1节点操作:
$ etcdctl member add etcd-3 --peer-urls=http://10.220.43.208:2380
Member 765137d296bf17e7 added to cluster cee4a9895463ca7d
ETCD_NAME="etcd-3"
ETCD_INITIAL_CLUSTER="etcd-2=http://10.220.43.207:2380,etcd-3=http://10.220.43.208:2380,etcd-1=http://10.220.43.206:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.220.43.208:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"
$ etcdctl member list
5d19435c38496e2c, started, etcd-2, http://10.220.43.207:2380, http://10.220.43.207:2379, false
765137d296bf17e7, unstarted, , http://10.220.43.208:2380, , false
9f23d9fd9d308b96, started, etcd-1, http://10.220.43.206:2380, http://10.220.43.206:2379, false
etcd-3启动etcd。?
$ systemctl start etcd
etcd-1节点操作:
$ etcdctl member list
5d19435c38496e2c, started, etcd-2, http://10.220.43.207:2380, http://10.220.43.207:2379, false
765137d296bf17e7, started, etcd-3, http://10.220.43.208:2380, http://10.220.43.208:2379, false
9f23d9fd9d308b96, started, etcd-1, http://10.220.43.206:2380, http://10.220.43.206:2379, false
https://github.com/etcd-io/etcd/tree/main/etcdctl
etcdctl 是用于与 ETCD 进行交互的命令行工具,它提供了一系列命令和选项,用于检索、设置、修改和删除 ETCD 中的数据。
前面介绍过,etcd是一个分布式的键值存储系统。etcd在键的组织上采用了层次化的空间结构,类似于文件系统中目录的概念。但是并不存在目录的那种层级关系。
类似于文件系统中的目录(文件夹)结构,ETCD的命名空间允许以键的层次结构来组织数据,并且每个键可以唯一地标识一个节点。顶层节点被称为根节点,然后可以在根节点下创建子节点,子节点又可以包含更多子节点,以此类推。这种层次化的结构可以用于将数据进行分类、分类和组织。
例如,假设我们有以下目录结构:
在上述示例中/singless/test和/singless/test/gender,有类似于目录一样的共同的索引前缀/和/singless,但是并不存在目录的那种层级关系。它们都可以作为一个key存储相应的value。
增加数据和修改数据需要用到put命令进行操作
$ etcdctl put /singless/test/gender male ##创建一个键/singless/test/gender,值为male
OK
$ etcdctl put /singless/test true ##创建一个键/singless/test,值为test
OK
查询操作需要使用到get参数
$ etcdctl get /singless/test/gender ##根据具体的key查询对应的值
/singless/test/gender
male
$ etcdctl get --prefix / ##根据索引前缀查询,这里查询的是以/为前缀的key、value,所以能看到所有的键值对
/singless/test
true
/singless/test/gender
male
$ etcdctl put /singless/test false ##使用put修改键/singless/test的value
OK
$ etcdctl get /singless/test
/singless/test
false
删除操作使用del命令
$ etcdctl del /singless/test ##可以指定key进行删除
1
$ etcdctl get --prefix /
/singless/test/gender
male
$ etcdctl del --prefix / ##也可以执行索引前缀进行删除
1
$ etcdctl get --prefix /