Etcd BackUp

select & delete

1
2
3
4
5
6
7
ETCDCTL_API=3

# 查询key
etcdctl get /  --prefix --keys-only

# 删除key
etcdctl del /registry/pods/default/nginx-deployment-6795df67f9-p4xqh

查看集群成员

1
2
3
4
5
ETCDCTL_API=3 \
etcdctl  --cacert=/etc/kubernetes/ssl/ca.pem \
--cert=/etc/etcd/ssl/etcd.pem \
--key=/etc/etcd/ssl/etcd-key.pem \
--endpoints="https://172.18.247.200:2379"   member list

Etcd备份

1
2
3
4
5
ETCDCTL_API=3 \
etcdctl --cacert=/etc/kubernetes/ssl/ca.pem \
--cert=/etc/etcd/ssl/etcd.pem \
--key=/etc/etcd/ssl/etcd-key.pem \
--endpoints=https://172.18.247.200:2379  snapshot save  Etcd-$(date +%Y-%m-%d).db

Etcd 单节点恢复

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    ETCDCTL_API=3 etcdctl snapshot restore  Etcd-2019-08-21.db \
    --endpoints=172.18.247.200:2379 \
    --name=etcd1 \
    --cacert=/etc/kubernetes/ssl/ca.pem \
    --cert=/etc/etcd/ssl/etcd.pem \
    --key=/etc/etcd/ssl/etcd-key.pem \
    --initial-advertise-peer-urls=https://172.18.247.200:2380 \
    --initial-cluster-token=etcd-cluster-0 \
    --initial-cluster=etcd1=https://172.18.247.200:2380 \
    --data-dir=/var/lib/etcd

Etcd 集群恢复

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    ETCDCTL_API=3 etcdctl snapshot restore  Etcd-2019-08-21.db \
    --endpoints=172.18.247.200:2379 \
    --name=etcd1 \
    --cacert=/etc/kubernetes/ssl/ca.pem \
    --cert=/etc/etcd/ssl/etcd.pem \
    --key=/etc/etcd/ssl/etcd-key.pem \
    --initial-advertise-peer-urls=https://172.18.247.200:2380 \
    --initial-cluster-token=etcd-cluster-0 \
    --initial-cluster=etcd1=https://172.18.247.200:2380,etcd2=https://172.18.247.201:2380,etcd3=https://172.18.247.202:2380 \
    --data-dir=/var/lib/etcd
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    ETCDCTL_API=3 etcdctl snapshot restore  Etcd-2019-08-21.db \
    --endpoints=172.18.247.201:2379 \
    --name=etcd2 \
    --cacert=/etc/kubernetes/ssl/ca.pem \
    --cert=/etc/etcd/ssl/etcd.pem \
    --key=/etc/etcd/ssl/etcd-key.pem \
    --initial-advertise-peer-urls=https://172.18.247.201:2380 \
    --initial-cluster-token=etcd-cluster-0 \
    --initial-cluster=etcd1=https://172.18.247.200:2380,etcd2=https://172.18.247.201:2380,etcd3=https://172.18.247.202:2380 \
    --data-dir=/var/lib/etcd
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    ETCDCTL_API=3 etcdctl snapshot restore  Etcd-2019-08-21.db \
    --endpoints=172.18.247.202:2379 \
    --name=etcd3 \
    --cacert=/etc/kubernetes/ssl/ca.pem \
    --cert=/etc/etcd/ssl/etcd.pem \
    --key=/etc/etcd/ssl/etcd-key.pem \
    --initial-advertise-peer-urls=https://172.18.247.202:2380 \
    --initial-cluster-token=etcd-cluster-0 \
    --initial-cluster=etcd1=https://172.18.247.200:2380,etcd2=https://172.18.247.201:2380,etcd3=https://172.18.247.202:2380 \
    --data-dir=/var/lib/etcd

查看集群状态

1
2
3
4
5
6
ETCDCTL_API=3 \
etcdctl  --write-out=table \
--cacert=/etc/kubernetes/ssl/ca.pem \
--cert=/etc/etcd/ssl/etcd.pem \
--key=/etc/etcd/ssl/etcd-key.pem \
--endpoints="https://172.18.247.200:2379,etcd2=https://172.18.247.201:2379,etcd3=https://172.18.247.202:2379"  endpoint status
1
2
3
4
5
6
7
+-------------------------+------------------+---------+---------+-----------+-----------+------------+
|        ENDPOINT         |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+-------------------------+------------------+---------+---------+-----------+-----------+------------+
| https://172.18.247.200:2379 | 3582869f1611cb8e |  3.3.10 |  6.8 MB |     false |         3 |    1593504 |
| https://172.18.247.201:2379 | 7bfaa404fb41a6b7 |  3.3.10 |  6.8 MB |     false |         3 |    1593504 |
| https://172.18.247.202:2379 | d4eb4fd0e7ee1fa7 |  3.3.10 |  6.8 MB |      true |         3 |    1593504 |
+-------------------------+------------------+---------+---------+-----------+-----------+------------+
1
2
3
4
5
ETCDCTL_API=3 \
etcdctl  --cacert=/etc/kubernetes/ssl/ca.pem \
--cert=/etc/etcd/ssl/etcd.pem \
--key=/etc/etcd/ssl/etcd-key.pem \
--endpoints="https://172.18.247.200:2379,etcd2=https://172.18.247.201:2379,etcd3=https://172.18.247.202:2379"   endpoint health
1
2
3
4
# 一毫秒等于1000微秒   单位µs 微秒
https://172.18.247.200:2379 is healthy: successfully committed proposal: took = 1.10561ms
https://172.18.247.201:2379 is healthy: successfully committed proposal: took = 1.10052ms
https://172.18.247.202:2379 is healthy: successfully committed proposal: took = 967.975µs

备份自动上传阿里oss shell

1
2
3
4
5
mkdir -p /data/etcd/backup

crontab -e
# 每天晚上10点30自动备份上传oss
30 22 * * * /data/Etcd_back.sh
1
2
3
4
5
6
#!/bin/bash
Endpoints=172.18.247.200
export ETCDCTL_API=3
/opt/kube/bin/etcdctl --endpoints=https://$Endpoints:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/etcd/ssl/etcd.pem --key=/etc/etcd/ssl/etcd-key.pem  snapshot save   /xylink/etcd/backup/Etcd-$(date +%Y-%m-%d).db
cd /data/etcd/backup/ && rm -rf  `ls  /data/etcd/backup/ | grep -v Etcd-$(date +%Y-%m-%d).db`
/xylink/ossutil64 cp /data/etcd/backup/Etcd-$(date +%Y-%m-%d).db oss://kubernetes-backup