Skip to content

googleContainerNodePool

-> 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.

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleContainerClusterPrimary =
  new google.containerCluster.ContainerCluster(this, "primary", {
    initial_node_count: 1,
    location: "us-central1",
    name: "my-gke-cluster",
    remove_default_node_pool: true,
  });
const googleServiceAccountDefault = new google.serviceAccount.ServiceAccount(
  this,
  "default",
  {
    account_id: "service-account-id",
    display_name: "Service Account",
  }
);
new google.containerNodePool.ContainerNodePool(
  this,
  "primary_preemptible_nodes",
  {
    cluster: googleContainerClusterPrimary.id,
    name: "my-node-pool",
    node_config: [
      {
        machine_type: "e2-medium",
        oauth_scopes: ["https://www.googleapis.com/auth/cloud-platform"],
        preemptible: true,
        service_account: googleServiceAccountDefault.email,
      },
    ],
    node_count: 1,
  }
);

Example Usage - 2 node pools, 1 separately managed + the default node pool

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleServiceAccountDefault = new google.serviceAccount.ServiceAccount(
  this,
  "default",
  {
    account_id: "service-account-id",
    display_name: "Service Account",
  }
);
const googleContainerClusterPrimary =
  new google.containerCluster.ContainerCluster(this, "primary", {
    initial_node_count: 3,
    location: "us-central1-a",
    name: "marcellus-wallace",
    node_config: [
      {
        guest_accelerator: [
          {
            count: 1,
            type: "nvidia-tesla-k80",
          },
        ],
        oauth_scopes: ["https://www.googleapis.com/auth/cloud-platform"],
        service_account: googleServiceAccountDefault.email,
      },
    ],
    node_locations: ["us-central1-c"],
  });
new google.containerNodePool.ContainerNodePool(this, "np", {
  cluster: googleContainerClusterPrimary.id,
  name: "my-node-pool",
  node_config: [
    {
      machine_type: "e2-medium",
      oauth_scopes: ["https://www.googleapis.com/auth/cloud-platform"],
      service_account: googleServiceAccountDefault.email,
    },
  ],
  timeouts: [
    {
      create: "30m",
      update: "20m",
    },
  ],
});

Argument Reference

  • cluster - (Required) The cluster to create the node pool for. Cluster must be present in location provided for clusters. May be specified in the format projects/{{project}}/locations/{{location}}/clusters/{{cluster}} or as just the name of the cluster.

  • location - (Optional) The location (region or zone) of the cluster.

  • autoscaling - (Optional) Configuration required by cluster autoscaler to adjust the size of the node pool to the current cluster usage. Structure is documented below.

  • initialNodeCount - (Optional) 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. WARNING: Resizing your node pool manually may change this value in your existing cluster, which will trigger destruction and recreation on the next Terraform run (to rectify the discrepancy). If you don't need this value, don't set it. If you do need it, you can use a lifecycle block to ignore subsequent changes to this field.

  • management - (Optional) Node management configuration, wherein auto-repair and auto-upgrade is configured. Structure is documented below.

  • maxPodsPerNode - (Optional) 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. See the official documentation for more information.

  • nodeLocations - (Optional) 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 nodeLocations will be used.

-> Note: nodeLocations will not revert to the cluster's default set of zones upon being unset. You must manually reconcile the list of zones with your cluster.

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

  • namePrefix - (Optional) Creates a unique name for the node pool beginning with the specified prefix. Conflicts with name.

  • nodeConfig - (Optional) Parameters used in creating the node pool. See google_container_cluster for schema.

  • networkConfig - (Optional) The network configuration of the pool. Such as configuration for Adding Pod IP address ranges) to the node pool. Or enabling private nodes. Structure is documented below

  • nodeCount - (Optional) 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.

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

  • upgradeSettings (Optional) Specify node upgrade settings to change how GKE upgrades nodes. The maximum number of nodes upgraded simultaneously is limited to 20. Structure is documented below.

  • version - (Optional) The Kubernetes version for the nodes in this pool. Note that if this field and autoUpgrade 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 googleContainerEngineVersions data source's versionPrefix field to approximate fuzzy versions in a Terraform-compatible way.

  • placementPolicy - (Optional) Specifies a custom placement policy for the nodes.

