etcd入门之安装部署
2022-08-18 本文已影响0人
生饼
1 单点部署
1.1 安装
确认一下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.0-linux-amd64.tar.gz
,并建立相应的工作目录结构
$ mkdir etcd
$ cp etcd-v3.5.0-linux-amd64.tar.gz ./etcd
$ cd ./etcd
$ tar -xzf etcd-v3.5.0-linux-amd64.tar.gz
$ mkdir -p bin conf log data
$ cp etcd-v3.5.0-linux-amd64/etcd ./bin && cp etcd-v3.5.0-linux-amd64/etcdctl ./bin && cp etcd-v3.5.0-linux-amd64/etcdutl ./bin
查看一下etcd的版本
$ ./bin/etcd --version
etcd Version: 3.5.0
Git SHA: 946a5a6f2
Go Version: go1.16.3
Go OS/Arch: linux/amd64
1.2 启动
1.2.1 前台启动
只是简单临时测试试用,可以直接启动
$ ./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
# 接收客户端连接的服务端点
listen-client-urls: http://0.0.0.0:2379
./bin/etcd --config-file=./conf/etcd.yaml
1.2.2 后台启动
$ nohup ./bin/etcd --config-file=conf/etcd.yaml 2>&1 1>log/etcd.log &
1.2.3 systemd服务启动
简单测试时上面两种方式启动非常方便快捷,线上生产环境倾向于配置成系统服务
制作一个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=work
# 服务启动组
Group=work
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
1.2.4 配置logrotate
配置logrotate,对etcd日志文件进行切割,并限制日志文件最大大小
在/etc/logrotate.d/
目录下创建etcd
服务的logrotate
的配置文件:
/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
}
1.3 验证
查看启动的进程和端口
# 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
2 集群部署
假设集群有三个节点,服务ip分别为10.131.21.11
、10.131.21.12
、10.131.21.13
,为这三个节点分别创建相应的配置文件
2.1 节点配置
节点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
2.2 启动三个节点
$ ./bin/etcd --config-file=conf/etcd.yaml
2.3 验证
$ ./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 | |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+