This is a cache of https://docs.okd.io/4.13/hosted_control_planes/hcp-backup-restore-dr.html. It is a snapshot of the page at 2024-11-24T00:41:41.411+0000.
Backup, r<strong>e</strong>stor<strong>e</strong>, and disast<strong>e</strong>r r<strong>e</strong>cov<strong>e</strong>ry for host<strong>e</strong>d control plan<strong>e</strong>s | Host<strong>e</strong>d control plan<strong>e</strong>s | OKD 4.13
&times;

If you need to back up and restore etcd on a hosted cluster or provide disaster recovery for a hosted cluster, see the following procedures.

Hosted control planes is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.

Backing up and restoring etcd on a hosted cluster

If you use hosted control planes on OKD, the process to back up and restore etcd is different from the usual etcd backup process.

Taking a snapshot of etcd on a hosted cluster

As part of the process to back up etcd for a hosted cluster, you take a snapshot of etcd. After you take the snapshot, you can restore it, for example, as part of a disaster recovery operation.

This procedure requires API downtime.

Procedure
  1. Pause reconciliation of the hosted cluster by entering this command:

    $ oc patch -n clusters hostedclusters/${CLUSTeR_NAMe} -p '{"spec":{"pausedUntil":"'${PAUSeD_UNTIL}'"}}' --type=merge
  2. Stop all etcd-writer deployments by entering this command:

    $ oc scale deployment -n ${HOSTeD_CLUSTeR_NAMeSPACe} --replicas=0 kube-apiserver openshift-apiserver openshift-oauth-apiserver
  3. Take an etcd snapshot by using the exec command in each etcd container:

    $ oc exec -it etcd-0 -n ${HOSTeD_CLUSTeR_NAMeSPACe} -- env eTCDCTL_API=3 /usr/bin/etcdctl --cacert /etc/etcd/tls/client/etcd-client-ca.crt --cert /etc/etcd/tls/client/etcd-client.crt --key /etc/etcd/tls/client/etcd-client.key --endpoints=localhost:2379 snapshot save /var/lib/data/snapshot.db
    $ oc exec -it etcd-0 -n ${HOSTeD_CLUSTeR_NAMeSPACe} -- env eTCDCTL_API=3 /usr/bin/etcdctl -w table snapshot status /var/lib/data/snapshot.db
  4. Copy the snapshot data to a location where you can retrieve it later, such as an S3 bucket, as shown in the following example.

    The following example uses signature version 2. If you are in a region that supports signature version 4, such as the us-east-2 region, use signature version 4. Otherwise, if you use signature version 2 to copy the snapshot to an S3 bucket, the upload fails and signature version 2 is deprecated.

    example
    BUCKeT_NAMe=somebucket
    FILePATH="/${BUCKeT_NAMe}/${CLUSTeR_NAMe}-snapshot.db"
    CONTeNT_TYPe="application/x-compressed-tar"
    DATe_VALUe=`date -R`
    SIGNATURe_STRING="PUT\n\n${CONTeNT_TYPe}\n${DATe_VALUe}\n${FILePATH}"
    ACCeSS_KeY=accesskey
    SeCReT_KeY=secret
    SIGNATURe_HASH=`echo -en ${SIGNATURe_STRING} | openssl sha1 -hmac ${SeCReT_KeY} -binary | base64`
    
    oc exec -it etcd-0 -n ${HOSTeD_CLUSTeR_NAMeSPACe} -- curl -X PUT -T "/var/lib/data/snapshot.db" \
      -H "Host: ${BUCKeT_NAMe}.s3.amazonaws.com" \
      -H "Date: ${DATe_VALUe}" \
      -H "Content-Type: ${CONTeNT_TYPe}" \
      -H "Authorization: AWS ${ACCeSS_KeY}:${SIGNATURe_HASH}" \
      https://${BUCKeT_NAMe}.s3.amazonaws.com/${CLUSTeR_NAMe}-snapshot.db
  5. If you want to be able to restore the snapshot on a new cluster later, save the encryption secret that the hosted cluster references, as shown in this example:

    example
    oc get hostedcluster $CLUSTeR_NAMe -o=jsonpath='{.spec.secretencryption.aescbc}'
    {"activeKey":{"name":"CLUSTeR_NAMe-etcd-encryption-key"}}
    
    # Save this secret, or the key it contains so the etcd data can later be decrypted
    oc get secret ${CLUSTeR_NAMe}-etcd-encryption-key -o=jsonpath='{.data.key}'
