安装部署etcd

环境:
OS:Centos 7
etcd:3.4.18
部署机器(3节点组成的集群)
192.168.1.134
192.168.1.135
192.168.1.136

##############################安装部署#######################################

1.下载
下载地址:
https://github.com/etcd-io/etcd/releases/tag/v3.4.18
选择相应的版本下载,我这里下载的是3.4.18这个版本
etcd-v3.4.18-linux-amd64.tar.gz

 

2.解压安装
每个节点上都需要执行
[root@host134 soft]# tar -xvf etcd-v3.4.18-linux-amd64.tar.gz
[root@host134 soft]# cd etcd-v3.4.18-linux-amd64
[root@host134 etcd-v3.4.18-linux-amd64]# ls
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
[root@host134 etcd-v3.4.18-linux-amd64]# cp etcd /usr/local/bin/
[root@host134 etcd-v3.4.18-linux-amd64]# cp etcdctl /usr/local/bin/

 

设置环境变量:
在/etc/profile文件底部添加一行
export PATH=/usr/local/bin:${PATH}
然后运行命令
source /etc/profile
生效环境变量

 

 

3.查看版本情况
每个节点上都需要执行
[root@host134 etcd-v3.4.18-linux-amd64]# etcd --version
etcd Version: 3.4.18
Git SHA: 72d3e382e
Go Version: go1.12.17
Go OS/Arch: linux/amd64
[root@host134 etcd-v3.4.18-linux-amd64]# etcdctl version
etcdctl version: 3.4.18
API version: 3.4

若系统找不到etcd和etcdctl两个脚本的话,需要设置环境变量

 

4.创建配置文件

每个节点都需要创建如下目录

mkdir -p /etc/etcd ##配置文件存放目录

mkdir -p /var/lib/etcd ##数据存放目录

192.168.1.134节点 配置文件

vi /etc/etcd/etcd.conf

ETCD_NAME=etcd1
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.134:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.134:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.134:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.134:2380,etcd2=http://192.168.1.135:2380,etcd3=http://192.168.1.136:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.134:2379"

192.168.1.135节点 配置文件

vi /etc/etcd/etcd.conf

ETCD_NAME=etcd2
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.135:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.135:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.135:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.134:2380,etcd2=http://192.168.1.135:2380,etcd3=http://192.168.1.136:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.135:2379"

 

192.168.1.136节点 配置文件

vi /etc/etcd/etcd.conf

ETCD_NAME=etcd3
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.136:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.136:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.136:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.134:2380,etcd2=http://192.168.1.135:2380,etcd3=http://192.168.1.136:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.136:2379"

 

4.设置启动

vi /usr/lib/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target

 

5.启动
每个节点都执行
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd

 

6.验证
在任意节点上查看ETCD成员:

[root@host134 etcd]# etcdctl member list
3e5aa50fd41b65e, started, etcd3, http://192.168.1.136:2380, http://192.168.1.136:2379, false
8d715818459cc60e, started, etcd2, http://192.168.1.135:2380, http://192.168.1.135:2379, false
aefed5149b767443, started, etcd1, http://192.168.1.134:2380, http://192.168.1.134:2379, false

查看那个是leader
[root@host134 etcd]# etcdctl -w table --endpoints=192.168.1.134:2379,192.168.1.135:2379,192.168.1.136:2379 endpoint status
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|      ENDPOINT      |        ID        | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 192.168.1.134:2379 | aefed5149b767443 |  3.4.18 |   20 kB |     false |      false |        67 |         11 |                 11 |        |
| 192.168.1.135:2379 | 8d715818459cc60e |  3.4.18 |   16 kB |      true |      false |        67 |         11 |                 11 |        |
| 192.168.1.136:2379 |  3e5aa50fd41b65e |  3.4.18 |   20 kB |     false |      false |        67 |         11 |                 11 |        |
+--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
[root@host134 etcd]# 


[root@host134 etcd]# etcdctl -w table --endpoints=192.168.1.134:2379,192.168.1.135:2379,192.168.1.136:2379 endpoint health
+--------------------+--------+-------------+-------+
|      ENDPOINT      | HEALTH |    TOOK     | ERROR |
+--------------------+--------+-------------+-------+
| 192.168.1.134:2379 |   true |   9.97441ms |       |
| 192.168.1.135:2379 |   true |  9.793468ms |       |
| 192.168.1.136:2379 |   true | 14.315624ms |       |
+--------------------+--------+-------------+-------+


查看当前节点的

