Cloud Composer 3 | Cloud Composer 2 | Cloud Composer 1
This page describes how to use the Google Kubernetes Engine operators to create clusters in Google Kubernetes Engine and to launch Kubernetes Pods in those clusters.
Google Kubernetes Engine operators run Kubernetes Pods in a specified cluster,
which can be a separate cluster that is not related to your environment.
In comparison, KubernetesPodOperator runs Kubernetes Pods
in the cluster of your environment.
This page walks you through an example DAG that creates a Google Kubernetes Engine
cluster with the GKECreateClusterOperator, uses the GKEStartPodOperator
with the following configurations, and then deletes it with
the GKEDeleteClusterOperator afterward:
- Minimal configuration: Sets only the required parameters.
- Template configuration: Uses parameters that you can template with Jinja.
- Pod affinity configuration: Limits the available nodes to schedule Pods on.
- Full configuration: Includes all configurations.
Before you begin
GKE operator configuration
To follow along with this example, put the entire gke_operator.py
file in your environment's dags/ folder or
add the relevant code to a DAG.
Create a cluster
The code shown here creates a Google Kubernetes Engine cluster with two node pools,
pool-0 and pool-1, each of which has one node. If needed, you can set
other parameters from the Google Kubernetes Engine API as part of the body.
We recommend using regional clusters. Zonal clusters are more exposed to zonal
failures. For example, you might want to use the us-central1 region for your
cluster instead of the us-central1-a zone.
For more information about region-specific considerations, see
Geography and regions.
Launch workloads in the cluster
The following sections explain each GKEStartPodOperator configuration
in the example. For information about each configuration variable, see
the Airflow reference for GKE operators.
Minimal configuration
To launch a Pod in your GKE cluster with
the GKEStartPodOperator, only the project_id, location, cluster_name,
name, namespace, image, and task_id options are required.
When you place the following code snippet in a DAG, the pod-ex-minimum task
succeeds as long as the previously listed parameters are defined and valid.
Template configuration
Airflow supports using
Jinja Templating.
You must declare the required variables (task_id, name, namespace,
and image) with the operator. As shown in the following example, you can
template all other parameters with Jinja, including cmds, arguments,
and env_vars.
Without changing the DAG or your environment, the ex-kube-templates task
fails. Set an Airflow variable called my_value to make this DAG succeed.
To set my_value with gcloud or the Airflow UI:
gcloud
Enter the following command:
gcloud composer environments run ENVIRONMENT \
--location LOCATION \
variables set -- \
my_value example_value
Replace:
ENVIRONMENTwith the name of the environment.LOCATIONwith the region where the environment is located.
Airflow UI
In the Airflow 3 UI:
In the toolbar, select Admin > Variables.
Click Add variable.
On the Add Variable page, enter the following information:
- Key:
my_value - Value:
example_value
- Key:
Click Save.
In the Airflow 2 UI:
In the toolbar, select Admin > Variables.
On the List Variable page, click Add a new record.
On the Add Variable page, enter the following information:
- Key:
my_value - Val:
example_value
- Key:
Click Save.
Template configuration:
Pod Affinity Configuration
When you configure the affinity parameter in the GKEStartPodOperator, you
control what nodes to schedule Pods on, such as nodes only in a particular
node pool. When you created your cluster, you created two node pools named
pool-0 and pool-1. This operator dictates that Pods must run only in
pool-1.
Full Configuration
This example shows all the variables that you can configure in
the GKEStartPodOperator. You don't need to modify the code for
the ex-all-configs task to succeed.
For details on each variable, see the Airflow reference for GKE operators.
Delete the cluster
The code shown here deletes the cluster that was created at the beginning of the guide.