欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

etcd 3.5.0 版群集部署和备份

最编程 2024-05-03 13:05:34
...

1,环境配置

https://github.com/etcd-io/etcd/releases/tag/v3.5.0

etcd版本:etcd-v3.5.0
集群ip:
etcd-1:192.168.44.218
etcd-2:192.168.6.166
etcd-3:192.168.44.210

2,ETCD下载

$ wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz
$ tar -zxvf  etcd-v3.5.0-linux-amd64.tar.gz -C /beta/etcd
$ cd /beta/etcd
$ mv etcd-v3.5.0-linux-amd64  etcd-v3.5.0
$ mkdir /etc/etcd           # 创建etcd配置文件目录
$ chown root.root etcd-v3.5.0 -R
开启防火墙端口
firewall-cmd --add-port=2379/tcp --permanent --zone=public
firewall-cmd --add-port=2380/tcp --permanent --zone=public
firewall-cmd --reload

3,创建etcd配置文件:

$ vi /etc/etcd/conf.yml

节点1,添加如下内容:
cat >/etc/etcd/conf.yml<<EOF
name: etcd-1
data-dir: /beta/etcd/data
listen-client-urls: http://192.168.44.218:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.44.218:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.44.218:2380
initial-advertise-peer-urls: http://192.168.44.218:2380
initial-cluster: etcd-1=http://192.168.44.218:2380,etcd-2=http://192.168.6.166:2380,etcd-3=http://192.168.44.210:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
EOF

节点2,添加如下内容:
cat >/etc/etcd/conf.yml<<EOF
name: etcd-2
data-dir: /beta/etcd/data
listen-client-urls: http://192.168.6.166:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.6.166:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.6.166:2380
initial-advertise-peer-urls: http://192.168.6.166:2380
initial-cluster: etcd-1=http://192.168.44.218:2380,etcd-2=http://192.168.6.166:2380,etcd-3=http://192.168.44.210:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
EOF

节点3,添加如下内容:
cat >/etc/etcd/conf.yml<<EOF
name: etcd-3
data-dir: /beta/etcd/data
listen-client-urls: http://192.168.44.210:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.44.210:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.44.210:2380
initial-advertise-peer-urls: http://192.168.44.210:2380
initial-cluster: etcd-1=http://192.168.44.218:2380,etcd-2=http://192.168.6.166:2380,etcd-3=http://192.168.44.210:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
EOF

启动命令:
$ ./etcd --config-file=/etc/etcd/conf.yml

4,配置ETCD为启动服务


编辑/usr/lib/systemd/system/etcd.service,添加下面内容:
cat >/usr/lib/systemd/system/etcd.service<<EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/beta/etcd/etcd-v3.5.0
# User=etcd
ExecStart=/beta/etcd/etcd-v3.5.0/etcd --config-file=/etc/etcd/conf.yml
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

5,更新启动:

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd

systemctl status etcd.service -l

6,API 3 备份与恢复方法

在命令行设置:
# export ETCDCTL_API=3

备份数据:
# etcdctl --endpoints localhost:2379 snapshot save snapshot.db

恢复:
# etcdctl snapshot restore snapshot.db --name m3 --data-dir=/home/etcd_data
恢复后的文件需要修改权限为 etcd:etcd
--name:重新指定一个数据目录,可以不指定,默认为 default.etcd
--data-dir:指定数据目录
建议使用时不指定 name 但指定 data-dir,并将 data-dir 对应于 etcd 服务中配置的 data-dir
更新etcd系统默认配置:
当前使用的是etcd v3版本,系统默认的是v2,通过下面命令修改配置。

$ vi /etc/profile
在末尾追加
export ETCDCTL_API=3
$ source /etc/profile

ETCD命令
$ cd /beta/etcd/etcd-v3.5.0

查看当前etcd版本:
$ ./etcdctl version
etcdctl version: 3.5.0
API version: 3.5

常用命令
export ETCDCTL_API=3
ETCD_VER=v3.5.0
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379   endpoint status
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379   endpoint health
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379  endpoint hashkv
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379  check perf
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379  check datascale
./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379  member list


查看集群成员信息:
export ETCDCTL_API=3
查看选举状态:
 ./etcdctl --endpoints=192.168.44.218:2379,192.168.6.166:2379,192.168.44.210:2379   endpoint status
192.168.44.218:2379, d0b6669beaefa1b1, 3.5.0, 63 MB, true, false, 5, 29022, 29022, 
192.168.6.166:2379, 4a53660b08484e27, 3.5.0, 63 MB, false, false, 5, 29022, 29022, 
192.168.44.210:2379, 61639fbcc5571efc, 3.5.0, 63 MB, false, false, 5, 29022, 29022, 


ECTD读写操作
基于HTTP协议的API使用起来比较简单,这里主要通过etcdctl和curl两种方式来做简单介绍。