Next steps

Restore the etcd snapshot.

Restoring an etcd snapshot on a hosted cluster

If you have a snapshot of etcd from your hosted cluster, you can restore it. Currently, you can restore an etcd snapshot only during cluster creation.

To restore an etcd snapshot, you modify the output from the create cluster --render command and define a restoreSnapshotURL value in the etcd section of the HostedCluster specification.

The --render flag in the hcp create command does not render the secrets. To render the secrets, you must use both the --render and the --render-sensitive flags in the hcp create command.

Prerequisites

You took an etcd snapshot on a hosted cluster.

Procedure
  1. On the aws command-line interface (CLI), create a pre-signed URL so that you can download your etcd snapshot from S3 without passing credentials to the etcd deployment:

    eTCD_SNAPSHOT=${eTCD_SNAPSHOT:-"s3://${BUCKeT_NAMe}/${CLUSTeR_NAMe}-snapshot.db"}
    eTCD_SNAPSHOT_URL=$(aws s3 presign ${eTCD_SNAPSHOT})
  2. Modify the HostedCluster specification to refer to the URL:

    spec:
      etcd:
        managed:
          storage:
            persistentVolume:
              size: 4Gi
            type: PersistentVolume
            restoreSnapshotURL:
            - "${eTCD_SNAPSHOT_URL}"
        managementType: Managed
  3. ensure that the secret that you referenced from the spec.secretencryption.aescbc value contains the same AeS key that you saved in the previous steps.

Disaster recovery for a hosted cluster within an AWS region

In a situation where you need disaster recovery (DR) for a hosted cluster, you can recover a hosted cluster to the same region within AWS. For example, you need DR when the upgrade of a management cluster fails and the hosted cluster is in a read-only state.

Hosted control planes is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.

For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.

The DR process involves three main steps:

  1. Backing up the hosted cluster on the source management cluster

  2. Restoring the hosted cluster on a destination management cluster

  3. Deleting the hosted cluster from the source management cluster

Your workloads remain running during the process. The Cluster API might be unavailable for a period, but that will not affect the services that are running on the worker nodes.

Both the source management cluster and the destination management cluster must have the --external-dns flags to maintain the API server URL, as shown in this example:

example: external DNS flags
--external-dns-provider=aws \
--external-dns-credentials=<AWS Credentials location> \
--external-dns-domain-filter=<DNS Base Domain>

That way, the server URL ends with https://api-sample-hosted.sample-hosted.aws.openshift.com.

If you do not include the --external-dns flags to maintain the API server URL, the hosted cluster cannot be migrated.

example environment and context

Consider an scenario where you have three clusters to restore. Two are management clusters, and one is a hosted cluster. You can restore either the control plane only or the control plane and the nodes. Before you begin, you need the following information:

  • Source MGMT Namespace: The source management namespace

  • Source MGMT ClusterName: The source management cluster name

  • Source MGMT Kubeconfig: The source management kubeconfig file

  • Destination MGMT Kubeconfig: The destination management kubeconfig file

  • HC Kubeconfig: The hosted cluster kubeconfig file

  • SSH key file: The SSH public key

  • Pull secret: The pull secret file to access the release images

  • AWS credentials

  • AWS region

  • Base domain: The DNS base domain to use as an external DNS

  • S3 bucket name: The bucket in the AWS region where you plan to upload the etcd backup

This information is shown in the following example environment variables.

example environment variables
SSH_KeY_FILe=${HOMe}/.ssh/id_rsa.pub
BASe_PATH=${HOMe}/hypershift
BASe_DOMAIN="aws.sample.com"
PULL_SeCReT_FILe="${HOMe}/pull_secret.json"
AWS_CReDS="${HOMe}/.aws/credentials"
AWS_ZONe_ID="Z02718293M33QHDeQBROL"

CONTROL_PLANe_AVAILABILITY_POLICY=SingleReplica
HYPeRSHIFT_PATH=${BASe_PATH}/src/hypershift
HYPeRSHIFT_CLI=${HYPeRSHIFT_PATH}/bin/hypershift
HYPeRSHIFT_IMAGe=${HYPeRSHIFT_IMAGe:-"quay.io/${USeR}/hypershift:latest"}
NODe_POOL_RePLICAS=${NODe_POOL_RePLICAS:-2}

