Create a Kubernetes cluster using GitLab

In this article, I will show you how to create a Kubernetes cluster using GitLab. This is done using the GitLab Agent for Kubernetes, which allows you to create, update and manage your Kubernetes clusters as part of your GitLab setup.

4 minutes reading time

Written by

Ian Banks
Ian Banks

SRE Team Lead at Civo

In this article, I will show you how to create a Civo Kubernetes cluster using GitLab. This is done using the GitLab Agent for Kubernetes, which allows you to create, update, and manage your Kubernetes clusters as part of your GitLab setup. To follow along with this tutorial, you will need a GitLab account alongside a Civo account.

For more information about the capabilities of the connection, see the GitLab agent for Kubernetes documentation.

Setting up the repository

The first step is to set up a project from your GitLab account.

Create a new project:

Create a new project

From the given options, choose "import project":

choose import project

Enter the Civo Gitlab Terraform Git repository URL (https://gitlab.com/civocloud/gitlab-terraform-civo.git) for the import and create the project:

Create a new project2

Agent registration

Once the project is created successfully, the next step is to create a GitLab agent for Kubernetes.

After this, you need to select the Kubernetes clusters under "infrastructure" which can be found in the left sidebar:

Agent registration

Select "connect a cluster":

Select connect a cluster

Choose civo-agent from the drop-down list and click "register":

click "register"

GitLab will generate a secret token that will be used later on:

secret token generation

Configuring project environment variables

You need to configure a few environment variables in your project from the information you got after creating the agent.

Below is the list of variables you need to configure under the Settings > CI/CD > Variables section of your project:

Configuring project environment variables
Add variables

You will need to use the Variables section to save the following variable names, with the values described.

  • BASE64_CIVO_TOKEN: the token from your Civo account.
  • TF_VAR_agent_token: the agent token you received in the previous step.
  • TF_VAR_kas_address: the agent server address in the previous step.
variable names, with the values described

Optional configuration:

The file variables.tf in the linked repository contains other pre-defined variables that you can override according to your needs:

  • TF_VAR_region: Set your cluster's region from the available Civo regions.
  • TF_VAR_cluster_name: Set your cluster's name.
  • TF_VAR_cluster_description: Set a description for the cluster. To create a reference to your GitLab project on your Civo cluster detail page, set this value to $CI_PROJECT_URL. This value helps you determine which project was responsible for provisioning the cluster you see on the Civo dashboard.
  • TF_VAR_target_nodes_size: Set the size of the nodes to use for the cluster.
  • TF_VAR_num_target_nodes: Set the number of Kubernetes nodes.
  • TF_VAR_agent_version: Set the version of the GitLab agent.
  • TF_VAR_agent_namespace: Set the Kubernetes namespace for the GitLab agent.

Refer to the Civo Terraform provider and the Kubernetes Terraform provider documentation for further resource options.

For this demo, I will use the default values from the variables.tf file that is present in the project.

variables.tf file

Provisioning the cluster

Now you can go to your project pipelines from the CI/CD section.

Provisioning the cluster

Now run the pipeline for the magic to start (no magic as it is all Terraform!)

run the pipeline

Once you hit the deploy button, you will be able to see the Kubernetes cluster created and then the GitLab agent being installed on that cluster. You can also see the cluster in your Civo dashboard.

cluster in your Civo dashboard

If you download the KUBECONFIG file for the cluster from the cluster page on Civo, and set it to be your current KUBECONFIG, you will be able to see the agent deployed.

kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system civo-ccm-7cb9c4b58f-tp5fp 1/1 Running 0 7h12m
kube-system civo-csi-node-79p2m 2/2 Running 0 7h11m
kube-system civo-csi-node-2rjq4 2/2 Running 0 7h11m
kube-system civo-csi-node-hrgjf 2/2 Running 0 7h11m
kube-system coredns-85cb69466-j4dqt 1/1 Running 0 7h12m
kube-system civo-csi-controller-0 3/3 Running 0 7h12m
gitlab-agent gitlab-agent-8fb766c7-gzgzx 1/1 Running 0 7h11m

That's it! You have successfully created a Civo Kubernetes cluster with a GitLab agent installed from within GitLab.

GitLab agent installed from within GitLab

Conclusion

The GitLab Agent will be aware of the state of your Kubernetes cluster, and be able to make adjustments to it based on your CI/CD and Terraform pipelines. Whether you're aiming for a GitOps workflow or to use the CI/CD process of managing applications, the Agent will be set up with the parameters you choose.

Ian Banks
Ian Banks

SRE Team Lead at Civo

As a seasoned IT professional with over 15 years of experience, Ian has honed his skills in cloud engineering, DevOps, and site reliability, holding various roles across multiple companies. At Civo, Ian has been an integral part of the team as a Site Reliability Engineer and SRE Team Lead since 2019, bringing his expertise to the table. With a strong background in managing complex infrastructure, Ian is well-equipped to drive reliability and innovation.

View author profile