Google Kubernetes (Container) Engine Node Pool

This page shows how to write Terraform for Kubernetes (Container) Engine Node Pool and write them securely.

google_container_node_pool (Terraform)

The Node Pool in Kubernetes (Container) Engine can be configured in Terraform with the resource name google_container_node_pool. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

container_node_pool_test.tf#L21
resource "google_container_node_pool" "default_regional" {
  name    = "default"
  cluster = google_container_cluster.default_regional.id
}

resource "google_container_node_pool" "with_node_config_regional" {
main.tf#L95
resource "google_container_node_pool" "node_pool" {
  provider = google-beta

  name     = "private-pool"
  project  = var.project
  location = var.location
positive.tf#L1
resource "google_container_node_pool" "positive1" {
  name       = "my-node-pool"
  location   = "us-central1-a"
  cluster    = google_container_cluster.primary.name
  node_count = 3

container_node_pool_test.tf#L21
resource "google_container_node_pool" "default_regional" {
  name    = "default"
  cluster = google_container_cluster.default_regional.id
}

resource "google_container_node_pool" "with_node_config_regional" {

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Security Best Practices for google_container_node_pool

There are 4 settings in google_container_node_pool that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

Ensure to use Container-Optimized OS (cos) for node images

It is better to use Container-Optimized OS (cos) for node images. GKE supports several OS image types. However, COS_CONTAINERD should be used for enhanced security.

risk-label

Ensure Node metadata values of your GKE cluster is disabled

It's better to disable Node metadata values of the GKE cluster to prevent unnecessary exposure to set the default value "SECURE".

risk-label

Ensure the auto repair of your GKE cluster is enabled

It is better to enable the auto repair of the GKE cluster for unexpected incidents.

risk-label

Ensure the auto upgrade of your GKE cluster is enabled

It is better to enable the auto upgrade of the GKE cluster for keeping the nodes in the cluster up-to-date.

Review your Google Kubernetes (Container) Engine settings

You can check if the google_container_node_pool setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

The cluster to create the node pool for. Cluster must be present in location provided for zonal clusters.

The initial number of nodes for the pool. In regional or multi-zonal clusters, this is the number of nodes per zone. Changing this will force recreation of the resource.

The resource URLs of the managed instance groups associated with this node pool.

The location (region or zone) of the cluster.

The maximum number of pods per node in this node pool. Note that this does not work on node pools which are "route-based" - that is, node pools belonging to clusters that do not have IP Aliasing enabled.

  • name optional computed - string

The name of the node pool. If left blank, Terraform will auto-generate a unique name.

Creates a unique name for the node pool beginning with the specified prefix. Conflicts with name.

The number of nodes per instance group. This field can be used to update the number of nodes per instance group but should not be used alongside autoscaling.

The list of zones in which the node pool's nodes should be located. Nodes must be in the region of their regional cluster or in the same region as their cluster's zone for zonal clusters. If unspecified, the cluster-level node_locations will be used.

The ID of the project in which to create the node pool. If blank, the provider-configured project will be used.

The Kubernetes version for the nodes in this pool. Note that if this field and auto_upgrade are both specified, they will fight each other for what the node version should be, so setting both is highly discouraged. While a fuzzy version can be specified, it's recommended that you specify explicit versions as Terraform will see spurious diffs when fuzzy versions are used. See the google_container_engine_versions data source's version_prefix field to approximate fuzzy versions in a Terraform-compatible way.

  • autoscaling list block

    Maximum number of nodes in the NodePool. Must be >= min_node_count.

    Minimum number of nodes in the NodePool. Must be >=0 and <= max_node_count.

  • management list block

    Whether the nodes will be automatically repaired.

    Whether the nodes will be automatically upgraded.

  • node_config list block

    Size of the disk attached to each node, specified in GB. The smallest allowed disk size is 10GB.

    Type of the disk attached to each node.

    List of the type and count of accelerator cards attached to the instance.

    The image type to use for this node. Note that for a given image type, the latest version of it will be used.

    • labels optional computed - map from string to string

    The map of Kubernetes labels (key/value pairs) to be applied to each node. These will added in addition to any default label(s) that Kubernetes may apply to the node.

    The number of local SSD disks to be attached to the node.

    The name of a Google Compute Engine machine type.

    • metadata optional computed - map from string to string

    The metadata key/value pairs assigned to instances in the cluster.

    Minimum CPU platform to be used by this instance. The instance may be scheduled on the specified or newer CPU platform.

    The set of Google API scopes to be made available on all of the node VMs.

    Whether the nodes are created as preemptible VM instances.

    The Google Cloud Platform Service Account to be used by the node VMs.

    • tags optional - list of string

    The list of instance tags applied to all nodes.

    • taint optional computed - list of object

    List of Kubernetes taints to be applied to each node.

  • timeouts single block
  • upgrade_settings list block

    The number of additional nodes that can be added to the node pool during an upgrade. Increasing max_surge raises the number of nodes that can be upgraded simultaneously. Can be set to 0 or greater.

    The number of nodes that can be simultaneously unavailable during an upgrade. Increasing max_unavailable raises the number of nodes that can be upgraded in parallel. Can be set to 0 or greater.

Explanation in Terraform Registry

-> See the Using GKE with Terraform guide for more information about using GKE with Terraform. Manages a node pool in a Google Kubernetes Engine (GKE) cluster separately from the cluster control plane. For more information see the official documentation and the API reference.

Tips: Best Practices for The Other Google Kubernetes (Container) Engine Resources

In addition to the google_container_cluster, Google Kubernetes (Container) Engine has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

google_container_cluster

Ensure legacy authentication is disabled

The legacy authentication for your GKE cluster is enabled. It is better to use OAuth authentication instead.

Review your Google Kubernetes (Container) Engine settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Frequently asked questions

What is Google Kubernetes (Container) Engine Node Pool?

Google Kubernetes (Container) Engine Node Pool is a resource for Kubernetes (Container) Engine of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Kubernetes (Container) Engine Node Pool?

For Terraform, the infracost/infracost, UCDenver-ccp/Translator-TM-Provider-Infrastructure-Modules and Checkmarx/kics source code examples are useful. See the Terraform Example section for further details.