Gitlab Integration with Kubernetes

In this story, I will discuss how to integrate Gitlab with Kubernetes cluster.

I am assuming that Kubernetes cluster is already deployed.


A Gitlab project whose CI/CD pipeline needs to do some operations in Kubernetes cluster.

Pre Requisites

Integration Guidelines

  • Create a project on gitlab.
  • There are two ways to configure Gitlab with Kubernetes Cluster:

Method 1: Using Gitlab UI: This method requires manual steps(which can be automated but it will require a lot of effort). Guidelines can be found on this .

Mehtod 2: Using Gitlab CI/CD Environment Variables: In this method cluster's configurations(Kube config) will be stored as a Gitlab CI/CD environment variables, which can be accessed by the runner during pipeline execution.

I will explain the Method 2 in details below.

Integrations by using Gitlab CI/CD Environment Variables:

  • Extract your cluster’s Kube config. Convert your Kube config that usually resides in ~/.kube/config to base64 string and store it in a config.txt file by using the command given below:
cat ~/.kube/config | base64 > config.txt

The config stored in config.txt has an issue that it is not stored as a single base64 string but in different lines with \n at the end of each line. An example can be seen below:

The problem with this format is that it cannot be decoded by using the base64 utility.

  • The above issue can be resolved using a python script given below:

It will remove \n char from the end of each line and concatenate all the lines in a single string and finally print the result on the screen. Copy the result. There are many ways to skin this cat, it is one of them.

  • Now we have the config, we will store it as a Gitlab project CI/CD Environment variable so that it can be accessible in each pipeline. Location of CI/CD environment variable is as follows Project -> Settings -> CI/CD -> Variables . An example is given below:
  • Once the variable is stored. It will be accessible in pipeline runners.
  • Now add .gitlab-ci.yml file in the root of your project and paste the content given below in that file:

.gitlab-ci.yml file description

  • I have created an image that has kubectl utility already installed in it with some auxiliary packages. You can use your own.
  • In the before_script section, I am decoding Kube config and storing it in ~/.kube/config file.
  • In stages section I defining the stages the0 pipeline will have.
  • In deploy section I am specifying the stage and in script section, I am running a command that will return all the namespaces of the cluster.
  • If everything is configured correctly, the list of namespaces will be returned.


DevSecOps Engineer

DevSecOps Engineer