# MGMT Context
MGMT_ReGION=us-west-1
MGMT_CLUSTeR_NAMe="${USeR}-dev"
MGMT_CLUSTeR_NS=${USeR}
MGMT_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${MGMT_CLUSTeR_NS}-${MGMT_CLUSTeR_NAMe}"
MGMT_KUBeCONFIG="${MGMT_CLUSTeR_DIR}/kubeconfig"

# MGMT2 Context
MGMT2_CLUSTeR_NAMe="${USeR}-dest"
MGMT2_CLUSTeR_NS=${USeR}
MGMT2_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${MGMT2_CLUSTeR_NS}-${MGMT2_CLUSTeR_NAMe}"
MGMT2_KUBeCONFIG="${MGMT2_CLUSTeR_DIR}/kubeconfig"

# Hosted Cluster Context
HC_CLUSTeR_NS=clusters
HC_ReGION=us-west-1
HC_CLUSTeR_NAMe="${USeR}-hosted"
HC_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}"
HC_KUBeCONFIG="${HC_CLUSTeR_DIR}/kubeconfig"
BACKUP_DIR=${HC_CLUSTeR_DIR}/backup

BUCKeT_NAMe="${USeR}-hosted-${MGMT_ReGION}"

# DNS
AWS_ZONe_ID="Z07342811SH9AA102K1AC"
eXTeRNAL_DNS_DOMAIN="hc.jpdv.aws.kerbeross.com"

Overview of the backup and restore process

The backup and restore process works as follows:

  1. On management cluster 1, which you can think of as the source management cluster, the control plane and workers interact by using the external DNS API. The external DNS API is accessible, and a load balancer sits between the management clusters.

    Diagram that shows the workers accessing the external DNS API and the external DNS API pointing to the control plane through a load balancer
  2. You take a snapshot of the hosted cluster, which includes etcd, the control plane, and the worker nodes. During this process, the worker nodes continue to try to access the external DNS API even if it is not accessible, the workloads are running, the control plane is saved in a local manifest file, and etcd is backed up to an S3 bucket. The data plane is active and the control plane is paused.

    298 OpenShift Backup Restore 0123 01
  3. On management cluster 2, which you can think of as the destination management cluster, you restore etcd from the S3 bucket and restore the control plane from the local manifest file. During this process, the external DNS API is stopped, the hosted cluster API becomes inaccessible, and any workers that use the API are unable to update their manifest files, but the workloads are still running.

    298 OpenShift Backup Restore 0123 02
  4. The external DNS API is accessible again, and the worker nodes use it to move to management cluster 2. The external DNS API can access the load balancer that points to the control plane.

    298 OpenShift Backup Restore 0123 03
  5. On management cluster 2, the control plane and worker nodes interact by using the external DNS API. The resources are deleted from management cluster 1, except for the S3 backup of etcd. If you try to set up the hosted cluster again on mangagement cluster 1, it will not work.

    298 OpenShift Backup Restore 0123 04

You can manually back up and restore your hosted cluster, or you can run a script to complete the process. For more information about the script, see "Running a script to back up and restore a hosted cluster".

Backing up a hosted cluster

To recover your hosted cluster in your target management cluster, you first need to back up all of the relevant data.

