CI/CD Pipelines for Kubernetes Using GitLab CI

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 for managing scalable, containerized applications. When combined with GitLab CI/CD pipelines, Kubernetes deployments become automated, repeatable, and reliable.

In this article, we’ll explore how to set up CI/CD pipelines for Kubernetes using GitLab CI. Whether you’re a developer, DevOps engineer, or IT manager, this guide will help you streamline your deployment process.


Why Kubernetes and GitLab CI?

Kubernetes simplifies container orchestration, while GitLab CI provides a robust framework for automating builds, tests, and deployments. Together, they enable teams to deliver applications faster and with greater reliability.

At ZippyOPS, we specialize in consulting, implementation, and management services for DevOps, DevSecOps, DataOps, Cloud, Automated Ops, AI Ops, ML Ops, Microservices, Infrastructure, and Security. If you’re looking to optimize your CI/CD pipelines or need expert guidance, explore our services or check out our YouTube playlist for demos and tutorials.


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 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:

apiVersion: v1
kind: ServiceAccount
meta sans-serif;">

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:

image: docker:20.10

stages:

build

test

deploy

variables:
KUBE_NAMESPACE: default
KUBECONFIG: "/root/.kube/config"
IMAGE_TAG: $CI_COMMIT_SHA
REGISTRY: registry.gitlab.com/username/project

services:

docker:dind

before_script:

apk add --no-cache curl jq bash git

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.22.0/bin/linux/amd64/kubectl

chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl

echo "KUBECONFIG"base64−d>KUBEC ONFIG"base64−d>KUBECONFIG

chmod 600 $KUBECONFIG

build:
stage: build
script:
- docker build -t REGISTRY:REGISTRY:IMAGE_TAG .
- docker login -u CIREGISTRYUSER−pCIR EGISTRYU SER−pCI_REGISTRY_PASSWORD CIREGISTRY−dockerpushCIR EGISTRY−dockerpushREGISTRY:$IMAGE_TAG

unit-test:
stage: test
script:
- echo "Running tests..."
- ./run-tests.sh

deploy:
stage: deploy
script:
- kubectl set image deployment/my-app my-app=REGISTRY:REGISTRY:IMAGE_TAG -n $KUBE_NAMESPACE
only:
- main


Secrets Management

GitLab CI supports secure secrets management using variables:

  1. Navigate to Settings > CI/CD > Variables.

  2. Add required variables like KUBE_CONFIGCI_REGISTRY_USER, and CI_REGISTRY_PASSWORD.

Encode kubeconfig before adding it as a variable:

cat ~/.kube/config | base64 | tr -d '\n'

Add the result as KUBE_CONFIG in GitLab.


Helm Deployment

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

apiVersion: v2
name: my-app
version: 1.0.0
appVersion: 1.0.0

image:
repository: registry.gitlab.com/username/project
tag: latest

service:
type: ClusterIP
port: 80*

Add Helm commands to the pipeline:

deploy:
stage: deploy
script:
- helm upgrade --install my-app ./helm-chart --set image.tag=$IMAGE_TAG


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.

kubectl logs -f deployment/my-app


Conclusion

This guide outlines how to configure CI/CD pipelines for Kubernetes using GitLab CI. From prerequisites to YAML configurations, secrets management, Helm deployments, and monitoring tools, this setup ensures efficient and reliable application deployment.

If you’re looking to optimize your CI/CD pipelines or need expert guidance, ZippyOPS offers consulting, implementation, and management services for DevOps, DevSecOps, DataOps, Cloud, and more. Explore our servicescheck out our products, or view our solutions. For a demo, visit our YouTube playlist.

If this seems interesting, please email us at [email protected] for a call. Let’s build scalable, efficient, and secure systems together!

 

Recent Comments

No comments

Leave a Comment