[root@localhost etcd-v3.4.22-linux-amd64]# etcdctl -w table endpoint status
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 127.0.0.1:2379 | cec9e0ee849de346 | 3.4.22 | 20 kB | true | false | 3 | 9 | 9 | |
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+

 

 

7.数据验证

写入数据
在其中一个节点上执行
[root@host134 etcd]# etcdctl put mykey "this is etcd"
OK

每个节点都执行查看
[root@host134 etcd]# etcdctl get mykey
mykey
this is etcd


模糊查询,查看前缀是my的key
[root@host134 etcd]# etcdctl get --prefix my
mykey
this is etcd

查询所有
[root@host134 etcd]# etcdctl get --prefix ""
mykey
this is etcd

 

######################################备份恢复################################

1.在一个节点上进行备份

[root@host134 etcd]# etcdctl snapshot save /tmp/etcd_snapshot_bak.db
{"level":"info","ts":1668495247.221431,"caller":"snapshot/v3_snapshot.go:119","msg":"created temporary db file","path":"/tmp/etcd_snapshot_bak.db.part"}
{"level":"info","ts":"2022-11-15T14:54:07.222+0800","caller":"clientv3/maintenance.go:200","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":1668495247.2225058,"caller":"snapshot/v3_snapshot.go:127","msg":"fetching snapshot","endpoint":"127.0.0.1:2379"}
{"level":"info","ts":"2022-11-15T14:54:07.224+0800","caller":"clientv3/maintenance.go:208","msg":"completed snapshot read; closing"}
{"level":"info","ts":1668495247.2629094,"caller":"snapshot/v3_snapshot.go:142","msg":"fetched snapshot","endpoint":"127.0.0.1:2379","size":"20 kB","took":0.041318269}
{"level":"info","ts":1668495247.2631133,"caller":"snapshot/v3_snapshot.go:152","msg":"saved","path":"/tmp/etcd_snapshot_bak.db"}
Snapshot saved at /tmp/etcd_snapshot_bak.db

 

2.模拟删除所有的key
[root@host134 etcd]# etcdctl del --prefix ""
1

 

3.恢复
先停掉etcd
systemctl stop etcd

 

恢复快照(集群所有节点都需要操作,而且必须用备份文件)
将备份文件拷贝到其他的节点确保每个节点有该备份文件
scp /tmp/etcd_snapshot_bak.db root@192.168.1.135:/tmp/
scp /tmp/etcd_snapshot_bak.db root@192.168.1.136:/tmp/


清空该目录前清注意是否有备份,没有的话需要先备份该目录,防止还原失败.若不清除该目录,则还原操作会失败
新环境数据目录不存在的话,可以指定--data-dir进行恢复

192.168.1.134
[root@sc-master1 ~]# rm -rf /var/lib/etcd
在备份的节点上执行,因为需要读取备份文件

[root@host134 etcd]# etcdctl snapshot restore /tmp/etcd_snapshot_bak.db --data-dir=/var/lib/etcd/ --endpoints=192.168.1.134:2379
{"level":"info","ts":1668496070.055417,"caller":"snapshot/v3_snapshot.go:296","msg":"restoring snapshot","path":"/tmp/etcd_snapshot_bak.db","wal-dir":"/var/lib/etcd/member/wal","data-dir":"/var/lib/etcd/","snap-dir":"/var/lib/etcd/member/snap"}
{"level":"info","ts":1668496070.2232707,"caller":"membership/cluster.go:392","msg":"added member","cluster-id":"cdf818194e3a8c32","local-member-id":"0","added-peer-id":"8e9e05c52164694d","added-peer-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":1668496070.3535967,"caller":"snapshot/v3_snapshot.go:309","msg":"restored snapshot","path":"/tmp/etcd_snapshot_bak.db","wal-dir":"/var/lib/etcd/member/wal","data-dir":"/var/lib/etcd/","snap-dir":"/var/lib/etcd/member/snap"}

 

192.168.1.135
rm -rf /var/lib/etcd
etcdctl snapshot restore /tmp/etcd_snapshot_bak.db --data-dir=/var/lib/etcd/ --endpoints=192.168.1.135:2379


192.168.1.136
rm -rf /var/lib/etcd
etcdctl snapshot restore /tmp/etcd_snapshot_bak.db --data-dir=/var/lib/etcd/ --endpoints=192.168.1.136:2379

启动
每个节点都相应的启动
systemctl start etcd

 

-- The End --

posted @ 2022-11-15 16:06  slnngk  阅读(235)  评论(0编辑  收藏  举报