Kubernetes Views 4

Restoring etcd on Kubernetes

Restoring etcd on Kubernetes image 1

To restore etcd


etcd stores all of the information about the Kubernetes cluster configuration.
To restore etcd safely, you have to build a cluster that matches the cluster you are restoring, and then restore etcd.

The information that has to match includes the node names, node IPs, and the cluster layout.

Why the cluster layout has to match


When you restore etcd, etcd is restored as if it were the original cluster. But if the node names and IPs are different, the metadata stored in etcd will not match, so the restore may not complete normally.

How to restore


  • The backup file must exist on every master node.

The command below must be run on every master node, and '현재노드의ip' means the IP of the node that is running the command.
(This was tested virtually with nodes 10.10.10.1–3.)

ETCDCTL_API=3 etcdctl snapshot restore backup.db --name m1또는m2,m3 \\
--cacert=/etc/kubernetes/pki/etcd/ca.crt \\
--cert=/etc/kubernetes/pki/etcd/server.crt \\
--key=/etc/kubernetes/pki/etcd/server.key \\
--initial-cluster m1=https://10.10.10.1:2380,m2=https://10.10.10.2:2380,m3=https://10.10.10.3:2380 \\
--initial-cluster-token etcd-cluster-1 \\
--initial-advertise-peer-urls https:현재노드의ip//:2380

After you run the command above, m1.etcd, m2.etcd, and m3.etcd files will be created on each node. Then run the commands below. You have to do this on every master node as well.

mkdir /root/etcd-restore
mv /etc/kubernetes/manifests/*.yaml /root/etcd-restore
mv /var/lib/etcd/member /var/lib/etcd/member.bak
cp -r m1.etcd/member /var/lib/etcd/
mv /root/etcd-restore/*.yaml /etc/kubernetes/manifests/

The commands above swap the Kubernetes configuration on the existing cluster with the restored configuration.
The restore then proceeds.

After the restore


After the restore, if you look at the etcd or apiserver logs you may see errors about a certificate x509 issue.

With that issue the cluster may look as if it is working correctly, but you cannot join a new worker node or modify the cluster.

To fix that, you update the certificates.

for namespace in $(kubectl get namespace -o jsonpath='{.items[*].metadata.name}'); do
    for name in $(kubectl get deployments -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
        kubectl patch deployment -n ${namespace} ${name} -p '{"spec":{"template":{"metadata":{"annotations":{"ca-rotation": "1"}}}}}';
    done
    for name in $(kubectl get daemonset -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
        kubectl patch daemonset -n ${namespace} ${name} -p '{"spec":{"template":{"metadata":{"annotations":{"ca-rotation": "1"}}}}}';
    done
done

base64_encoded_ca="$(base64 -w0 /etc/kubernetes/pki/ca.crt)"

kubectl get cm/cluster-info --namespace kube-public -o yaml | \
    /bin/sed "s/\(certificate-authority-data:\).*/\1 ${base64_encoded_ca}/" | \
    kubectl apply -f -

You can update the certificates by running that content as a shell script.

Reactions

Comments 0

A nickname and a password are required. The same password edits or deletes the comment later. Comments appear after a review.

Comments appear after a review. The password is used again to edit or delete.