CI/CD Pipelines for Kubernetes Using GitLab CI

Modern software development demands rapid deployment cycles, scalability, and resilience. Kubernetes has emerged as the go-to orchestration platform, enabling scalable containerized application management. When combined with GitLab CI/CD pipelines, Kubernetes deployments become automated, repeatable, and reliable. 

This article explores the technical details of setting up CI/CD pipelines for Kubernetes using GitLab CI.

Prerequisites

Before configuring your CI/CD pipeline, ensure the following requirements are met:

  • GitLab Account: Access to a GitLab repository where the CI/CD pipeline will be configured.
  • Kubernetes Cluster: An existing Kubernetes cluster such as Minikube (for local testing) or managed clusters like Google Kubernetes Engine (GKE), Amazon EKS, or Azure AKS.
  • kubectl: The Kubernetes command-line tool must be installed and configured for cluster interaction.
  • Helm (optional): Kubernetes package manager for deploying and managing applications.
  • GitLab Runner: Ensure that GitLab Runner is installed and registered with your project for executing CI/CD jobs.
  • Docker: Required for building and pushing container images to a container registry.

Setting Up Kubernetes Integration With GitLab

Connect Kubernetes Cluster

Integrating Kubernetes with GitLab allows seamless deployment and resource management directly from your pipeline. Follow these steps:

  1. Go to your GitLab project dashboard.
  2. Navigate to Infrastructure > Kubernetes Clusters.
  3. Click Add Kubernetes Cluster and either connect an existing cluster or create a new one using cloud providers.
  4. Assign proper permissions to GitLab using Role-Based Access Control (RBAC).

RBAC Configuration

RBAC defines access permissions for Kubernetes resources. Below is an example YAML configuration to set up RBAC:

YAML

 

Apply Configuration

Apply the YAML file using kubectl:

YAML

 

Generate Token for Authentication

Extract the service account token needed for GitLab:

YAML

 

Paste this token into GitLab’s Kubernetes configuration settings.

Configuring GitLab CI/CD Pipeline

Define .gitlab-ci.yml

The GitLab CI configuration file defines pipeline stages, variables, and commands. Below is an example configuration:

YAML

 

Explanation of Configuration 

  • Stages: The pipeline is divided into build, test, and deploy stages for modular execution.
  • Variables: Environment variables like image tags and namespaces simplify configuration management.
  • before_script: Installs dependencies and sets up Kubernetes authentication.
  • Image Tagging: Uses commit SHA for uniquely identifying each image version.
  • Deployment: Updates Kubernetes deployment by setting the container image.

Secrets Management

GitLab CI supports secure secrets management using variables:

  1. Navigate to Settings > CI/CD > Variables.
  2. Add required variables like KUBE_CONFIG, CI_REGISTRY_USER, and CI_REGISTRY_PASSWORD.

Encode kubeconfig before adding it as a variable:

YAML

 

Add the result as KUBE_CONFIG in GitLab.

Helm Deployment

Helm simplifies Kubernetes deployments with reusable charts. Example Helm configuration:

YAML

 

Add Helm commands to the pipeline:

YAML

 

Monitoring and Debugging Tools

Monitor pipeline status in GitLab under CI/CD > Pipelines. Use tools like:

  • Prometheus and Grafana: For metrics and visualization.
  • Kubernetes Dashboard: Cluster management.
  • kubectl logs: Fetch deployment logs.
YAML

 

Conclusion

This article outlines configuring CI/CD pipelines for Kubernetes with GitLab CI. It covers prerequisites, YAML configurations, secrets management, Helm deployments, and monitoring tools. With this setup, developers can build, test, and deploy containerized applications efficiently and reliably.

Source:
https://dzone.com/articles/cicd-pipelines-for-kubernetes-using-gitlab-ci