- Import the example project
- Register the agent
- Create your GCP credentials
- Configure your project
- Enable Kubernetes Engine API
- Provision your cluster
- Use your cluster
- Remove the cluster
Create a Google GKE cluster
Learn how to create a new cluster on Google Kubernetes Engine (GKE) through Infrastructure as Code (IaC). This process uses the Google and Kubernetes Terraform providers create GKE clusters. You connect the clusters to GitLab by using the GitLab agent for Kubernetes.
Prerequisites:
- A Google Cloud Platform (GCP) service account.
- A runner you can use to run the GitLab CI/CD pipeline.
Steps:
- Import the example project.
- Register the agent for Kubernetes.
- Create your GCP credentials.
- Configure your project.
- Provision your cluster.
Import the example project
To create a cluster from GitLab using Infrastructure as Code, you must create a project to manage the cluster from. In this tutorial, you start with a sample project and modify it according to your needs.
Start by importing the example project by URL.
To import the project:
- In GitLab, on the left sidebar, select Search or go to.
- Select View all my projects.
- On the right of the page, select New project.
- Select Import project.
- Select Repository by URL.
- For the Git repository URL, enter
https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-gke.git
. - Complete the fields and select Create project.
This project provides you with:
- A cluster on Google Cloud Platform (GCP) with defaults for name, location, node count, and Kubernetes version.
- The GitLab agent for Kubernetes installed in the cluster.
Register the agent
certificate_based_clusters
changed the Actions menu to focus on the agent rather than certificates. The flag is enabled on GitLab.com, GitLab Dedicated, and self-managed.To create a GitLab agent for Kubernetes:
- On the left sidebar, select Operate > Kubernetes clusters.
- Select Connect a cluster (agent).
- From the Select an agent or enter a name to create new dropdown list, choose your agent’s name and select Register.
- GitLab generates a registration token for the agent. Securely store this secret token, as you will need it later.
- Optional. If you use Helm, GitLab provides an address for the agent server (KAS) in the Helm command example. You need this for later.
Create your GCP credentials
To set up your project to communicate to GCP and the GitLab API:
- To authenticate GCP with GitLab, create a GCP service account
with following roles:
Compute Network Viewer
,Kubernetes Engine Admin
,Service Account User
, andService Account Admin
. Both User and Admin service accounts are necessary. The User role impersonates the default service account when creating the node pool. The Admin role creates a service account in thekube-system
namespace. - Download the JSON file with the service account key you created in the previous step.
-
On your computer, encode the JSON file to
base64
(replace/path/to/sa-key.json
to the path to your key):MacOSbase64 -i /path/to/sa-key.json | tr -d \\n
Linuxbase64 /path/to/sa-key.json | tr -d \\n
- Use the output of this command as the
BASE64_GOOGLE_CREDENTIALS
environment variable in the next step.
Configure your project
Use CI/CD environment variables to configure your project.
Required configuration:
- On the left sidebar, select Settings > CI/CD.
- Expand Variables.
- Set the variable
BASE64_GOOGLE_CREDENTIALS
to thebase64
encoded JSON file you just created. - Set the variable
TF_VAR_gcp_project
to your GCPproject
ID. - Set the variable
TF_VAR_agent_token
to the agent token displayed in the previous task. - Set the variable
TF_VAR_kas_address
to the agent server address displayed in the previous task.
Optional configuration:
The file variables.tf
contains other variables that you can override according to your needs:
-
TF_VAR_gcp_region
: Set your cluster’s region. -
TF_VAR_cluster_name
: Set your cluster’s name. -
TF_VAR_cluster_description
: Set a description for the cluster. We recommend setting this to$CI_PROJECT_URL
to create a reference to your GitLab project on your GCP cluster detail page. This way you know which project was responsible for provisioning the cluster you see on the GCP dashboard. -
TF_VAR_machine_type
: Set the machine type for the Kubernetes nodes. -
TF_VAR_node_count
: Set the number of Kubernetes nodes. -
TF_VAR_agent_namespace
: Set the Kubernetes namespace for the GitLab agent.
Refer to the Google Terraform provider and the Kubernetes Terraform provider documentation for further resource options.
Enable Kubernetes Engine API
From the Google Cloud console, enable the Kubernetes Engine API.
Provision your cluster
After configuring your project, manually trigger the provisioning of your cluster. In GitLab:
- On the left sidebar, select Build > Pipelines.
- Select Run pipeline.
- Next to Play (), select the dropdown list icon ().
- Select Deploy to manually trigger the deployment job.
When the pipeline finishes successfully, you can see your new cluster:
- In GCP: on your GCP console’s Kubernetes list.
- In GitLab: from your project’s sidebar, select Operate > Kubernetes clusters.
Use your cluster
After you provision the cluster, it is connected to GitLab and is ready for deployments. To check the connection:
- On the left sidebar, select Operate > Kubernetes clusters.
- In the list, view the Connection status column.
For more information about the capabilities of the connection, see the GitLab agent for Kubernetes documentation.
Remove the cluster
A cleanup job is not included in your pipeline by default. To remove all created resources, you must modify your GitLab CI/CD template before running the cleanup job.
To remove all resources:
-
Add the following to your
.gitlab-ci.yml
file:stages: - init - validate - build - test - deploy - cleanup destroy: extends: .terraform:destroy needs: []
- On the left sidebar, select Build > Pipelines and select the most recent pipeline.
- For the
destroy
job, select Play ().