How to integrate Kubernetes with Gitlab
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Integrating Kubernetes with GitLab can streamline your DevOps processes by enabling seamless deployment and management of containerized applications. GitLab offers native Kubernetes integration, allowing you to automate the deployment, scaling, and management of your applications without leaving the GitLab interface. This article provides a comprehensive guide on how to integrate Kubernetes with GitLab, including setup and configuration, along with technical explanations and examples.
Prerequisites
Before diving into the integration process, ensure the following prerequisites are met:
- A GitLab account with administrator access.
- A Kubernetes cluster you have administrative access to. This could be a cloud-hosted service like Google Kubernetes Engine (GKE), Amazon EKS, or a self-managed cluster.
kubectlcommand line tool installed for interacting with Kubernetes clusters.- Helm installed, which is a package manager for Kubernetes.
Setting up the GitLab Kubernetes Integration
Step 1: Access GitLab Kubernetes Integration
- Log in to your GitLab account.
- Navigate to the project you want to integrate with Kubernetes.
- Go to Settings -> CI/CD.
- Expand the Kubernetes section.
Step 2: Connect Kubernetes Cluster to GitLab
Obtain Kubernetes Credentials
To connect your Kubernetes cluster, you’ll need:
- API URL of your Kubernetes cluster.
- The service account token for authentication.
- CA certificate data (for securing communications).
Generate Kubernetes Service Account
Create a ServiceAccount in your Kubernetes cluster for GitLab:
- kind: ServiceAccount
- Kubernetes API URL: Your cluster's API URL.
- CA Certificate: Paste the CA certificate data.
- Service Token: The token you retrieved.
- Namespace: Usually
defaultor a specific namespace you prefer.- build
- deploy
- echo "Building the image"
- docker build -t registry.gitlab.com/CI_COMMIT_SHORT_SHA .
- main
- echo "Deploying to Kubernetes cluster"
- kubectl apply -f k8s/deployment.yaml
- main
- Stages: Define two stages—build and deploy.
- Build Stage: Build a Docker image and tag it.
- Deploy Stage: Use
kubectlto apply Kubernetes configurations located ink8s/deployment.yaml.- echo "Deploying with Helm"
- helm upgrade --install myapp ./mychart
- main
Related reading
- How to invoke the Pod proxy verb using the Kubernetes Go client?
- How to kill pods on Kubernetes local setup
- How to know a Pod's own IP address from inside a container in the Pod?
- How to know more details about a previous rollout revision using kubectl?
- How to keep Docker container running after starting services?
- How to know if a docker container is running in privileged mode
- How to interact with sub-server through main server
- How to intercept SLF4J with logback logging via a JUnit test?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.