etcd单点部署集群及数据备份与恢复

etcd单机集群部署

下载etcd 二进制安装包。
https://github.com/etcd-io/etcd/releases
创建相关目录
mkdir –p /data/etcd/node1
mkdir –p /data/etcd/node2
mkdir –p /data/etcd/node3

在 /data/etcd/node1,/data/etcd/node2,/data/etcd/node3创建三个配置文件

node1.yml
name: node1
data-dir: /data/etcd/node1 
listen-client-urls: 'http://192.168.73.222:9002'
advertise-client-urls: 'http://192.168.73.222:9002'
listen-peer-urls: 'http://192.168.73.222:9001'
initial-advertise-peer-urls: 'http://192.168.73.222:9001'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node2.yml
name: node2
data-dir: /data/etcd/node2 
listen-client-urls: 'http://192.168.73.222:9004'
advertise-client-urls: 'http://192.168.73.222:9004'
listen-peer-urls: 'http://192.168.73.222:9003'
initial-advertise-peer-urls: 'http://192.168.73.222:9003'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node3.yml
name: node3
data-dir: /data/etcd/node3
listen-client-urls: 'http://192.168.73.222:9006'
advertise-client-urls: 'http://192.168.73.222:9006'
listen-peer-urls: 'http://192.168.73.222:9005'
initial-advertise-peer-urls: 'http://192.168.73.222:9005'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new

配置文件参数解析

name: node3
data-dir: /data/etcd/node3
listen-client-urls: 'http://192.168.73.222:9006'
advertise-client-urls: 'http://192.168.73.222:9006'
listen-peer-urls: 'http://192.168.73.222:9005'
initial-advertise-peer-urls: 'http://192.168.73.222:9005'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new

启动3个节点

nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node1/node1.yml >> /data/etcd/node1.log &
nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node2/node2.yml >> /data/etcd/node2.log &
nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node3/node3.yml >> /data/etcd/node3.log &

查看集群状态

/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 member list
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006  endpoint health
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006  endpoint status

etcd备份脚本

# etcd-bak.sh
#!/usr/bin/env bash

date;
ENDPOINTS="http://192.168.73.222:9001"
ETCDCTL_API=3 /data/etcd/etcd/etcdctl \
--endpoints=${ENDPOINTS} \
snapshot save /data/etcdbak/etcd-data-`date +%Y%m%d%H%M%S`.db

# 备份保留30天
find /data/etcdbak/ -name *.db -mtime +30 -exec rm -f {} \;

etcd数据恢复

停止etcd
ps -ef |grep etcd|grep -v grep|awk '{print $2}'|xargs kill -9
导入数据,如下
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db  \
--name node1 \
--data-dir /data/etcd/node11  \
--initial-advertise-peer-urls http://192.168.73.222:9001 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db  \
--name node2 \
--data-dir /data/etcd/node22  \
--initial-advertise-peer-urls http://192.168.73.222:9003 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db  \
--name node3 \
--data-dir /data/etcd/node33  \
--initial-advertise-peer-urls http://192.168.73.222:9005 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 
更改启动文件中data-dir为导入时的目录
node1.yml
name: node1
data-dir: /data/etcd/node11 
listen-client-urls: 'http://192.168.73.222:9002'
advertise-client-urls: 'http://192.168.73.222:9002'
listen-peer-urls: 'http://192.168.73.222:9001'
initial-advertise-peer-urls: 'http://192.168.73.222:9001'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node2.yml
name: node2
data-dir: /data/etcd/node22 
listen-client-urls: 'http://192.168.73.222:9004'
advertise-client-urls: 'http://192.168.73.222:9004'
listen-peer-urls: 'http://192.168.73.222:9003'
initial-advertise-peer-urls: 'http://192.168.73.222:9003'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node3.yml
name: node3
data-dir: /data/etcd/node33
listen-client-urls: 'http://192.168.73.222:9006'
advertise-client-urls: 'http://192.168.73.222:9006'
listen-peer-urls: 'http://192.168.73.222:9005'
initial-advertise-peer-urls: 'http://192.168.73.222:9005'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
启动etcd
nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node1/node1.yml >> /data/etcd/node1.log &
nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node2/node2.yml >> /data/etcd/node2.log &
nohup /data/etcd/etcd/etcd  --config-file=/data/etcd/node3/node3.yml >> /data/etcd/node3.log &
查看集群状态
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 member list
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006  endpoint health
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006  endpoint status
查看etcd中的key值
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 get  --prefix / |wc -l

你可能感兴趣的:(ETCD,etcd,数据库)