Replace a Node

Replace a worker node


Before you begin, you must:

Replace a worker node

In certain situations, you may want to delete a worker node and have Cluster API replace it with a newly provisioned machine.

  1. Identify the name of the node to delete.

    List the nodes:

    kubectl --kubeconfig ${CLUSTER_NAME}.conf get nodes

    The output from this command resembles the following:

    NAME                                         STATUS   ROLES                  AGE   VERSION    Ready    <none>                 16m   v1.22.8   Ready    control-plane,master   15m   v1.22.8   Ready    control-plane,master   17m   v1.22.8   Ready    control-plane,master   16m   v1.22.8    Ready    <none>                 16m   v1.22.8    Ready    <none>                 16m   v1.22.8     Ready    <none>                 16m   v1.22.8
  2. Export a variable with the node name to use in the next steps:

    This example uses the name

    export NAME_NODE_TO_DELETE="<>"
  3. Delete the Machine resource

    export NAME_MACHINE_TO_DELETE=$(kubectl --kubeconfig ${CLUSTER_NAME}.conf get machine -ojsonpath="{.items[?(\"$NAME_NODE_TO_DELETE\")]}")
    kubectl --kubeconfig ${CLUSTER_NAME}.conf delete machine "$NAME_MACHINE_TO_DELETE" "aws-example-1-md-0-cb9c9bbf7-t894m" deleted

    The command will not return immediately. It will return once the Machine resource has been deleted.

    A few minutes after the Machine resource is deleted, the corresponding Node resource is also deleted.

  4. Observe that the Machine resource is being replaced using this command:

    kubectl --kubeconfig ${CLUSTER_NAME}.conf get machinedeployment
    aws-example-md-0   aws-example   4          3       4         1             ScalingUp   7m53s   v1.22.8

    In this example, there are two replicas, but only 1 is ready. One replica is unavailable, and the ScalingUp phase means a new Machine is being created.

  5. Identify the replacement Machine using this command:

    export NAME_NEW_MACHINE=$(kubectl --kubeconfig ${CLUSTER_NAME}.conf get machines \${CLUSTER_NAME}-md-0 \
    echo "$NAME_NEW_MACHINE"
    aws-example-md-0-cb9c9bbf7-hcl8z aws-example-md-0-cb9c9bbf7-rtdqw aws-example-md-0-cb9c9bbf7-td29r aws-example-md-0-cb9c9bbf7-w64kg

    If the output is empty, the new Machine has probably exited the Provisioning phase and entered the Running phase.

  6. Identify the replacement Node using this command:

    kubectl --kubeconfig ${CLUSTER_NAME}.conf get nodes 
    NAME                                         STATUS   ROLES                  AGE   VERSION   Ready    control-plane,master   20m   v1.22.8   Ready    control-plane,master   23m   v1.22.8   Ready    control-plane,master   22m   v1.22.8    Ready    <none>                 22m   v1.22.8    Ready    <none>                 22m   v1.22.8     Ready    <none>                 57s   v1.22.8     Ready    <none>                 22m   v1.22.8

    If the output is empty, the Node resource is not yet available, or does not yet have the expected annotation. Wait a few minutes, then repeat the command.