0%

备份etcd

前言

整个k8s诸多组件几乎都是无状态的,所有的数据保存在etcd里,可以说etcd是整个k8s集群的数据库。

具体方案

etcd-backup

https://github.com/giantswarm/etcd-backup

将etcdctl 修改为线上实际的版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM alpine:3.8

RUN apk add --no-cache curl

# Get etcdctl
ENV ETCD_VER=v3.2.24
RUN \
cd /tmp && \
curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz | \
tar xz -C /usr/local/bin --strip-components=1

COPY ./etcd-backup /
ENTRYPOINT ["/etcd-backup"]
CMD ["-h"]

k8s

选择k8s中的cronjob比较合适,备份策略是每三小时备份一次。

cronjob.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: etcd-backup
namespace: kube-system
spec:
schedule: "0 */4 * * *"
successfulJobsHistoryLimit: 2
failedJobsHistoryLimit: 2
jobTemplate:
spec:
# Job timeout
activeDeadlineSeconds: 300
template:
spec:
tolerations:
# Tolerate master taint
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
# Container creates etcd backups.
# Run container in host network mode on G8s masters
# to be able to use 127.0.0.1 as etcd address.
# For etcd v2 backups container should have access
# to etcd data directory. To achive that,
# mount /var/lib/etcd3 as a volume.
nodeSelector:
node-role.kubernetes.io/master: ""
containers:
- name: etcd-backup
image: iyacontrol/etcd-backup:0.1
args:
# backup guest clusters only on production instalations
# testing installation can have many broken guest clusters
- -prefix=k8s-prod-1
- -etcd-v2-datadir=/var/lib/etcd
- -etcd-v3-endpoints=https://172.xx.xx.221:2379,https://172.xx.xx.83:2379,https://172.xx.xx.246:2379
- -etcd-v3-cacert=/certs/ca.crt
- -etcd-v3-cert=/certs/server.crt
- -etcd-v3-key=/certs/server.key
- -aws-s3-bucket=mybucket
- -aws-s3-region=us-east-1
volumeMounts:
- mountPath: /var/lib/etcd
name: etcd-datadir
- mountPath: /certs
name: etcd-certs
env:
- name: ETCDBACKUP_AWS_ACCESS_KEY
valueFrom:
secretKeyRef:
name: etcd-backup
key: ETCDBACKUP_AWS_ACCESS_KEY
- name: ETCDBACKUP_AWS_SECRET_KEY
valueFrom:
secretKeyRef:
name: etcd-backup
key: ETCDBACKUP_AWS_SECRET_KEY
- name: ETCDBACKUP_PASSPHRASE
valueFrom:
secretKeyRef:
name: etcd-backup
key: ETCDBACKUP_PASSPHRASE
volumes:
- name: etcd-datadir
hostPath:
path: /var/lib/etcd
- name: etcd-certs
hostPath:
path: /etc/kubernetes/pki/etcd/
# Do not restart pod, job takes care on restarting failed pod.
restartPolicy: Never
hostNetwork: true

注意:容忍 和 nodeselector配合,让pod调度到master节点上。

secret.yaml

1
2
3
4
5
6
7
8
9
10
apiVersion: v1
kind: Secret
metadata:
name: etcd-backup
namespace: kube-system
type: Opaque
data:
ETCDBACKUP_AWS_ACCESS_KEY: QUtJTI0TktCT0xQRlEK
ETCDBACKUP_AWS_SECRET_KEY: aXJ6eThjQnM2MVRaSkdGMGxDeHhoeFZNUDU4ZGRNbgo=
ETCDBACKUP_PASSPHRASE: ""