Create a multi-tenant cluster using Terraform
A multi-tenant cluster in Google Kubernetes Engine (GKE) Enterprise edition is a Kubernetes cluster shared by multiple distinct teams or users, known as tenants. Each tenant typically has its own set of resources and applications within the cluster.
This Terraform tutorial lets you quickly create a GKE Enterprise
cluster shared by two teams, backend
and frontend
, that can deploy
team-specific workloads on the cluster. This tutorial assumes that you are
already familiar with Terraform. If not, you can use the following resources to
get familiar with the basics of Terraform:
Before you begin
Take the following steps to enable the Kubernetes Engine API:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE, GKE Hub, Cloud SQL, Resource Manager, IAM, Connect gateway APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE, GKE Hub, Cloud SQL, Resource Manager, IAM, Connect gateway APIs.
-
Make sure that you have the following role or roles on the project: roles/owner, roles/iam.serviceAccountTokenCreator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role colunn to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- In the Select a role list, select a role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
Prepare the environment
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/multitenant
Review the Terraform files
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.
Review the
main.tf
file, which describes a GKE Enterprise cluster resource:cat main.tf
The output is similar to the following:
Create a cluster and SQL database
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
Optional: 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: 23 added, 0 changed, 0 destroyed.
Deploy the backend team application
Review the following Terraform file:
cat backend.yaml
The output should be similar to the following:
This file describes the following resources:
- A Deployment with a sample application.
- A Service of type LoadBalancer.
The Service exposes the Deployment on port 80. To expose your application
to the internet, configure an external load balancer by removing the
networking.gke.io/load-balancer-type
annotation.
In Cloud Shell, run the following command to impersonate the backend team's service account:
gcloud config set auth/impersonate_service_account backend@PROJECT_ID.iam.gserviceaccount.com
Replace
PROJECT_ID
with your project ID.Retrieve the cluster credentials:
gcloud container fleet memberships get-credentials gke-enterprise-cluster --location us-central1
Apply the backend team's manifest to the cluster:
kubectl apply -f backend.yaml
Verify the backend application is working
Do the following to confirm your cluster is running correctly:
Go to the Workloads page in the Google Cloud console:
Click the
backend
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.Click the
backend
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 Endpoints section, click the IPv4 link to view your Service in the browser. The output is similar to the following:
Backend! Hostname: backendweb-765f6c4fc9-cl7jx Set Color: green
Whenever a user accesses the backend endpoint, the Service randomly picks and stores a color from red, green, or blue in the shared database.
Deploy a frontend team application
Review the following Terraform file:
cat frontend.yaml
The output should be similar to the following:
This file describes the following resources:
- A Deployment with a sample application.
- A Service of type LoadBalancer.
The Service exposes the Deployment on port 80. To expose your application
to the internet, configure an external load balancer by removing the
networking.gke.io/load-balancer-type
annotation.
In Cloud Shell, run the following command to impersonate the frontend team's service account:
gcloud config set auth/impersonate_service_account frontend@PROJECT_ID.iam.gserviceaccount.com
Replace
PROJECT_ID
with your project ID.Retrieve the cluster credentials:
gcloud container fleet memberships get-credentials gke-enterprise-cluster --location us-central1
Apply the frontend team's manifest to the cluster:
kubectl apply -f frontend.yaml
Verify the frontend application is working
Do the following to confirm your cluster is running correctly:
Go to the Workloads page in the Google Cloud console:
Click the
frontend
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.Click the
frontend
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 Endpoints section, click the IPv4 link to view your Service in the browser. The output is similar to the following:
Frontend! Hostname: frontendweb-5cd888d88f-gwwtc Got Color: green
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In Cloud Shell, run this command to unset service account impersonation:
gcloud config unset auth/impersonate_service_account
Run the following command to delete the Terraform resources:
terraform destroy --auto-approve
What's next
Explore the Terraform resources for GKE.
Explore opinionated configuration samples in the Terraform GKE module GitHub repository.