ETCD集群部署
静态配置部署etcd集群
部署一个三节点的etcd集群,节点信息为:
etcd1-101 | 10.0.0.101 | 2380 |
etcd2-102 | 10.0.0.102 | 2380 |
etcd3-103 | 10.0.0.103 | 2380 |
- 接下来对每个节点进行如下部署:
1.下载安装包
地址: https://github.com/etcd-io/etcd/releases
自行选择版本,这里使用当前最新版: v3.5.1
curl -L https://github.com/coreos/etcd/releases/download/v3.5.1/etcd-v3.5.1-linux-amd64.tar.gz -o etcd-v3.5.1-linux-amd64.tar.gz
2.解压 / 创建数据目录
tar -zxvf etcd-v3.5.1-linux-amd64.tar.gz
- 解压后将目录改名为etcd, 放在/usr/local/ 目录下
3.创建数据目录
mkdir /usr/local/etcd/data
4.编辑配置文件
etcd1-101: 10.0.0.101
name: etcd1 data-dir: /usr/local/etcd/data initial-advertise-peer-urls: http://10.0.0.101:2380 listen-peer-urls: http://10.0.0.101:2380 listen-client-urls: http://10.0.0.101:2379,http://127.0.0.1:2379 advertise-client-urls: http://10.0.0.101:2379 initial-cluster-token: Alayman-etcd-cluster initial-cluster: etcd1=http://10.0.0.101:2380,etcd2=http://10.0.0.102:2380,etcd3=http://10.0.0.103:2380 initial-cluster-state: new
etcd2-102: 10.0.0.102
name: etcd2 data-dir: /usr/local/etcd/data initial-advertise-peer-urls: http://10.0.0.102:2380 listen-peer-urls: http://10.0.0.102:2380 listen-client-urls: http://10.0.0.102:2379,http://127.0.0.1:2379 advertise-client-urls: http://10.0.0.102:2379 initial-cluster-token: Alayman-etcd-cluster initial-cluster: etcd1=http://10.0.0.101:2380,etcd2=http://10.0.0.102:2380,etcd3=http://10.0.0.103:2380 initial-cluster-state: new
etcd3-103: 10.0.0.103
name: etcd3 data-dir: /usr/local/etcd/data initial-advertise-peer-urls: http://10.0.0.103:2380 listen-peer-urls: http://10.0.0.103:2380 listen-client-urls: http://10.0.0.103:2379,http://127.0.0.1:2379 advertise-client-urls: http://10.0.0.103:2379 initial-cluster-token: Alayman-etcd-cluster initial-cluster: etcd1=http://10.0.0.101:2380,etcd2=http://10.0.0.102:2380,etcd3=http://10.0.0.103:2380 initial-cluster-state: new
配置文件解释:
- name:当前etcd节点名称。
- data-dir:数据存储目录。
- initial-advertise-peer-urls:集群的其他节点通过该地址与当前节点通信。
- listen-peer-urls:当前节点通过该地址监听集群其他节点发送的信息。
- listen-client-urls:当前节点通过该地址监听客户端发送的信息。
- advertise-client-urls:客户端通过该地址与当前节点通信
- initial-cluster-token:用于区分不同的集群,同一集群的所有节点配置相同的值。
- initial-cluster:当前集群的所有节点信息,当前节点根据此信息与其他节点取得联系。
- initial-cluster-state: 本次是否为新建集群,有两个取值:new和existing。
5.开放端口
etcd server默认使用2379端口监听客户端的请求,使用2380端口监听其他server的请求。本文配置使用的也是2379和2380两个端口。
1. 开放2379和2380两个端口
firewall-cmd --zone=public --add-port=2379/tcp --permanent firewall-cmd --zone=public --add-port=2380/tcp --permanent
2.重启防火墙:
firewall-cmd --reload
3.验证端口是否开放成功:
firewall-cmd --zone=public --query-port=2379/tcp firewall-cmd --zone=public --query-port=2380/tcp
6.配置启动文件
1.创建服务启动文件在/usr/lib/systemd/system/etcd.service 目录下
[Unit] Description=etcd After=network.target [Service] Type=notify ExecStart=/usr/local/etcd/etcd --config-file=/usr/local/etcd/conf.yml [Install] WantedBy=multi-user.target
2.启动服务
1 2 | systemctl daemon-reload systemctl start etcd.service |
3.检查并设置开机自启动
systemctl status etcd.service
systemctl enable etcd.service
7.验证集群
1.将etcdctl 二进制文件复制到 /usr/local/bin/ 目录下,便于执行命令:
cp /usr/local/etcd/etcdctl /usr/local/bin/
2.查看集群成员列表:
[root@etcd3-103~]# etcdctl member list 3fd7902e14c12d66, started, etcd1, http://10.0.0.101:2380, http://10.0.0.101:2379, false 79b84a65e74e6767, started, etcd2, http://10.0.0.102:2380, http://10.0.0.102:2379, false 995b12b403030935, started, etcd3, http://10.0.0.103:2380, http://10.0.0.103:2379, false
3.查看集群成员健康情况:
[root@etcd3-103~]# etcdctl endpoint health --endpoints="http://10.0.0.101:2380,http://10.0.0.102:2380,http://10.0.0.103:2380"http://10.0.0.102:2380 is healthy: successfully committed proposal: took = 5.345893ms http://10.0.0.101:2380 is healthy: successfully committed proposal: took = 5.808915ms http://10.0.0.103:2380 is healthy: successfully committed proposal: took = 5.950364ms
4.查看etcd版本
[root@etcd3-103~]# /usr/local/etcd/etcd --version etcd Version: 3.5.1 Git SHA: e8732fb5f Go Version: go1.16.3 Go OS/Arch: linux/amd64
5.在其中一个节点设置一个key-value,到其他节点查看。
##103写入 [root@etcd3-103~]# etcdctl put Alayman pretty OK [root@etcd3-103~]# etcdctl put golang is-nice OK [root@etcd3-103~]# etcdctl get Alayman Alayman pretty [root@etcd3-103~]# etcdctl get golang golang is-nice ##101/102查看 [root@etcd1-101~]# etcdctl get Alayman Alayman pretty [root@etcd2-102~]# etcdctl get golang golang is-nice
etcd常用命令
插入数据测试
# etcdctl put name1 james
# etcdctl put name11 alice
# etcdctl put name12 seli # etcdctl put name2 jetty # etcdctl put name3 tom # etcdctl put name4 cris
# etcdctl put avg_age 25
查询数据
按key查询
key/value都显示
# etcdctl get name1 name1 james
不显示key只显示value
# etcdctl get --print-value-only name1 james
按key前缀查找
# etcdctl get --prefix name
name1
james
name11
alice
name12
seli
name2
jetty
name3
tom
name4
cris
按key的字节排序的前缀查找 >=
# etcdctl get --from-key name2
name2
jetty
name3
tom
name4
cris
按key的字节排序区间查找<= value <
# etcdctl get name1 name3
name1
james
name11
alice
name12
seli
name2
jetty
查找所有key
# etcdctl get --from-key "" avg_age 25 name1 james name11 alice name12 seli name2 jetty name3 tom name4 cris
删除数据
删除key name11
etcdctl del name11
删除key时并返回被删除的键值对
# etcdctl del --prev-kv name12 1 name12 seli
删除指定字节排序起始值后的key
# etcdctl del --prev-kv --from-key name3 2 name3 tom name4 cris
删除指定前缀的key
# etcdctl del --prev-kv --prefix name 2 name1 james name2 jetty
删除所有数据
# etcdctl del --prefix "" 9
更新数据--直接用put即可
# etcdctl get --prefix "" avg_age 25 # etcdctl put avg_age 30 OK # etcdctl get --prefix "" avg_age 30
博客参考地址: https://www.cnblogs.com/wujuntian/p/15121004.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南