Restoring etcd quorum

If you lose etcd quorum, you can restore it.

  • If you run etcd on a separate host, you must back up etcd, take down your etcd cluster, and form a new one. You can use one healthy etcd node to form a new cluster, but you must remove all other healthy nodes.

  • If you run etcd as static pods on your master nodes, you stop the etcd pods, create a temporary cluster, and then restart the etcd pods.

During etcd quorum loss, applications that run on OKD are unaffected. However, the platform functionality is limited to read-only operations. You cannot take action such as scaling an application up or down, changing deployments, or running or modifying builds.

To confirm the loss of etcd quorum, run one of the following commands and confirm that the cluster is unhealthy:

  • If you use the etcd v2 API, run the following command:

    1. # etcd_ctl=2 etcdctl --cert-file=/etc/origin/master/master.etcd-client.crt \
    2. --key-file /etc/origin/master/master.etcd-client.key \
    3. --ca-file /etc/origin/master/master.etcd-ca.crt \
    4. --endpoints="https://*master-0.example.com*:2379,\
    5. https://*master-1.example.com*:2379,\
    6. https://*master-2.example.com*:2379"\
    7. cluster-health
    8. member 165201190bf7f217 is unhealthy: got unhealthy result from https://master-0.example.com:2379
    9. member b50b8a0acab2fa71 is unreachable: [https://master-1.example.com:2379] are all unreachable
    10. member d40307cbca7bc2df is unreachable: [https://master-2.example.com:2379] are all unreachable
    11. cluster is unhealthy
  • If you use the v3 API, run the following command:

    1. # ETCDCTL_API=3 etcdctl --cert=/etc/origin/master/master.etcd-client.crt \
    2. --key=/etc/origin/master/master.etcd-client.key \
    3. --cacert=/etc/origin/masterca.crt \
    4. --endpoints="https://*master-0.example.com*:2379,\
    5. https://*master-1.example.com*:2379,\
    6. https://*master-2.example.com*:2379"\
    7. endpoint health
    8. https://master-0.example.com:2379 is unhealthy: failed to connect: context deadline exceeded
    9. https://master-1.example.com:2379 is unhealthy: failed to connect: context deadline exceeded
    10. https://master-2.example.com:2379 is unhealthy: failed to connect: context deadline exceeded
    11. Error: unhealthy cluster

Note the member IDs and host names of the hosts. You use one of the nodes that can be reached to form a new cluster.

Restoring etcd quorum for separate services

Backing up etcd

When you back up etcd, you must back up both the etcd configuration files and the etcd data.

Backing up etcd configuration files

The etcd configuration files to be preserved are all stored in the /etc/etcd directory of the instances where etcd is running. This includes the etcd configuration file (/etc/etcd/etcd.conf) and the required certificates for cluster communication. All those files are generated at installation time by the Ansible installer.

Procedure

For each etcd member of the cluster, back up the etcd configuration.

  1. $ ssh master-0
  2. # mkdir -p /backup/etcd-config-$(date +%Y%m%d)/
  3. # cp -R /etc/etcd/ /backup/etcd-config-$(date +%Y%m%d)/

The certificates and configuration files on each etcd cluster member are unique.

Backing up etcd data

Prerequisites

The OKD installer creates aliases to avoid typing all the flags named etcdctl2 for etcd v2 tasks and etcdctl3 for etcd v3 tasks.

However, the etcdctl3 alias does not provide the full endpoint list to the etcdctl command, so you must specify the —endpoints option and list all the endpoints.

Before backing up etcd:

  • etcdctl binaries must be available or, in containerized installations, the rhel7/etcd container must be available.

  • Ensure that the OKD API service is running.

  • Ensure connectivity with the etcd cluster (port 2379/tcp).

  • Ensure the proper certificates to connect to the etcd cluster.

  • Ensure go is installed.

Procedure

While the etcdctl backup command is used to perform the backup, etcd v3 has no concept of a backup. Instead, you either take a snapshot from a live member with the etcdctl snapshot save command or copy the member/snap/db file from an etcd data directory.

The etcdctl backup command rewrites some of the metadata contained in the backup, specifically, the node ID and cluster ID, which means that in the backup, the node loses its former identity. To recreate a cluster from the backup, you create a new, single-node cluster, then add the rest of the nodes to the cluster. The metadata is rewritten to prevent the new node from joining an existing cluster.

Back up the etcd data:

+

Clusters upgraded from previous versions of OKD might contain v2 data stores. Back up all etcd data stores.

  1. Make a snapshot of the etcd node:

    1. # systemctl show etcd --property=ActiveState,SubState
    2. # mkdir -p /var/lib/etcd/backup/etcd-$(date +%Y%m%d) (1)
    3. # etcdctl3 snapshot save /var/lib/etcd/backup/etcd-$(date +%Y%m%d)/db
    1You must write the snapshot to a directory under /var/lib/etcd/.

    The etcdctl snapshot save command requires the etcd service to be running.

  2. Stop all etcd services by removing the etcd pod definition and rebooting the host:

    1. # mkdir -p /etc/origin/node/pods-stopped
    2. # mv /etc/origin/node/pods/* /etc/origin/node/pods-stopped/
  3. Create the etcd data backup and copy the etcd db file:

    1. # etcdctl2 backup \
    2. --data-dir /var/lib/etcd \
    3. --backup-dir /backup/etcd-$(date +%Y%m%d)

    A /backup/etcd-<date>/ directory is created, where <date> represents the current date, which must be an external NFS share, S3 bucket, or any external storage location.

    In the case of an all-in-one cluster, the etcd data directory is located in the /var/lib/origin/openshift.local.etcd directory.

    • If etcd runs as a static pod, run the following commands:

      If you use static pods, use the v3 API.

  1. Obtain the etcd endpoint IP address from the static pod manifest:

    1. $ export ETCD_POD_MANIFEST="/etc/origin/node/pods/etcd.yaml"
    2. $ export ETCD_EP=$(grep https ${ETCD_POD_MANIFEST} | cut -d '/' -f3)
  2. Obtain the etcd pod name:

    1. $ oc login -u system:admin
    2. $ export ETCD_POD=$(oc get pods -n kube-system | grep -o -m 1 '\S*etcd\S*')
  3. Take a snapshot of the etcd data in the pod and store it locally:

    1. $ oc project kube-system
    2. $ oc exec ${ETCD_POD} -c etcd -- /bin/bash -c "ETCDCTL_API=3 etcdctl \
    3. --cert /etc/etcd/peer.crt \
    4. --key /etc/etcd/peer.key \
    5. --cacert /etc/etcd/ca.crt \
    6. --endpoints $ETCD_EP \
    7. snapshot save /var/lib/etcd/snapshot.db"

Removing an etcd host

If an etcd host fails beyond restoration, remove it from the cluster. To recover from an etcd quorum loss, you must also remove all healthy etcd nodes but one from your cluster.

Steps to be performed on all masters hosts

Procedure

  1. Remove each other etcd host from the etcd cluster. Run the following command for each etcd node:

    1. # etcdctl -C https://<surviving host IP address>:2379 \
    2. --ca-file=/etc/etcd/ca.crt \
    3. --cert-file=/etc/etcd/peer.crt \
    4. --key-file=/etc/etcd/peer.key member remove <failed member ID>
  2. Remove the other etcd hosts from the /etc/origin/master/master-config.yaml +master configuration file on every master:

    1. etcdClientInfo:
    2. ca: master.etcd-ca.crt
    3. certFile: master.etcd-client.crt
    4. keyFile: master.etcd-client.key
    5. urls:
    6. - https://master-0.example.com:2379
    7. - https://master-1.example.com:2379 (1)
    8. - https://master-2.example.com:2379 (1)
    1The host to remove.
  3. Restart the master API service on every master:

    1. # master-restart api restart-master controller

Steps to be performed in the current etcd cluster

Procedure

  1. Remove the failed host from the cluster:

    1. # etcdctl2 cluster-health
    2. member 5ee217d19001 is healthy: got healthy result from https://192.168.55.12:2379
    3. member 2a529ba1840722c0 is healthy: got healthy result from https://192.168.55.8:2379
    4. failed to check the health of member 8372784203e11288 on https://192.168.55.21:2379: Get https://192.168.55.21:2379/health: dial tcp 192.168.55.21:2379: getsockopt: connection refused
    5. member 8372784203e11288 is unreachable: [https://192.168.55.21:2379] are all unreachable
    6. member ed4f0efd277d7599 is healthy: got healthy result from https://192.168.55.13:2379
    7. cluster is healthy
    8. # etcdctl2 member remove 8372784203e11288 (1)
    9. Removed member 8372784203e11288 from cluster
    10. # etcdctl2 cluster-health
    11. member 5ee217d19001 is healthy: got healthy result from https://192.168.55.12:2379
    12. member 2a529ba1840722c0 is healthy: got healthy result from https://192.168.55.8:2379
    13. member ed4f0efd277d7599 is healthy: got healthy result from https://192.168.55.13:2379
    14. cluster is healthy
    1The remove command requires the etcd ID, not the hostname.
  2. To ensure the etcd configuration does not use the failed host when the etcd service is restarted, modify the /etc/etcd/etcd.conf file on all remaining etcd hosts and remove the failed host in the value for the ETCD_INITIAL_CLUSTER variable:

    1. # vi /etc/etcd/etcd.conf

    For example:

    1. ETCD_INITIAL_CLUSTER=master-0.example.com=https://192.168.55.8:2380,master-1.example.com=https://192.168.55.12:2380,master-2.example.com=https://192.168.55.13:2380

    becomes:

    1. ETCD_INITIAL_CLUSTER=master-0.example.com=https://192.168.55.8:2380,master-1.example.com=https://192.168.55.12:2380

    Restarting the etcd services is not required, because the failed host is removed using etcdctl.

  3. Modify the Ansible inventory file to reflect the current status of the cluster and to avoid issues when re-running a playbook:

    1. [OSEv3:children]
    2. masters
    3. nodes
    4. etcd
    5. ... [OUTPUT ABBREVIATED] ...
    6. [etcd]
    7. master-0.example.com
    8. master-1.example.com
  4. If you are using Flannel, modify the flanneld service configuration located at /etc/sysconfig/flanneld on every host and remove the etcd host:

    1. FLANNEL_ETCD_ENDPOINTS=https://master-0.example.com:2379,https://master-1.example.com:2379,https://master-2.example.com:2379
  5. Restart the flanneld service:

    1. # systemctl restart flanneld.service

Creating a single-node etcd cluster

To restore the full functionality of your OKD instance, make a remaining etcd node a standalone etcd cluster.

Procedure

  1. On the etcd node that you did not remove from the cluster, stop all etcd services by removing the etcd pod definition:

    1. # mkdir -p /etc/origin/node/pods-stopped
    2. # mv /etc/origin/node/pods/etcd.yaml /etc/origin/node/pods-stopped/
    3. # systemctl stop atomic-openshift-node
    4. # mv /etc/origin/node/pods-stopped/etcd.yaml /etc/origin/node/pods/
  2. Run the etcd service on the host, forcing a new cluster.

    These commands create a custom file for the etcd service, which adds the --force-new-cluster option to the etcd start command:

    1. # mkdir -p /etc/systemd/system/etcd.service.d/
    2. # echo "[Service]" > /etc/systemd/system/etcd.service.d/temp.conf
    3. # echo "ExecStart=" >> /etc/systemd/system/etcd.service.d/temp.conf
    4. # sed -n '/ExecStart/s/"$/ --force-new-cluster"/p' \
    5. /usr/lib/systemd/system/etcd.service \
    6. >> /etc/systemd/system/etcd.service.d/temp.conf
    7. # systemctl daemon-reload
    8. # master-restart etcd
  3. List the etcd member and confirm that the member list contains only your single etcd host:

    1. # etcdctl member list
    2. 165201190bf7f217: name=192.168.34.20 peerURLs=http://localhost:2380 clientURLs=https://master-0.example.com:2379 isLeader=true
  4. After restoring the data and creating a new cluster, you must update the peerURLs parameter value to use the IP address where etcd listens for peer communication:

    1. # etcdctl member update 165201190bf7f217 https://192.168.34.20:2380 (1)
    1165201190bf7f217 is the member ID shown in the output of the previous command, and https://192.168.34.20:2380 is its IP address.
  5. To verify, check that the IP is in the member list:

    1. $ etcdctl2 member list
    2. 5ee217d17301: name=master-0.example.com peerURLs=https://*192.168.55.8*:2380 clientURLs=https://192.168.55.8:2379 isLeader=true

Adding etcd nodes after restoring

After the first instance is running, you can add multiple etcd servers to your cluster.

Procedure
  1. Get the etcd name for the instance in the ETCD_NAME variable:

    1. # grep ETCD_NAME /etc/etcd/etcd.conf
  2. Get the IP address where etcd listens for peer communication:

    1. # grep ETCD_INITIAL_ADVERTISE_PEER_URLS /etc/etcd/etcd.conf
  3. If the node was previously part of a etcd cluster, delete the previous etcd data:

    1. # rm -Rf /var/lib/etcd/*
  4. On the etcd host where etcd is properly running, add the new member:

    1. # etcdctl3 member add *<name>* \
    2. --peer-urls="*<advertise_peer_urls>*"

    The command outputs some variables. For example:

    1. ETCD_NAME="master2"
    2. ETCD_INITIAL_CLUSTER="master-0.example.com=https://192.168.55.8:2380"
    3. ETCD_INITIAL_CLUSTER_STATE="existing"
  5. Add the values from the previous command to the /etc/etcd/etcd.conf file of the new host:

    1. # vi /etc/etcd/etcd.conf
  6. Start the etcd service in the node joining the cluster:

    1. # systemctl start etcd.service
  7. Check for error messages:

    1. # master-logs etcd etcd
  8. Once you add all the nodes, verify the cluster status and cluster health:

    1. # etcdctl3 endpoint health --endpoints="https://<etcd_host1>:2379,https://<etcd_host2>:2379,https://<etcd_host3>:2379"
    2. https://master-0.example.com:2379 is healthy: successfully committed proposal: took = 1.423459ms
    3. https://master-1.example.com:2379 is healthy: successfully committed proposal: took = 1.767481ms
    4. https://master-2.example.com:2379 is healthy: successfully committed proposal: took = 1.599694ms
    5. # etcdctl3 endpoint status --endpoints="https://<etcd_host1>:2379,https://<etcd_host2>:2379,https://<etcd_host3>:2379"
    6. https://master-0.example.com:2379, 40bef1f6c79b3163, 3.2.5, 28 MB, true, 9, 2878
    7. https://master-1.example.com:2379, 1ea57201a3ff620a, 3.2.5, 28 MB, false, 9, 2878
    8. https://master-2.example.com:2379, 59229711e4bc65c8, 3.2.5, 28 MB, false, 9, 2878
  9. Add the remaining peers back into the cluster.

Restoring etcd quorum for static pods

If you lose etcd quorum on a cluster that uses static pods for etcd, take the following steps:

Procedure

  1. Stop the etcd pod:

    1. mv /etc/origin/node/pods/etcd.yaml .
  2. Temporarily force a new cluster on the etcd host:

    1. $ cp /etc/etcd/etcd.conf etcd.conf.bak
    2. $ echo "ETCD_FORCE_NEW_CLUSTER=true" >> /etc/etcd/etcd.conf
  3. Restart the etcd pod:

    1. $ mv etcd.yaml /etc/origin/node/pods/.
  4. Stop the etcd pod and remove the FORCE_NEW_CLUSTER command:

    1. $ mv /etc/origin/node/pods/etcd.yaml .
    2. $ rm /etc/etcd/etcd.conf
    3. $ mv etcd.conf.bak /etc/etcd/etcd.conf
  5. Restart the etcd pod:

    1. $ mv etcd.yaml /etc/origin/node/pods/.