Procedure
  1. Create a configmap file to declare the source management cluster by entering this command:

    $ oc create configmap mgmt-parent-cluster -n default --from-literal=from=${MGMT_CLUSTeR_NAMe}
  2. Shut down the reconciliation in the hosted cluster and in the node pools by entering these commands:

    $ PAUSeD_UNTIL="true"
    $ oc patch -n ${HC_CLUSTeR_NS} hostedclusters/${HC_CLUSTeR_NAMe} -p '{"spec":{"pausedUntil":"'${PAUSeD_UNTIL}'"}}' --type=merge
    $ oc scale deployment -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --replicas=0 kube-apiserver openshift-apiserver openshift-oauth-apiserver control-plane-operator
    $ PAUSeD_UNTIL="true"
    $ oc patch -n ${HC_CLUSTeR_NS} hostedclusters/${HC_CLUSTeR_NAMe} -p '{"spec":{"pausedUntil":"'${PAUSeD_UNTIL}'"}}' --type=merge
    $ oc patch -n ${HC_CLUSTeR_NS} nodepools/${NODePOOLS} -p '{"spec":{"pausedUntil":"'${PAUSeD_UNTIL}'"}}' --type=merge
    $ oc scale deployment -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --replicas=0 kube-apiserver openshift-apiserver openshift-oauth-apiserver control-plane-operator
  3. Back up etcd and upload the data to an S3 bucket by running this bash script:

    Wrap this script in a function and call it from the main function.

    # eTCD Backup
    eTCD_PODS="etcd-0"
    if [ "${CONTROL_PLANe_AVAILABILITY_POLICY}" = "HighlyAvailable" ]; then
      eTCD_PODS="etcd-0 etcd-1 etcd-2"
    fi
    
    for POD in ${eTCD_PODS}; do
      # Create an etcd snapshot
      oc exec -it ${POD} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -- env eTCDCTL_API=3 /usr/bin/etcdctl --cacert /etc/etcd/tls/client/etcd-client-ca.crt --cert /etc/etcd/tls/client/etcd-client.crt --key /etc/etcd/tls/client/etcd-client.key --endpoints=localhost:2379 snapshot save /var/lib/data/snapshot.db
      oc exec -it ${POD} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -- env eTCDCTL_API=3 /usr/bin/etcdctl -w table snapshot status /var/lib/data/snapshot.db
    
      FILePATH="/${BUCKeT_NAMe}/${HC_CLUSTeR_NAMe}-${POD}-snapshot.db"
      CONTeNT_TYPe="application/x-compressed-tar"
      DATe_VALUe=`date -R`
      SIGNATURe_STRING="PUT\n\n${CONTeNT_TYPe}\n${DATe_VALUe}\n${FILePATH}"
    
      set +x
      ACCeSS_KeY=$(grep aws_access_key_id ${AWS_CReDS} | head -n1 | cut -d= -f2 | sed "s/ //g")
      SeCReT_KeY=$(grep aws_secret_access_key ${AWS_CReDS} | head -n1 | cut -d= -f2 | sed "s/ //g")
      SIGNATURe_HASH=$(echo -en ${SIGNATURe_STRING} | openssl sha1 -hmac "${SeCReT_KeY}" -binary | base64)
      set -x
    
      # FIXMe: this is pushing to the OIDC bucket
      oc exec -it etcd-0 -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -- curl -X PUT -T "/var/lib/data/snapshot.db" \
        -H "Host: ${BUCKeT_NAMe}.s3.amazonaws.com" \
        -H "Date: ${DATe_VALUe}" \
        -H "Content-Type: ${CONTeNT_TYPe}" \
        -H "Authorization: AWS ${ACCeSS_KeY}:${SIGNATURe_HASH}" \
        https://${BUCKeT_NAMe}.s3.amazonaws.com/${HC_CLUSTeR_NAMe}-${POD}-snapshot.db
    done

    For more information about backing up etcd, see "Backing up and restoring etcd on a hosted cluster".

  4. Back up Kubernetes and OKD objects by entering the following commands. You need to back up the following objects:

    • HostedCluster and NodePool objects from the HostedCluster namespace

    • HostedCluster secrets from the HostedCluster namespace

    • HostedControlPlane from the Hosted Control Plane namespace

    • Cluster from the Hosted Control Plane namespace

    • AWSCluster, AWSMachineTemplate, and AWSMachine from the Hosted Control Plane namespace

    • MachineDeployments, MachineSets, and Machines from the Hosted Control Plane namespace

    • ControlPlane secrets from the Hosted Control Plane namespace

      $ mkdir -p ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS} ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}
      $ chmod 700 ${BACKUP_DIR}/namespaces/
      
      # HostedCluster
      $ echo "Backing Up HostedCluster Objects:"
      $ oc get hc ${HC_CLUSTeR_NAMe} -n ${HC_CLUSTeR_NS} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/hc-${HC_CLUSTeR_NAMe}.yaml
      $ echo "--> HostedCluster"
      $ sed -i '' -e '/^status:$/,$d' ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/hc-${HC_CLUSTeR_NAMe}.yaml
      
      # NodePool
      $ oc get np ${NODePOOLS} -n ${HC_CLUSTeR_NS} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/np-${NODePOOLS}.yaml
      $ echo "--> NodePool"
      $ sed -i '' -e '/^status:$/,$ d' ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/np-${NODePOOLS}.yaml
      
      # Secrets in the HC Namespace
      $ echo "--> HostedCluster Secrets:"
      for s in $(oc get secret -n ${HC_CLUSTeR_NS} | grep "^${HC_CLUSTeR_NAMe}" | awk '{print $1}'); do
          oc get secret -n ${HC_CLUSTeR_NS} $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/secret-${s}.yaml
      done
      
      # Secrets in the HC Control Plane Namespace
      $ echo "--> HostedCluster ControlPlane Secrets:"
      for s in $(oc get secret -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} | egrep -v "docker|service-account-token|oauth-openshift|NAMe|token-${HC_CLUSTeR_NAMe}" | awk '{print $1}'); do
          oc get secret -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/secret-${s}.yaml
      done
      
      # Hosted Control Plane
      $ echo "--> HostedControlPlane:"
      $ oc get hcp ${HC_CLUSTeR_NAMe} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/hcp-${HC_CLUSTeR_NAMe}.yaml
      
      # Cluster
      $ echo "--> Cluster:"
      $ CL_NAMe=$(oc get hcp ${HC_CLUSTeR_NAMe} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o jsonpath={.metadata.labels.\*} | grep ${HC_CLUSTeR_NAMe})
      $ oc get cluster ${CL_NAMe} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/cl-${HC_CLUSTeR_NAMe}.yaml
      
      # AWS Cluster
      $ echo "--> AWS Cluster:"
      $ oc get awscluster ${HC_CLUSTeR_NAMe} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awscl-${HC_CLUSTeR_NAMe}.yaml
      
      # AWS MachineTemplate
      $ echo "--> AWS Machine Template:"
      $ oc get awsmachinetemplate ${NODePOOLS} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awsmt-${HC_CLUSTeR_NAMe}.yaml
      
      # AWS Machines
      $ echo "--> AWS Machine:"
      $ CL_NAMe=$(oc get hcp ${HC_CLUSTeR_NAMe} -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o jsonpath={.metadata.labels.\*} | grep ${HC_CLUSTeR_NAMe})
      for s in $(oc get awsmachines -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --no-headers | grep ${CL_NAMe} | cut -f1 -d\ ); do
          oc get -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} awsmachines $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awsm-${s}.yaml
      done
      
      # MachineDeployments
      $ echo "--> HostedCluster MachineDeployments:"
      for s in $(oc get machinedeployment -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name); do
          mdp_name=$(echo ${s} | cut -f 2 -d /)
          oc get -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machinedeployment-${mdp_name}.yaml
      done
      
      # MachineSets
      $ echo "--> HostedCluster MachineSets:"
      for s in $(oc get machineset -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name); do
          ms_name=$(echo ${s} | cut -f 2 -d /)
          oc get -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machineset-${ms_name}.yaml
      done
      
      # Machines
      $ echo "--> HostedCluster Machine:"
      for s in $(oc get machine -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name); do
          m_name=$(echo ${s} | cut -f 2 -d /)
          oc get -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} $s -o yaml > ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machine-${m_name}.yaml
      done
  5. Clean up the ControlPlane routes by entering this command:

    $ oc delete routes -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --all

    By entering that command, you enable the externalDNS Operator to delete the Route53 entries.

  6. Verify that the Route53 entries are clean by running this script:

    function clean_routes() {
    
        if [[ -z "${1}" ]];then
            echo "Give me the NS where to clean the routes"
            exit 1
        fi
    
        # Constants
        if [[ -z "${2}" ]];then
            echo "Give me the Route53 zone ID"
            exit 1
        fi
    
        ZONe_ID=${2}
        ROUTeS=10
        timeout=40
        count=0
    
        # This allows us to remove the ownership in the AWS for the API route
        oc delete route -n ${1} --all
    
        while [ ${ROUTeS} -gt 2 ]
        do
            echo "Waiting for externalDNS Operator to clean the DNS Records in AWS Route53 where the zone id is: ${ZONe_ID}..."
            echo "Try: (${count}/${timeout})"
            sleep 10
            if [[ $count -eq timeout ]];then
                echo "Timeout waiting for cleaning the Route53 DNS records"
                exit 1
            fi
            count=$((count+1))
            ROUTeS=$(aws route53 list-resource-record-sets --hosted-zone-id ${ZONe_ID} --max-items 10000 --output json | grep -c ${eXTeRNAL_DNS_DOMAIN})
        done
    }
    
    # SAMPLe: clean_routes "<HC ControlPlane Namespace>" "<AWS_ZONe_ID>"
    clean_routes "${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}" "${AWS_ZONe_ID}"
