部署
mkdir /opt/etcd/
wget https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz # 把etcd压到一个本地目录 {etcd-dir} tar -zxvf etcd-v3.3.13-linux-amd64.tar.gz ./etcd 运行 etcd 为服务端启动程序 etcdctl 为客户端启动程序 直接运行 etcd 命令,启动服务端,启动日志展示了etcd 的关键参数及选举过程
创建数据目录和配置文件目录
[root@localhost etcd]# mkdir -p /var/lib/etcd/
[root@localhost etcd]# mkdir -p /etc/etcd/config/
创建配置文件
[root@localhost etcd]# vi /etc/etcd/config/etcd.conf
name: 184etcd
data-dir: /home/scapp/app/etcd/data
listen-peer-urls: http://10.252.161.184:2380
listen-client-urls: http://10.252.161.184:9004,http://127.0.0.1:9004
advertise-client-urls: http://10.252.161.184:9004
创建服务文件
[root@localhost etcd]# vi /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target
[Service]
User=root
Type=notify
ExecStart=/opt/etcd/etcd --config-file /etc/etcd/config/etcd.conf
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
启动服务
[root@localhost etcd]# systemctl daemon-reload
[root@localhost etcd]# systemctl enable etcd
Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /etc/systemd/system/etcd.service.
[root@localhost etcd]# systemctl start etcd
关键配置参数说明
–name:指定 etcd Node 名称,可以使用 hostname。
–data-dir:指定 etcd Server 持久化数据存储目录路径。
–snapshot-count:指定有多少事务(transaction)被提交后,触发截取快照并持久化到磁盘。
–heartbeat-interval:指定 Leader 多久发送一次心跳到 Followers。
–eletion-timeout:指定重新投票的超时时间,如果 Follow 在该时间间隔没有收到 Leader 发出的心跳包,则会触发重新投票。
–listen-peer-urls:指定和 Cluster 其他 Node 通信的地址,比如:http://IP:2380,如果有多个,则使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost。
–listen-client-urls:指定对外提供服务的地址,比如:http://IP:2379,http://127.0.0.1:2379。
–advertise-client-urls:对外通告的该节点的客户端监听地址,会告诉集群中其他节点。
–initial-advertise-peer-urls:对外通告该节点的同伴(Peer)监听地址,这个值会告诉集群中其他节点。
–initial-cluster:指定集群中所有节点的信息,通常为 IP:Port 信息,格式为:node1=http://ip1:2380,node2=http://ip2:2380,…。这里的 node1 就是 --name 指定的名字,ip1:2380 就是 --initial-advertise-peer-urls 指定的值。
–initial-cluster-state:新建集群时,这个值为 new;假如已经存在了集群,这个值为 existing。
–initial-cluster-token:创建集群的 token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 UUID;否则会导致多个集群之间的冲突,造成未知的错误。
集群部署
就是多个主机上分别安装etcd节点,节点加入到统一的集群中。
etcd Cluster 必须具有时间同步服务器,否则会导致 Cluster 频繁进行 Leader Change。在 OpenShift 的 etcd Cluster 中,会每隔 100ms 进行心跳检测。
在安装和启动 etcd 服务进程的时候,各个 Node 都需要知道 Cluster 中其他 Nodes 的信息,一般是 IP:Port 信息。根据用户是否提前知晓(规划)了每个 Node 的 IP 地址,有以下几种不同的集群部署方案:
静态配置:在启动 etcd Server 的时候,通过 --initial-cluster 参数配置好所有的节点信息。
注册到已有的 etcd Cluster:比如官方提供的 discovery.etcd.io。
使用 DNS 启动
我们采用第一种方案
服务器准备
这里准备三台服务器 分别为
192.168.47.10(已按照上述步骤部署单点etcd) 从192.168.47.10克隆两台虚拟机IP分别设置为:192.168.47.11 和 192.168.47.12
增加启动脚本
192.168.47.10增加启动脚 /opt/etcd/etcdClusterStart.sh
————————————————
也可以通过配置文件来启动 配置文件内容如下
/opt/etcd/etcd --name node10 --data-dir /var/lib/etcd --listen-peer-urls http://192.168.47.10:2380 --listen-client-urls http://192.168.47.1:2379,http://127.0.0.1:2379 --snapshot-count 5 --initial-advertise-peer-urls http://192.168.47.11:2380 --initial-cluster node10=http://192.168.47.10:2380,node11=http://192.168.47.11:2380,node12=http://192.168.47.12:2380 --initial-cluster-state new --initial-cluster-token etcd-cluster --advertise-client-urls http://192.168.47.11:2379
[scapp@iZd4l01sl2kkco2pihvlk1Z ~]$ sudo cat /usr/local/etcd/etcd.yml
name: node179
data-dir: /usr/local/etcd/data
wal-dir: /usr/local/etcd/wal
listen-peer-urls: http://10.252.161.179:2380
listen-client-urls: http://10.252.161.179:443,http://127.0.0.1:443
snapshot-count: 5
initial-advertise-peer-urls: http://10.252.161.179:2380
initial-cluster: node178_89=http://10.252.161.179:2381,node179=http://10.252.161.179:2380,node180=http://10.252.161.180:2380
initial-cluster-state: new
initial-cluster-token: etcd-cluster
advertise-client-urls: http://10.252.161.179:443
92.168.47.11增加启动脚/opt/etcd/etcdClusterStart.sh
/opt/etcd/etcd --name node11 --data-dir /var/lib/etcd --listen-peer-urls http://192.168.47.11:2380 --listen-client-urls http://192.168.47.11:2379,http://127.0.0.1:2379 --snapshot-count 5 --initial-advertise-peer-urls http://192.168.47.11:2380 --initial-cluster node10=http://192.168.47.10:2380,node11=http://192.168.47.11:2380,node12=http://192.168.47.12:2380 --initial-cluster-state new --initial-cluster-token etcd-cluster --advertise-client-urls http://192.168.47.11:2379
192.168.47.12增加启动脚/opt/etcd/etcdClusterStart.sh
/opt/etcd/etcd --name node12 --data-dir /var/lib/etcd --listen-peer-urls http://192.168.47.12:2380 --listen-client-urls http://192.168.47.12:2379,http://127.0.0.1:2379 --snapshot-count 5 --initial-advertise-peer-urls http://192.168.47.12:2380 --initial-cluster node10=http://192.168.47.10:2380,node11=http://192.168.47.11:2380,node12=http://192.168.47.12:2380 --initial-cluster-state new --initial-cluster-token etcd-cluster --advertise-client-urls http://192.168.47.12:2379
修改脚本执行权限并启动etcd
chmod u+x /opt/etcd/etcdClusterStart.sh --增加执行权限
/opt/etcd/etcdClusterStart.sh
启动日志可以看到三个节点都加到了 集群de2552097e6fd22a中
2021-04-07 23:16:01.963032 I | etcdserver: starting server... [version: 3.2.32, cluster version: to_be_decided]
2021-04-07 23:16:01.964646 I | etcdserver/membership: added member 3c3338c57d4cba41 [http://192.168.47.12:2380] to cluster de2552097e6fd22a
2021-04-07 23:16:01.964848 I | etcdserver/membership: added member 6d065fcb2738cb70 [http://192.168.47.10:2380] to cluster de2552097e6fd22a
2021-04-07 23:16:01.965021 I | etcdserver/membership: added member debef2b0c1cbfddf [http://192.168.47.11:2380] to cluster de2552097e6fd22a
2021-04-07 23:16:01.966992 I | rafthttp: peer 6d065fcb2738cb70 became active
通过客户端程序查看集群状态
[root@localhost ~]# /opt/etcd/etcdctl member list
3c3338c57d4cba41: name=node12 peerURLs=http://192.168.47.12:2380 clientURLs=http://192.168.47.12:2379 isLeader=false
6d065fcb2738cb70: name=node10 peerURLs=http://192.168.47.10:2380 clientURLs=http://192.168.47.10:2379 isLeader=false
debef2b0c1cbfddf: name=node11 peerURLs=http://192.168.47.11:2380 clientURLs=http://192.168.47.11:2379 isLeader=true
V3API基本操作
alias etc=/root/etcd/etcdctl 为命令取别名 export ETCDCTL_API=3 --使用v3API etc put test zhangsan 写入键值 etc put test3 zhangsan3 写入键值 etc get test3 读取键值 etc get test11 -w=json json格式返回键值,内容比较全 etc get test --prefix 读取键值 根据前缀 etc get test --prefix --limit=1 读取键值根据前缀并限制返回数量 etc get test --prefix --limit 1 etc get test --rev=2 根据版本获取值 etc del test3 删除键值 etc del test test4 根据范围删除键值 etc del --from-key a 删除key字节值大于或等 于 a 的键值 etc get --from-key a 读取key字节值大于或等 于 a 的键值 etc watch test4 监听键值变化 etc watch test test7 按范围监听键值变化 etc watch -i 手工输入监视多个键值变化 etc watch ttt --rev=2 从某版本开始监视键值变化 etc watch ttt --prev-kv 监视键值变化变化时推送原值 etc compact 5 按照某个版本号进行压缩 这个版本号不是某个key的版本号,是整个wal日志的左引号 etc lease grant 10 创建一个租约 10s 有效 etc put test12 test12-7 --lease=694d7c2919403153 为键值设置租约 etc put test12 test12-7 --lease=694d7c2919403157 etc get test12 -w=json etc lease revoke 7587857463148294487 释放某个租约 etc lease grant 20 etc lease keep-alive 694d7c291940316e 自动续期某个租约 [root@localhost ~]# etc --endpoints=127.0.0.1:2379,127.0.0.1:2479,127.0.0.1:2479 endpoint status --write-out=table --查看集群状态 +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 127.0.0.1:2379 | c085e6085ca1ec17 | 3.5.1 | 20 kB | true | false | 2 | 9 | 9 | | | 127.0.0.1:2479 | bd20ac606784e77a | 3.5.1 | 20 kB | false | false | 2 | 9 | 9 | | | 127.0.0.1:2479 | bd20ac606784e77a | 3.5.1 | 20 kB | false | false | 2 | 9 | 9 | | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战