ETCD 集群搭建-静态配置方式
参考1:https://zhuanlan.zhihu.com/p/405811320
参考2:https://blog.csdn.net/liuhuayang/article/details/122834141
参考3:https://blog.51cto.com/mingongge/2982443
ETCD 启动相关参数:
—data-dir 指定节点的数据存储目录,这些数据包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未指定—wal-dir,还会存储WAL文件;
—wal-dir 指定节点的was文件的存储目录,若指定了该参数,wal文件会和其他数据文件分开存储。
—name 节点名称
—initial-advertise-peer-urls 告知集群其他节点url.
— listen-peer-urls 监听URL,用于与其他节点通讯
— advertise-client-urls 告知客户端url, 也就是服务的url
— initial-cluster-token 集群的ID
— initial-cluster 集群中所有节点
ETCD下载:
github: https://github.com/etcd-io/etcd/releases/tag/v3.5.4
-rw-r--r-- 1 root root 19432359 May 17 07:05 etcd-v3.5.4-linux-amd64.tar.gz
集群安装:
节点信息:
hostname ip
etcd1 192.168.123.160
etcd2 192.168.123.161
etcd3 192.168.123.162
测试环境避免带来不必要的困扰,请先关闭防火墙、SELinux、如果之前使用过 iptables 请检查 iptables 规则,如汝不懂 iptables 可直接跳过。关闭 防火墙、SELinux 具体操作请找隔壁老王。
配置主机名和/etc/hosts: 非必须步骤,只是个人习惯,方便后续操作而已;
1 2 | # 设置主机名 三台主机均设置 hostnamectl set - hostname etcd1 |
hosts文件配置 三台主机配置一致
1 2 3 4 5 6 7 8 | [root@etcd1 etcd-v3.5.4-linux-amd64] # cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.123.160 etcd1 192.168.123.161 etcd2 192.168.123.162 etcd3 |
在etcd1节点配置密钥,将其它两个节点设置密钥登录:非必须
生成密钥:ssh-keygen
将密钥拷贝其它节点:ssh-copy-id
具体操作,找隔壁老王。
重点:任何集群在配置前,请一定先同步好集群节点的时间,避免入坑。
操作节点 etcd1 解压安装包:
1 | ] # tar -xzvf etcd-v3.5.4-linux-amd64.tar.gz |
操作节点 etcd1 修改文件及文件夹的属主属组:此处不讨论权限最小安全运行,只是测试环境运行体验
1 | ] # chown -R root:root ./etcd-v3.5.4-linux-amd64 |
操作节点 etcd1 安装:
1 | ] # cp -a ./etcd* /usr/bin/ |
操作节点 etcd1 其它节点安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@etcd1 etcd-v3.5.4-linux-amd64] # scp ./etcd* etcd2:/usr/bin/ etcd 100% 22MB 110.4MB /s 00:00 etcdctl 100% 17MB 132.4MB /s 00:00 etcdutl 100% 15MB 121.8MB /s 00:00 [root@etcd1 etcd-v3.5.4-linux-amd64] # scp ./etcd* etcd3:/usr/bin/ etcd 100% 22MB 124.7MB /s 00:00 etcdctl 100% 17MB 134.7MB /s 00:00 etcdutl 100% 15MB 114.9MB /s 00:00 [root@etcd1 etcd-v3.5.4-linux-amd64] # ssh etcd2 "etcd --version" etcd Version: 3.5.4 Git SHA: 08407ff76 Go Version: go1.16.15 Go OS /Arch : linux /amd64 [root@etcd1 etcd-v3.5.4-linux-amd64] # ssh etcd3 "etcd --version" etcd Version: 3.5.4 Git SHA: 08407ff76 Go Version: go1.16.15 Go OS /Arch : linux /amd64 |
创建ETCD数据存储目录:所有节点
1 | ] # mkdir -p /data/etcdData |
配置service文件:etcd1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@etcd1 system] # cat /usr/lib/systemd/system/etcd.service [Unit] Description=etcd service Documentation=https: //github .com /coreos/etcd [Service] User=root Type=notify ExecStart= /usr/bin/etcd \ --name etcd1 \ --data- dir /data/etcdData \ --initial-advertise-peer-urls http: //192 .168.123.160:2380 \ --listen-peer-urls http: //192 .168.123.160:2380 \ --listen-client-urls http: //192 .168.123.160:2379,http: //127 .0.0.1:2379 \ --advertise-client-urls http: //192 .168.123.160:2379 \ --initial-cluster-token etcd-cluster \ --initial-cluster etcd1=http: //192 .168.123.160:2380,etcd2=http: //192 .168.123.161:2380,etcd3=http: //192 .168.123.162:2380 \ --initial-cluster-state new \ --heartbeat-interval 1000 \ --election-timeout 5000 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target |
etcd2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@etcd2 ~] # cat /usr/lib/systemd/system/etcd.service [Unit] Description=etcd service Documentation=https: //github .com /coreos/etcd [Service] User=root Type=notify ExecStart= /usr/bin/etcd \ --name etcd2 \ --data- dir /data/etcdData \ --initial-advertise-peer-urls http: //192 .168.123.161:2380 \ --listen-peer-urls http: //192 .168.123.161:2380 \ --listen-client-urls http: //192 .168.123.161:2379,http: //127 .0.0.1:2379 \ --advertise-client-urls http: //192 .168.123.161:2379 \ --initial-cluster-token etcd-cluster \ --initial-cluster etcd1=http: //192 .168.123.160:2380,etcd2=http: //192 .168.123.161:2380,etcd3=http: //192 .168.123.162:2380 \ --initial-cluster-state new \ --heartbeat-interval 1000 \ --election-timeout 5000 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target |
etcd3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@etcd3 ~] # cat /usr/lib/systemd/system/etcd.service [Unit] Description=etcd service Documentation=https: //github .com /coreos/etcd [Service] User=root Type=notify ExecStart= /usr/bin/etcd \ --name etcd3 \ --data- dir /data/etcdData \ --initial-advertise-peer-urls http: //192 .168.123.162:2380 \ --listen-peer-urls http: //192 .168.123.162:2380 \ --listen-client-urls http: //192 .168.123.162:2379,http: //127 .0.0.1:2379 \ --advertise-client-urls http: //192 .168.123.162:2379 \ --initial-cluster-token etcd-cluster \ --initial-cluster etcd1=http: //192 .168.123.160:2380,etcd2=http: //192 .168.123.161:2380,etcd3=http: //192 .168.123.162:2380 \ --initial-cluster-state new \ --heartbeat-interval 1000 \ --election-timeout 5000 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target |
注意:所有节点在修改、新建service文件后都需要执行 systemctl daemon-reload
启动:
先启动 etcd1:systemctl start etcd.service 注意:不用管状态,启动命令执行后会卡住一会,可以先不用管,接着启动第二个节点即可;
启动 etcd2:systemctl start etcd.service
启动 etcd3:systemctl start etcd.service
如果 etcd1 报异常,执行 systemctl restart etcd.service 即可
弊端:此种方式将集群配置信息写入service文件,在集群之启动一个节点的时候,etcd是已经启动了,但是集群相关信息则是失败的,导致 systemctl 返回是失败的,此时端口已经监听,可以继续启动其它节点,待其它节点启动成功后,则可以返回第一个节点,重启下第一个节点的服务即可。
验证:
1 2 3 4 5 6 7 8 | [root@etcd1 system] # etcdctl member list --write-out=table +------------------+---------+-------+-----------------------------+-----------------------------+------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER | +------------------+---------+-------+-----------------------------+-----------------------------+------------+ | 2f107ed12dcf060d | started | etcd2 | http: //192 .168.123.161:2380 | http: //192 .168.123.161:2379 | false | | 4f36d86f7d67fa14 | started | etcd1 | http: //192 .168.123.160:2380 | http: //192 .168.123.160:2379 | false | | 89d28152c949347f | started | etcd3 | http: //192 .168.123.162:2380 | http: //192 .168.123.162:2379 | false | +------------------+---------+-------+-----------------------------+-----------------------------+------------+ |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」