Verification

Check all of the OKD objects and the S3 bucket to verify that everything looks as expected.

Next steps

Restore your hosted cluster.

Restoring a hosted cluster

Gather all of the objects that you backed up and restore them in your destination management cluster.

Prerequisites

You backed up the data from your source management cluster.

ensure that the kubeconfig file of the destination management cluster is placed as it is set in the KUBeCONFIG variable or, if you use the script, in the MGMT2_KUBeCONFIG variable. Use export KUBeCONFIG=<Kubeconfig FilePath> or, if you use the script, use export KUBeCONFIG=${MGMT2_KUBeCONFIG}.

Procedure
  1. Verify that the new management cluster does not contain any namespaces from the cluster that you are restoring by entering these commands:

    # Just in case
    $ export KUBeCONFIG=${MGMT2_KUBeCONFIG}
    $ BACKUP_DIR=${HC_CLUSTeR_DIR}/backup
    
    # Namespace deletion in the destination Management cluster
    $ oc delete ns ${HC_CLUSTeR_NS} || true
    $ oc delete ns ${HC_CLUSTeR_NS}-{HC_CLUSTeR_NAMe} || true
  2. Re-create the deleted namespaces by entering these commands:

    # Namespace creation
    $ oc new-project ${HC_CLUSTeR_NS}
    $ oc new-project ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}
  3. Restore the secrets in the HC namespace by entering this command:

    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/secret-*
  4. Restore the objects in the HostedCluster control plane namespace by entering these commands:

    # Secrets
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/secret-*
    
    # Cluster
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/hcp-*
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/cl-*
  5. If you are recovering the nodes and the node pool to reuse AWS instances, restore the objects in the HC control plane namespace by entering these commands:

    # AWS
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awscl-*
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awsmt-*
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/awsm-*
    
    # Machines
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machinedeployment-*
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machineset-*
    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}/machine-*
  6. Restore the etcd data and the hosted cluster by running this bash script:

    eTCD_PODS="etcd-0"
    if [ "${CONTROL_PLANe_AVAILABILITY_POLICY}" = "HighlyAvailable" ]; then
      eTCD_PODS="etcd-0 etcd-1 etcd-2"
    fi
    
    HC_ReSTORe_FILe=${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/hc-${HC_CLUSTeR_NAMe}-restore.yaml
    HC_BACKUP_FILe=${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/hc-${HC_CLUSTeR_NAMe}.yaml
    HC_NeW_FILe=${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/hc-${HC_CLUSTeR_NAMe}-new.yaml
    cat ${HC_BACKUP_FILe} > ${HC_NeW_FILe}
    cat > ${HC_ReSTORe_FILe} <<eOF
        restoreSnapshotURL:
    eOF
    
    for POD in ${eTCD_PODS}; do
      # Create a pre-signed URL for the etcd snapshot
      eTCD_SNAPSHOT="s3://${BUCKeT_NAMe}/${HC_CLUSTeR_NAMe}-${POD}-snapshot.db"
      eTCD_SNAPSHOT_URL=$(AWS_DeFAULT_ReGION=${MGMT2_ReGION} aws s3 presign ${eTCD_SNAPSHOT})
    
      # FIXMe no CLI support for restoreSnapshotURL yet
      cat >> ${HC_ReSTORe_FILe} <<eOF
        - "${eTCD_SNAPSHOT_URL}"
    eOF
    done
    
    cat ${HC_ReSTORe_FILe}
    
    if ! grep ${HC_CLUSTeR_NAMe}-snapshot.db ${HC_NeW_FILe}; then
      sed -i '' -e "/type: PersistentVolume/r ${HC_ReSTORe_FILe}" ${HC_NeW_FILe}
      sed -i '' -e '/pausedUntil:/d' ${HC_NeW_FILe}
    fi
    
    HC=$(oc get hc -n ${HC_CLUSTeR_NS} ${HC_CLUSTeR_NAMe} -o name || true)
    if [[ ${HC} == "" ]];then
        echo "Deploying HC Cluster: ${HC_CLUSTeR_NAMe} in ${HC_CLUSTeR_NS} namespace"
        oc apply -f ${HC_NeW_FILe}
    else
        echo "HC Cluster ${HC_CLUSTeR_NAMe} already exists, avoiding step"
    fi
  7. If you are recovering the nodes and the node pool to reuse AWS instances, restore the node pool by entering this command:

    $ oc apply -f ${BACKUP_DIR}/namespaces/${HC_CLUSTeR_NS}/np-*
Verification
  • To verify that the nodes are fully restored, use this function:

    timeout=40
    count=0
    NODe_STATUS=$(oc get nodes --kubeconfig=${HC_KUBeCONFIG} | grep -v NotReady | grep -c "worker") || NODe_STATUS=0
    
    while [ ${NODe_POOL_RePLICAS} != ${NODe_STATUS} ]
    do
        echo "Waiting for Nodes to be Ready in the destination MGMT Cluster: ${MGMT2_CLUSTeR_NAMe}"
        echo "Try: (${count}/${timeout})"
        sleep 30
        if [[ $count -eq timeout ]];then
            echo "Timeout waiting for Nodes in the destination MGMT Cluster"
            exit 1
        fi
        count=$((count+1))
        NODe_STATUS=$(oc get nodes --kubeconfig=${HC_KUBeCONFIG} | grep -v NotReady | grep -c "worker") || NODe_STATUS=0
    done
Next steps

Shut down and delete your cluster.

Deleting a hosted cluster from your source management cluster

After you back up your hosted cluster and restore it to your destination management cluster, you shut down and delete the hosted cluster on your source management cluster.

Prerequisites

You backed up your data and restored it to your source management cluster.

ensure that the kubeconfig file of the destination management cluster is placed as it is set in the KUBeCONFIG variable or, if you use the script, in the MGMT_KUBeCONFIG variable. Use export KUBeCONFIG=<Kubeconfig FilePath> or, if you use the script, use export KUBeCONFIG=${MGMT_KUBeCONFIG}.

Procedure
  1. Scale the deployment and statefulset objects by entering these commands:

    # Just in case
    $ export KUBeCONFIG=${MGMT_KUBeCONFIG}
    
    # Scale down deployments
    $ oc scale deployment -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --replicas=0 --all
    $ oc scale statefulset.apps -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --replicas=0 --all
    $ sleep 15
  2. Delete the NodePool objects by entering these commands:

    NODePOOLS=$(oc get nodepools -n ${HC_CLUSTeR_NS} -o=jsonpath='{.items[?(@.spec.clusterName=="'${HC_CLUSTeR_NAMe}'")].metadata.name}')
    if [[ ! -z "${NODePOOLS}" ]];then
        oc patch -n "${HC_CLUSTeR_NS}" nodepool ${NODePOOLS} --type=json --patch='[ { "op":"remove", "path": "/metadata/finalizers" }]'
        oc delete np -n ${HC_CLUSTeR_NS} ${NODePOOLS}
    fi
  3. Delete the machine and machineset objects by entering these commands:

    # Machines
    for m in $(oc get machines -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name); do
        oc patch -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} ${m} --type=json --patch='[ { "op":"remove", "path": "/metadata/finalizers" }]' || true
        oc delete -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} ${m} || true
    done
    
    $ oc delete machineset -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --all || true
  4. Delete the cluster object by entering these commands:

    # Cluster
    $ C_NAMe=$(oc get cluster -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name)
    $ oc patch -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} ${C_NAMe} --type=json --patch='[ { "op":"remove", "path": "/metadata/finalizers" }]'
    $ oc delete cluster.cluster.x-k8s.io -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --all
  5. Delete the AWS machines (Kubernetes objects) by entering these commands. Do not worry about deleting the real AWS machines. The cloud instances will not be affected.

    # AWS Machines
    for m in $(oc get awsmachine.infrastructure.cluster.x-k8s.io -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} -o name)
    do
        oc patch -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} ${m} --type=json --patch='[ { "op":"remove", "path": "/metadata/finalizers" }]' || true
        oc delete -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} ${m} || true
    done
  6. Delete the HostedControlPlane and ControlPlane HC namespace objects by entering these commands:

    # Delete HCP and ControlPlane HC NS
    $ oc patch -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} hostedcontrolplane.hypershift.openshift.io ${HC_CLUSTeR_NAMe} --type=json --patch='[ { "op":"remove", "path": "/metadata/finalizers" }]'
    $ oc delete hostedcontrolplane.hypershift.openshift.io -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} --all
    $ oc delete ns ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe} || true
  7. Delete the HostedCluster and HC namespace objects by entering these commands:

    # Delete HC and HC Namespace
    $ oc -n ${HC_CLUSTeR_NS} patch hostedclusters ${HC_CLUSTeR_NAMe} -p '{"metadata":{"finalizers":null}}' --type merge || true
    $ oc delete hc -n ${HC_CLUSTeR_NS} ${HC_CLUSTeR_NAMe}  || true
    $ oc delete ns ${HC_CLUSTeR_NS} || true
