A Kubernetes cluster provides compute, storage, networking, and other services for applications, similar to a virtual data center. Apps and their associated services that run in Kubernetes are called workloads.
This tutorial lets you quickly see a running Google Kubernetes Engine cluster and sample workload, all set up using Terraform. You can then explore the workload in the Google Cloud console before going on to our more in-depth learning path, or to start planning and creating your own production-ready cluster. This tutorial assumes that you are already familiar with Terraform.
If you'd prefer to set up your sample cluster and workload in the Google Cloud console, see Create a cluster in the Google Cloud console.
What is Terraform? Infrastructure as Code (IaC) is a practice of managing and provisioning software infrastructure resources using code. Terraform is a popular open source IaC tool created by HashiCorp that supports a wide range of Cloud services, including GKE. As a GKE platform administrator, you can use Terraform to standardize configuration of your Kubernetes clusters in production and streamline your DevOps workflows. To learn more, see Terraform support for GKE. Before you beginTake the following steps to enable the Kubernetes Engine API:
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud init
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace PROJECT_ID
with a name for the Google Cloud project you are creating.
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace PROJECT_ID
with your Google Cloud project name.
Verify that billing is enabled for your Google Cloud project.
Enable the GKE API:
gcloud services enable container.googleapis.com
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud init
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace PROJECT_ID
with a name for the Google Cloud project you are creating.
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace PROJECT_ID
with your Google Cloud project name.
Verify that billing is enabled for your Google Cloud project.
Enable the GKE API:
gcloud services enable container.googleapis.com
Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/container.admin, roles/compute.networkAdmin, roles/iam.serviceAccountUser
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account—for example, myemail@example.com
.ROLE
: the IAM role that you grant to your user account.In this tutorial you use Cloud Shell to manage resources hosted on Google Cloud. Cloud Shell is preinstalled with the software you need for this tutorial, including Terraform, kubectl
, and the the Google Cloud CLI.
Launch a Cloud Shell session from the Google Cloud console, by clicking the Cloud Shell activation icon Activate Cloud Shell . This launches a session in the bottom pane of the Google Cloud console.
The service credentials associated with this virtual machine are automatic, so you don't have to set up or download a service account key.
Before you run commands, set your default project in the gcloud CLI using the following command:
gcloud config set project PROJECT_ID
Replace PROJECT_ID
with your project ID.
Clone the GitHub repository:
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
Change to the working directory:
cd terraform-docs-samples/gke/quickstart/autopilot
The Google Cloud provider is a plugin that lets you manage and provision Google Cloud resources using Terraform. It serves as a bridge between Terraform configurations and Google Cloud APIs, letting you declaratively define infrastructure resources, such as virtual machines and networks.
The cluster and sample app for this tutorial are specified in two Terraform files that use the Google Cloud and Kubernetes providers.
Review the cluster.tf
file:
cat cluster.tf
The output is similar to the following
This file describes the following resources:
google_compute_network
: a VPC network with internal IPv6 enabled.google_compute_subnetwork
: a dual-stack subnetwork.google_container_cluster
: a dual-stack Autopilot mode cluster located in us-central1
. The deletion_protection
setting controls whether you can use Terraform to delete this cluster. If you set the value in the deletion_protection
field to false
, Terraform can delete the cluster. For details, see the google_container_cluster
reference.Review the app.tf
file:
cat app.tf
The output is similar to the following:
This file describes the following resources:
The Terraform files for the example describe an application with an internal IP address, which can only be accessed from the same Virtual Private Cloud (VPC) as the sample app. If you want to access the running demo app's web interface from the internet (for example, from your laptop), modify the Terraform files to create a public IP address instead before you create the cluster. You can do this using a text editor directly in Cloud Shell or by using the Cloud Shell Editor.
To expose the demo application to the internet:
In cluster.tf
, change ipv6_access_type
from INTERNAL
to EXTERNAL
.
ipv6_access_type = "EXTERNAL"
In app.tf
, configure an external load balancer by removing the networking.gke.io/load-balancer-type
annotation.
annotations = {
"networking.gke.io/load-balancer-type" = "Internal" # Remove this line
}
In Cloud Shell, run this command to verify that Terraform is available:
terraform
The output should be similar to the following:
Usage: terraform [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
Initialize Terraform:
terraform init
Plan the Terraform configuration:
terraform plan
Apply the Terraform configuration
terraform apply
When prompted, enter yes
to confirm actions. This command might take several minutes to complete. The output is similar to the following:
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
Do the following to confirm your cluster is running correctly:
Go to the Workloads page in the Google Cloud console:
Click the example-hello-app-deployment
workload. The Pod details page displays. This page shows information about the Pod, such as annotations, containers running on the Pod, Services exposing the Pod, and metrics including CPU, Memory, and Disk usage.
Go to the Services & Ingress page in the Google Cloud console:
Click the example-hello-app-loadbalancer
LoadBalancer Service. The Service details page displays. This page shows information about the Service, such as the Pods associated with the Service, and the Ports the Services uses.
In the External endpoints section, click the IPv4 link or the IPv6 link to view your Service in the browser. The output is similar to the following:
Hello, world!
Version: 2.0.0
Hostname: example-hello-app-deployment-5df979c4fb-kdwgr
To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.
If you plan to take additional tutorials or to explore your sample further, wait until you're finished to perform this cleanup step.
In Cloud Shell, run the following command to delete the Terraform resources:
terraform destroy --auto-approve
If you see an error message similar to The network resource 'projects/PROJECT_ID/global/networks/example-network' is already being used by 'projects/PROJECT_ID/global/firewalls/example-network-yqjlfql57iydmsuzd4ot6n5v'
, do the following:
Delete the firewall rules:
gcloud compute firewall-rules list --filter="NETWORK:example-network" --format="table[no-heading](name)" | xargs gcloud --quiet compute firewall-rules delete
Re-run the Terraform command:
terraform destroy --auto-approve
Explore your cluster and workload in the Google Cloud console to learn about the some of the key workload settings and resources that you deployed.
Learn more about setting up and using Terraform with GKE in Terraform support for GKE.
Try our more in-depth Learning path: Scalable apps.
Learn how to get started with real life cluster administration in our Cluster administration overview.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4