etcd 是一个分布式的、高可用的、一致的 key-value 存储数据库,基于 Go 语言实现,主要用于共享配置和服务发现。
在分布式系统中,各种服务配置信息的管理共享和服务发现是一个很基本也是很重要的问题。etcd 可集中管理配置信息,服务端将配置信息存储于 etcd,客户端通过 etcd 得到服务配置信息,etcd 监听配置信息的改变,发现改变通知客户端。
为了防止单点故障,还可启动多个 etcd 组成集群。etcd 集群使用 raft 一致性算法处理日志复制,保证多节点数据的强一致性。
首先确认一下 linux 操作系统内核版本,版本号需要大于 3.10
$ uname -a
Linux 4.14.0_1-0-0-45 #2 SMP Tue Oct 19 18:27:28 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
直接从官网下载相应的二进制文件包 etcd-v3.5.11-linux-amd64.tar.gz,并建立相应的工作目录结构
https://github.com/etcd-io/etcd/releases/tag/v3.5.11
$ mkdir etcd
$ cp etcd-v3.5.11-linux-amd64.tar.gz ./etcd
$ cd ./etcd
$ tar -xzf etcd-v3.5.11-linux-amd64.tar.gz
$ mkdir -p bin conf log data
$ cp etcd-v3.5.11-linux-amd64/etcd ./bin && cp etcd-v3.5.11-linux-amd64/etcdctl ./bin && cp etcd-v3.5.11-linux-amd64/etcdutl ./bin
查看一下 etcd 的版本
$ ./bin/etcd --version
etcd Version: 3.5.11
Git SHA: 946a5a6f2
Go Version: go1.20.12
Go OS/Arch: linux/amd64
只是简单临时测试试用,可以直接启动
$ ./bin/etcd
也可以指定启动参数, –name 为节点取一个名字,–log-level 指定打印日志的级别
$ ./bin/etcd --name=etcd-cnlab0 --log-level=info
启动参数也可以放到配置文件中进行统一管理,然后通过 –config-file 启动参数指定启动的配置文件
$ cat conf/etcd.yaml
# 节点名字
name: etcd-cnlab0
# etcd数据存储目录
data-dir: ./data
# 日志存储路径
log-outputs: ['./log/etcd.log']
# 接收客户端连接的服务端点
listen-client-urls: http://0.0.0.0:2379
./bin/etcd --config-file=./conf/etcd.yaml
后台启动
$ nohup ./bin/etcd --config-file=conf/etcd.yaml 2>&1 1>log/etcd.log &
访问:http://你的服务器IP:2379/version 可以返回 ETCD 的版本
简单测试时上面两种方式启动非常方便快捷,线上生产环境倾向于配置成系统服务
制作一个 etcd.service 文件:
$ sudo su -
# cd /usr/lib/systemd/system
# vim etcd.service
[Unit]
Description=Etcd Server
# 启动etcd服务后,需要启动network.target服务
After=network.target
[Service]
# 服务类型
Type=simple
# 工作目录
WorkingDirectory=/home/work/etcd
# 服务启动目录
ExecStart=/home/work/etcd/bin/etcd --config-file=/home/work/etcd/conf/etcd.yaml
# 服务启动用户名
User=root
# 服务启动组
Group=root
PIDFile=/home/work/etcd/etcd.pid
# 服务失败后重启服务
Restart=on-failure
# 服务运行打开的文件句柄数
LimitNOFILE=65536
[Install]
# 系统以多用户模式启动时自动启动本服务
WantedBy=multi-user.target
然后可以使用 systemctl 系统命令管理了 etcd 服务了
# 查看文件内容
# systemctl cat etcd.service
# 服务还未使能
# systemctl list-unit-files --type=service | grep etcd
etcd.service disabled
# 重新加载服务文件
# systemctl daemon-reload
# 使能自启动
# systemctl enable etcd.service
# 启动服务
# systemctl start etcd.service
其他的几个管理命令
# 关闭服务
# systemctl stop etcd.service
# 重启服务
# systemctl restart etcd.service
# 显示服务的状态
# systemctl status etcd.service
# 在开机时不启动服务
# systemctl disable etcd.service
# 查看服务是否开机启动
# systemctl is-enabled etcd.service
# 查看启动失败的服务
# systemctl --failed | grep etcd
logrotate 是 centos 自带命令,其他 linux 操作系统可能需要自行安装,用来进行日志切割和定期删除,简单来说就是将某个日志文件按照时间或大小分割成多份,删除时间久远的日志。
配置 logrotate,对 etcd 日志文件进行切割,并限制日志文件最大大小
在 /etc/logrotate.d/ 目录下创建 etcd 服务的 logrotate 的配置文件:
vi /etc/logrotate.d/etcd
/home/work/etcd/log/*.log {
# 切割周期为天
daily
# 最多保留10个文件
rotate 10
# 切割后的文件名添加日期后缀
dateext
# 日期格式
dateformat -%Y%m%d
# 切割后文件的后缀
extension log
# 如果日志文件不存在,不报错
missingok
# 日志文件大小为50M,超过50M后进行切割
size 50M
compress
delaycompress
notifempty
# 文件权限,user,group
create 0644 work work
sharedscripts
# 切割后执行的命令,让etcd重新加载配置
postrotate
/usr/bin/killall -HUP etcd
endscript
}
查看启动的进程和端口
# ps -ef | grep etcd
work 3000 25754 0 15:14 pts/0 00:00:00 ./bin/etcd --name=etcd-cnlab0 --log-level=info
# lsof -i :2379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
etcd 3000 work 8u IPv4 2434566770 0t0 TCP localhost:2379 (LISTEN)
etcd 3000 work 13u IPv4 2434581997 0t0 TCP localhost:59605->localhost:2379 (ESTABLISHED)
etcd 3000 work 14u IPv4 2434569656 0t0 TCP localhost:2379->localhost:59605 (ESTABLISHED)
# lsof -i :2380
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
etcd 3000 work 7u IPv4 2434566767 0t0 TCP localhost:2380 (LISTEN)
查看服务端点状态
$ ./bin/etcdctl endpoint status --cluster -w table
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://localhost:2379 | 8e9e05c52164694d | 3.5.0 | 20 kB | true | false | 2 | 4 | 4 | |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
查看服务端点健康情况
$ ./bin/etcdctl endpoint health --cluster -w table
+-----------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+-----------------------+--------+------------+-------+
| http://localhost:2379 | true | 2.059608ms | |
+-----------------------+--------+------------+-------+
查看成员
$ ./bin/etcdctl member list
8e9e05c52164694d, started, etcd-cnlab0, http://localhost:2380, http://localhost:2379, false
写入和读取数据
$ ./bin/etcdctl put key1 hello
OK
$ ./bin/etcdctl --endpoints=http://127.0.0.1:2379 get key1
key1
hello
假设集群有三个节点,服务 ip 分别为 10.131.21.11、10.131.21.12、10.131.21.13,为这三个节点分别创建相应的配置文件
节点1(10.131.21.11)配置文件内容:
$ cat conf/etcd.yaml
# 节点名字,集群内唯一
name: etcd-cnlab0
# 数据存储目录
data-dir: ./data
# 日志文件配置
# logger: zap
log-outputs: ['./log/etcd.log']
# log-level: info
# 客户端连接相关配置
# 服务监听端点
listen-client-urls: http://0.0.0.0:2379
# 向客户端发布的服务端点
advertise-client-urls: http://10.131.21.11:2379
# 集群相关配置
# 监听集群其他节点连接的端点
listen-peer-urls: http://0.0.0.0:2380
# 向集群其他节点发布的服务端点
initial-advertise-peer-urls: http://10.131.21.11:2380
# 集群成员的名字以及服务端点列表,名字与每个节点配置的name字段值对应
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
# 集群标识token,可以认为是集群名字
initial-cluster-token: etcd-cnlab
# 创建一个新的集群。如果data-dir目录下的数据属于另外一个集群,则无法启动
inital-cluster-state: new
节点 2(10.131.21.12)配置文件内容:
$ cat conf/etcd.yaml
name: etcd-cnlab1
data-dir: ./data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.131.21.12:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.131.21.12:2380
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
initial-cluster-token: etcd-cnlab
inital-cluster-state: new
节点 3(10.131.21.13)配置文件内容:
$ cat conf/etcd.yaml
name: etcd-cnlab2
data-dir: ./data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.131.21.13:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.131.21.13:2380
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
initial-cluster-token: etcd-cnlab
inital-cluster-state: new
启动三个节点
$ ./bin/etcd --config-file=conf/etcd.yaml
验证
$ ./bin/etcdctl endpoint health --cluster -w table
+--------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+--------------------------+--------+------------+-------+
| http://10.131.21.11:2379 | true | 1.405365ms | |
| http://10.131.21.12:2379 | true | 2.106704ms | |
| http://10.131.21.13:2379 | true | 2.257057ms | |
+--------------------------+--------+------------+-------+
$ ./bin/etcdctl member list
2d3298f7865acc32, started, etcd-cnlab1, http://10.131.21.12:2380, http://10.131.21.12:2379, false
71e4f8695d490d37, started, etcd-cnlab0, http://10.131.21.11:2380, http://10.131.21.11:2379, false
c2a413a1e752b148, started, etcd-cnlab2, http://10.131.21.13:2380, http://10.131.21.13:2379, false
$ ./bin/etcdctl endpoint status --cluster -w table
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://10.131.21.12:2379 | 2d3298f7865acc32 | 3.5.0 | 20 kB | false | false | 2 | 10 | 10 | |
| http://10.131.21.11:2379 | 71e4f8695d490d37 | 3.5.0 | 20 kB | true | false | 2 | 10 | 10 | |
| http://10.131.21.13:2379 | c2a413a1e752b148 | 3.5.0 | 20 kB | false | false | 2 | 10 | 10 | |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
etcd入门之安装部署 - 简书 https://www.jianshu.com/p/56df66161488
Install | etcd https://etcd.io/docs/v3.5/install/
etcd简单介绍 - 疯一样的狼人 - 博客园 https://www.cnblogs.com/wujuntian/p/12837926.html