如何把ETCD的数据库备份,以及还原的操作方法(待更新中)
地址:
Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障。为了节省机器,这里把3个ETCD实例分别部署在一个Matser节点和两个Node节点上。
ETCD实例 IP
etcd-1 172.23.199.15
etcd-2 172.23.199.16
etcd-3 172.23.199.17
1.准备cfssl证书生成工具
# 安装cfssl证书生成工具(如果下载不了,可尝试换成http协议)
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
2.生成ETCD证书
# 生成自签证书的根证书:创建文件夹
mkdir -p ~/TLS/{etcd,k8s}
cd TLS/etcd
# 生成自签证书的根证书:文件夹中写入配置文件2个
# 配置文件1
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
# 配置文件2
cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
# 执行如下指令生成证书
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
接下来,使用自签的根证书签发ETCD HTTPS证书
#5、创建证书申请文件
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"172.23.199.15",
"172.23.199.16",
"172.23.199.17"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注意:上面配置文件中hosts字段中的IP为所有etcd节点的集群内部通信IP,为了方便后期扩容可以多写几个预留IP
# 生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
3.下载ETCD的二进制文件
https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
4.部署ETCD集群
以下操作只在节点1(Master)上进行一遍,至于节点2(node01)、节点3(node02),直接从节点1中把结果拷贝过去即可。
1)创建工作目录并解压二进制包
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
2)创建ETCD配置文件
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://172.23.199.15:2380"
ETCD_LISTEN_CLIENT_URLS="https://172.23.199.15:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.23.199.15:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://172.23.199.15:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://172.23.199.15:2380,etcd-2=https://172.23.199.16:2380,etcd-3=https://172.23.199.17:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
配置文件中各个参数含义如下:
#ETCD_NAME:节点名称,集群中唯一
#ETCD_DATA_DIR:数据目录
#ETCD_LISTEN_PEER_URLS:集群通信监听地址
#ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
#ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址
#ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
#ETCD_INITIAL_CLUSTER:集群节点地址
#ETCD_INITIAL_CLUSTER_TOKEN:集群Token
#ETCD_INITIAL_CLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
3)systemd管理etcd
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
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
4)拷贝证书到配置文件路径下
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
5)启动,并设置开机启动
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
注意:systemctl start etcd 指令执行之后会“卡住”因为其他的ETCD节点还没部署呢。还有防火墙需要把 2379
和 2380
的端口开通起来,否则执行启动etcd服务也会卡住的。
输入以下的指令可以查看日志:
#把和etcd有关的日志保存到a文件中,再从a中检索和ip相关的内容,下图截取了其中的一部分
#注意:本步骤不是必须要执行的
journalctl -u etcd > a
cat a grep|172.23.199.15
6)将上述节点1所有生成的文件拷贝到节点2和节点3
#分别拷贝etcd的工作目录、管理服务的配置文件到另外两台主机
scp -r /opt/etcd/ root@k8s-node01:/opt/
scp /usr/lib/systemd/system/etcd.service root@k8s-node01:/usr/lib/systemd/system/
scp -r /opt/etcd/ root@k8s-node02:/opt/
scp /usr/lib/systemd/system/etcd.service root@k8s-node02:/usr/lib/systemd/system/
注意:需要修改ETCD配置文件参数(共5处修改点),分别修改另外2个ETCD的参数
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" # 修改1,节点2改为etcd-2,节点3改为etcd-3
ETCD_LISTEN_PEER_URLS="https://172.23.199.15:2380" # 修改2 当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://172.23.199.15:2379" # 修改3 当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.23.199.15:2380" # 修改4 当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://172.23.199.15:2379" # 修改5 当前服务器IP
启动并设置开机启动(同上):
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
7)查看集群状态
在任意一个ETCD节点上输入如下指令,可以查看集群状态情况true表示健康(保证IP正确)
ETCDCTL_API=3 /opt/etcd/bin/etcdctl \
--cacert=/opt/etcd/ssl/ca.pem \
--cert=/opt/etcd/ssl/server.pem \
--key=/opt/etcd/ssl/server-key.pem \
--endpoints="https://172.23.199.15:2379,https://172.23.199.16:2379,https://172.23.199.17:2379" endpoint health \
--write-out=table
显示结果:
+----------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+----------------------------+--------+------------+-------+
| https://172.23.199.15:2379 | true | 6.801153ms | |
| https://172.23.199.16:2379 | true | 5.66978ms | |
| https://172.23.199.17:2379 | true | 5.644431ms | |
+----------------------------+--------+------------+-------+
网友评论