Create a multi-tenant cluster using Terraform
A multi-tenant cluster in Google Kubernetes Engine 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
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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE, GKE Hub, Cloud SQL, Resource Manager, IAM, Connect gateway APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE, GKE Hub, Cloud SQL, Resource Manager, IAM, Connect gateway APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 column 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_IDReplace
PROJECT_IDwith your project ID.Clone the GitHub repository:
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branchChange 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.tffile, which describes a GKE cluster resource:cat main.tfThe output is similar to the following:
Create a cluster and SQL database
In Cloud Shell, run this command to verify that Terraform is available:
terraformThe 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 infrastructureInitialize Terraform:
terraform initOptional: Plan the Terraform configuration:
terraform planApply the Terraform configuration
terraform applyWhen prompted, enter
yesto 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.yamlThe 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-typeannotation.
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.comReplace
PROJECT_IDwith your project ID.Retrieve the cluster credentials:
gcloud container fleet memberships get-credentials gke-enterprise-cluster --location us-central1Apply 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
backendworkload. 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
backendLoadBalancer 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: greenWhenever 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.yamlThe 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-typeannotation.
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.comReplace
PROJECT_IDwith your project ID.Retrieve the cluster credentials:
gcloud container fleet memberships get-credentials gke-enterprise-cluster --location us-central1Apply 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
frontendworkload. 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
frontendLoadBalancer 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_accountRun 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.