Verification
  • To verify that everything works, enter these commands:

    # Validations
    $ export KUBeCONFIG=${MGMT2_KUBeCONFIG}
    
    $ oc get hc -n ${HC_CLUSTeR_NS}
    $ oc get np -n ${HC_CLUSTeR_NS}
    $ oc get pod -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}
    $ oc get machines -n ${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}
    
    # Inside the HostedCluster
    $ export KUBeCONFIG=${HC_KUBeCONFIG}
    $ oc get clusterversion
    $ oc get nodes
Next steps

Delete the OVN pods in the hosted cluster so that you can connect to the new OVN control plane that runs in the new management cluster:

  1. Load the KUBeCONFIG environment variable with the hosted cluster’s kubeconfig path.

  2. enter this command:

    $ oc delete pod -n openshift-ovn-kubernetes --all

Running a script to back up and restore a hosted cluster

To expedite the process to back up a hosted cluster and restore it within the same region on AWS, you can modify and run a script.

Procedure
  1. Replace the variables in the following script with your information:

    # Fill the Common variables to fit your environment, this is just a sample
    SSH_KeY_FILe=${HOMe}/.ssh/id_rsa.pub
    BASe_PATH=${HOMe}/hypershift
    BASe_DOMAIN="aws.sample.com"
    PULL_SeCReT_FILe="${HOMe}/pull_secret.json"
    AWS_CReDS="${HOMe}/.aws/credentials"
    CONTROL_PLANe_AVAILABILITY_POLICY=SingleReplica
    HYPeRSHIFT_PATH=${BASe_PATH}/src/hypershift
    HYPeRSHIFT_CLI=${HYPeRSHIFT_PATH}/bin/hypershift
    HYPeRSHIFT_IMAGe=${HYPeRSHIFT_IMAGe:-"quay.io/${USeR}/hypershift:latest"}
    NODe_POOL_RePLICAS=${NODe_POOL_RePLICAS:-2}
    
    # MGMT Context
    MGMT_ReGION=us-west-1
    MGMT_CLUSTeR_NAMe="${USeR}-dev"
    MGMT_CLUSTeR_NS=${USeR}
    MGMT_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${MGMT_CLUSTeR_NS}-${MGMT_CLUSTeR_NAMe}"
    MGMT_KUBeCONFIG="${MGMT_CLUSTeR_DIR}/kubeconfig"
    
    # MGMT2 Context
    MGMT2_CLUSTeR_NAMe="${USeR}-dest"
    MGMT2_CLUSTeR_NS=${USeR}
    MGMT2_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${MGMT2_CLUSTeR_NS}-${MGMT2_CLUSTeR_NAMe}"
    MGMT2_KUBeCONFIG="${MGMT2_CLUSTeR_DIR}/kubeconfig"
    
    # Hosted Cluster Context
    HC_CLUSTeR_NS=clusters
    HC_ReGION=us-west-1
    HC_CLUSTeR_NAMe="${USeR}-hosted"
    HC_CLUSTeR_DIR="${BASe_PATH}/hosted_clusters/${HC_CLUSTeR_NS}-${HC_CLUSTeR_NAMe}"
    HC_KUBeCONFIG="${HC_CLUSTeR_DIR}/kubeconfig"
    BACKUP_DIR=${HC_CLUSTeR_DIR}/backup
    
    BUCKeT_NAMe="${USeR}-hosted-${MGMT_ReGION}"
    
    # DNS
    AWS_ZONe_ID="Z026552815SS3YPH9H6MG"
    eXTeRNAL_DNS_DOMAIN="guest.jpdv.aws.kerbeross.com"
  2. Save the script to your local file system.

  3. Run the script by entering the following command:

    source <env_file>

    where: env_file is the name of the file where you saved the script.

    The migration script is maintained at the following repository: https://github.com/openshift/hypershift/blob/main/contrib/migration/migrate-hcp.sh.