Consuming reserved zonal resources

Customers can reserve Compute Engine instances in a specific zone to ensure resources are available for their workloads when needed. For more details on how to manage reservations, go to Reserving Compute Engine zonal resources.

After creating reservations, you can create GKE clusters and node pools to consume the reserved instances. GKE supports the same consumption modes as Compute Engine:

Before you begin

Before you start, make sure you have performed the following tasks:

Set up default gcloud settings using one of the following methods:

  • Using gcloud init, if you want to be walked through setting defaults.
  • Using gcloud config, to individually set your project ID, zone, and region.

Using gcloud init

  1. Run gcloud init and follow the directions:

    gcloud init

    If you are using SSH on a remote server, use the --console-only flag to prevent the command from launching a browser:

    gcloud init --console-only
  2. Follow the instructions to authorize gcloud to use your Google Cloud account.
  3. Create a new configuration or select an existing one.
  4. Choose a Google Cloud project.
  5. Choose a default Compute Engine zone.

Using gcloud config

  • Set your default project ID:
    gcloud config set project [PROJECT_ID]
  • If you are working with zonal clusters, set your default compute zone:
    gcloud config set compute/zone [COMPUTE_ZONE]
  • If you are working with regional clusters, set your default compute region:
    gcloud config set compute/region [COMPUTE_REGION]
  • Update gcloud to the latest version:
    gcloud components update

Consuming reserved instaces in GKE

You can specify the reservation consumption mode by using --reservation-affinity when you create a cluster or node pool.

Consuming any matching reservations

To consume from any matching reservations automatically, set the reservation affinity flag to --reservation-affinity=any.

In this mode, nodes first take capacity from any matching reservations in the same zone, and if that's insufficient, fulfil the rest from the general Compute Engine resource pool. See How reservations work for details on how Compute Engine matches reservations.

To create a reservation and instances to consume any reservation, perform the following steps:

gcloud

  1. Create a reservation of 3 VMs:

    gcloud compute reservations create res1 --machine-type=n1-standard-2 \
     --vm-count=3
    
  2. Verify the reservation was created successfully:

    gcloud compute reservations describe res1
    
  3. Create a cluster to consume any matching reservation:

    gcloud container clusters create cluster1 --machine-type=n1-standard-2 \
     --num-nodes=1 --reservation-affinity=any
    
  4. Create a node pool to consume any matching reservation:

    gcloud container node-pools create np1 --cluster cluster1 \
     --machine-type=n1-standard-2 --num-nodes=3 --reservation-affinity=any
    

The total number of nodes is 4 which exceeds the capacity of the reservation. So 3 of the nodes consume the reservation while the last node takes capacity from the general Compute Engine resource pool.

Consuming a specific reservation

To consume a specific reservation, set the reservation affinity flag to --reservation-affinity=specific and provide the specific reservation name. In this mode, instances must take capacity from the specified reservation in the zone. The request fails if the reservation does not have sufficient capacity.

To create a reservation and instances to consume a specific reservation, perform the following steps:

gcloud

  1. Create a specific reservation of 3 VMs:

    gcloud compute reservations create res2 --machine-type=n1-standard-2 \
     --vm-count=3 --require-specific-reservation
    
  2. Create a cluster to consume any matching reservation:

    gcloud container clusters create cluster2 --machine-type=n1-standard-2 \
     --reservation-affinity specific --reservation=res2 --num-nodes 1
    
  3. Create a node pool to consume any matching reservation:

    gcloud container node-pools create np2 --cluster cluster2 \
     --machine-type=n1-standard-2 --reservation-affinity specific \
     --reservation=res2 --num-nodes 2
    

Additional considerations for consuming from a specific reservation

When a node pool is created with specific reservation affinity, including default node pools during cluster creation, its size is limited to the capacity of the specific reservation over the node pool's entire lifetime. This affects the following GKE features:

Cluster with multiple zones

In regional or multi-zonal clusters, nodes of a node pool can span across multiple zones. Since reservations are single-zonal, multiple reservations are needed. To create a node pool consuming specific reservation in these clusters, you must create a specific reservation with exactly the same name and machine properties in each zone of the node pool.

Cluster Autoscaling/Surge upgrade

If you don't have extra capacity in the specific reservation, surge upgrades or auto-scaling of the node pool might fail because both require creating extra instances. To resolve this, you can change the size of the reservation, or free up some of its bounded resources.

Creating nodes without consuming reservations

To explicitly avoid consuming resources from any reservations, set the affinity to --reservation-affinity=none.

gcloud

  1. Create a cluster to consume any matching reservation:

    gcloud container clusters create cluster3 --reservation-affinity=none
    
  2. Create a node pool to consume any matching reservation:

    gcloud container node-pools create np3 --cluster cluster3 \
     --reservation-affinity=none
    

Clean up

To delete the clusters you created, run the following commands:

gcloud container clusters delete cluster1
gcloud container clusters delete cluster2
gcloud container clusters delete cluster3

To delete the reservations, run the following commands:

gcloud compute reservations delete res1
gcloud compute reservations delete res2

What's next