The autoscaling block supports (either total or per zone limits are required):

  • minNodeCount - (Optional) Minimum number of nodes per zone in the NodePool. Must be >=0 and <= maxNodeCount. Cannot be used with total limits.

  • maxNodeCount - (Optional) Maximum number of nodes per zone in the NodePool. Must be >= min_node_count. Cannot be used with total limits.

  • totalMinNodeCount - (Optional) Total minimum number of nodes in the NodePool. Must be >=0 and <= totalMaxNodeCount. Cannot be used with per zone limits. Total size limits are supported only in 1.24.1+ clusters.

  • totalMaxNodeCount - (Optional) Total maximum number of nodes in the NodePool. Must be >= total_min_node_count. Cannot be used with per zone limits. Total size limits are supported only in 1.24.1+ clusters.

  • locationPolicy - (Optional) Location policy specifies the algorithm used when scaling-up the node pool. Location policy is supported only in 1.24.1+ clusters.

    • "BALANCED" - Is a best effort policy that aims to balance the sizes of available zones.
    • "ANY" - Instructs the cluster autoscaler to prioritize utilization of unused reservations, and reduce preemption risk for Spot VMs.

The management block supports:

  • autoRepair - (Optional) Whether the nodes will be automatically repaired.

  • autoUpgrade - (Optional) Whether the nodes will be automatically upgraded.

The networkConfig block supports:

  • createPodRange - (Optional) Whether to create a new range for pod IPs in this node pool. Defaults are provided for podRange and podIpv4CidrBlock if they are not specified.

  • enablePrivateNodes - (Optional) Whether nodes have internal IP addresses only.

  • podIpv4CidrBlock - (Optional) The IP address range for pod IPs in this node pool. Only applicable if createPodRange is true. Set to blank to have a range chosen with the default size. Set to /netmask (e.g. /14) to have a range chosen with a specific netmask. Set to a CIDR notation (e.g. 10.96.0.0/14) to pick a specific range to use.

  • podRange - (Optional) The ID of the secondary range for pod IPs. If createPodRange is true, this ID is used for the new range. If createPodRange is false, uses an existing secondary range with this ID.

The upgradeSettings block supports:

  • maxSurge - (Optional) The number of additional nodes that can be added to the node pool during an upgrade. Increasing maxSurge raises the number of nodes that can be upgraded simultaneously. Can be set to 0 or greater.

  • maxUnavailable - (Optional) The number of nodes that can be simultaneously unavailable during an upgrade. Increasing maxUnavailable raises the number of nodes that can be upgraded in parallel. Can be set to 0 or greater.

maxSurge and maxUnavailable must not be negative and at least one of them must be greater than zero.

  • strategy - (Default surge) The upgrade stragey to be used for upgrading the nodes.

  • blueGreenSettings - (Optional) The settings to adjust blue green upgrades. Structure is documented below

The blueGreenSettings block supports:

  • standardRolloutPolicy - (Required) Specifies the standard policy settings for blue-green upgrades.

    • batchPercentage - (Optional) Percentage of the blue pool nodes to drain in a batch.
    • batchNodeCount - (Optional) Number of blue nodes to drain in a batch.
    • batchSoakDuration - (Optionial) Soak time after each batch gets drained.
  • nodePoolSoakDuration - (Optional) Time needed after draining the entire blue pool. After this period, the blue pool will be cleaned up.

The placementPolicy block supports:

  • type - (Required) The type of the policy. Supports a single value: COMPACT. Specifying COMPACT placement policy type places node pool's nodes in a closer physical proximity in order to reduce network latency between nodes.

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format {{project}}/{{location}}/{{cluster}}/{{name}}

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

  • managedInstanceGroupUrls - List of instance group URLs which have been assigned to this node pool.

Timeouts

googleContainerNodePool provides the following Timeouts configuration options: configuration options:

  • create - (Default 30Minutes) Used for adding node pools
  • update - (Default 30Minutes) Used for updates to node pools
  • delete - (Default 30Minutes) Used for removing node pools.

Import

Node pools can be imported using the project, location, cluster and name. If the project is omitted, the project value in the provider configuration will be used. Examples:

$ terraform import google_container_node_pool.mainpool my-gcp-project/us-east1-a/my-cluster/main-pool

$ terraform import google_container_node_pool.mainpool us-east1/my-cluster/main-pool