Automate Kubernetes deployment using CI/CD pipelines (GitLab, Jenkins)
In modern software development, Continuous Integration and Continuous Deployment (CI/CD) are fundamental practices for automating and simplifying the process of building, testing, and deploying applications. With its powerful orchestration capabilities, Kubernetes is often the platform of choice for running containerized applications in production. Integrate Kubernetes with your CI/CD pipeline (using something like GitLab and Jenkins) can greatly improve the efficiency, reliability, and speed of the deployment process.
In this article, we’ll explore how to use CI/CD pipelines to automate Kubernetes deployments GitLab and Jenkins. By the end of this guide, you’ll know how to set up a pipeline to deploy applications to a Kubernetes cluster and gain insight into best practices.
What is CI/CD?
Continuous Integration (CI) and Continuous deployment (CD) is a software engineering practice in which:
- CI Focused on automating builds, testing and integrating code changes into the shared repository frequently (multiple times per day).
- CD Automate the delivery process to ensure that code changes are deployed to the production environment after passing through the CI pipeline.
Kubernetes is an ideal platform for CI/CD because it allows developers to seamlessly deploy, scale, and manage applications. Kubernetes provides an environment in which applications can be deployed as containers, making it easier to automate the entire lifecycle of a service.
Why automate Kubernetes deployment?
Using CI/CD pipelines for automated deployment has several key benefits:
- Faster release cycle: Automated deployment reduces manual steps and speeds up the deployment process, allowing you to release features more frequently.
- consistency: Automated deployment ensures applications are deployed in a consistent, repeatable manner, reducing the chance of errors.
- Reduce downtime: Through automatic rollback, if an error occurs during deployment, Kubernetes can automatically revert to the previous stable version.
- Scalability: CI/CD pipelines can easily scale with the needs of your infrastructure, allowing for rapid release of features and bug fixes.
Prerequisites
Before setting up automated deployments with Kubernetes, you need to meet some requirements:
- Kubernetes cluster: You should have access to a Kubernetes cluster (native or a cloud provider such as AWS, GCP, or Azure).
- docker: Since Kubernetes manages containers, you will need a Dockerfile and the necessary steps to build your application into a container image.
- Continuous integration/continuous delivery tools: any one GitLab CI/CD or Jenkins Will be used to automate build and deployment pipelines.
- Kubektel: This is a command line tool for interacting with a Kubernetes cluster. It will be used to deploy and manage resources in Kubernetes.
- Docker registry: A container registry (such as Docker Hub, GitLab Container Registry, or Google Container Registry) that stores built Docker images.
Automate Kubernetes deployment using GitLab CI/CD
GitLab CI/CD is a powerful tool that integrates seamlessly with Kubernetes for continuous deployment. It allows you to define your deployment process .gitlab-ci.yml
document.
Steps to set up a CI/CD pipeline using GitLab:
-
Create a GitLab repository:
- Start by creating a GitLab repository for your application code.
-
Dockerfile:
- In your repository, make sure you have a Dockerfile to build the application image. example
Dockerfile
:
- In your repository, make sure you have a Dockerfile to build the application image. example
FROM node:14
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
-
GitLab CI/CD configuration (
.gitlab-ci.yml
):- This file defines the stages of the pipeline, including build, test, and deployment. example
.gitlab-ci.yml
:
- This file defines the stages of the pipeline, including build, test, and deployment. example
stages:
- build
- deploy
variables:
IMAGE_NAME: "your-docker-repo/your-app"
K8S_NAMESPACE: "default"
build:
stage: build
script:
- docker build -t $IMAGE_NAME .
- docker push $IMAGE_NAME
deploy:
stage: deploy
script:
- kubectl apply -f k8s/deployment.yaml
only:
- master
- In the above example:
- this
build
stage builds the Docker image and pushes it to the Docker registry. - this
deploy
Stages deploy applications to Kubernetes clusters using predefineddeployment.yaml
document.
- this
-
Kubernetes deployment YAML:Create a
deployment.yaml
files to define how applications should operate in Kubernetes. exampledeployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: your-docker-repo/your-app:latest
ports:
- containerPort: 80
-
Kubernetes configuration in GitLab CI/CD:
To enable GitLab to interact with a Kubernetes cluster, you need to set Kubernetes credentials using GitLab’s CI/CD variables. Set the following environment variables:-
K8S_CLUSTER_NAME
: Your Kubernetes cluster name. -
K8S_CLUSTER_CA
: The certificate authority for your cluster. -
K8S_CLUSTER_TOKEN
:Authentication token for the cluster. -
K8S_CLUSTER_SERVER
: Kubernetes API server endpoint.
-
-
pipeline execution:
- Push changes to the repository. GitLab will automatically trigger the CI/CD pipeline. If the code passes the build and test phases, it is deployed to the Kubernetes cluster.
Use Jenkins to automate Kubernetes deployment
Jenkins is another widely used CI/CD tool that can automate Kubernetes deployments using the following commands Jenkins pipeline.
Steps to set up CI/CD pipeline using Jenkins:
-
Install the Kubernetes plugin for Jenkins:
- Install the Kubernetes plugin in Jenkins to enable it to interact with the Kubernetes cluster.
-
Jenkins files:
- Create a
Jenkinsfile
Define the phases for building, testing, and deploying applications. exampleJenkinsfile
:
- Create a
pipeline {
agent any
environment {
IMAGE_NAME = "your-docker-repo/your-app"
K8S_NAMESPACE = "default"
}
stages {
stage('Build') {
steps {
script {
sh 'docker build -t $IMAGE_NAME .'
sh 'docker push $IMAGE_NAME'
}
}
}
stage('Deploy') {
steps {
script {
sh 'kubectl apply -f k8s/deployment.yaml'
}
}
}
}
}
- Similar to GitLab CI, Jenkins will build a Docker image, push it to the container registry, and deploy it to the Kubernetes cluster.
-
Kubernetes deployment YAML:
Just like in GitLab settings, create adeployment.yaml
Profiles define how your application operates in Kubernetes. -
Jenkins Kubernetes plug-in configuration:
Configure Jenkins to communicate with the Kubernetes cluster by providing the necessary credentials, such as API server URL, CA certificate, and token. -
pipeline execution:
onceJenkinsfile
After committing to your repository, Jenkins automatically triggers the CI/CD pipeline whenever changes are pushed to the repository.
Best practices for Kubernetes CI/CD automation
-
version control:Always use version control for Kubernetes deployment configurations (
deployment.yaml
etc.) to ensure consistency. - pipeline stage: Break down the pipeline into phases such as build, test, deploy, and rollback for better control.
- Canary or blue/green deployment: Implement advanced deployment strategies such as canary or blue/green deployments to minimize downtime and reduce risk during updates.
- Safety: Ensure sensitive information (such as Kubernetes credentials) is securely stored and accessed through secret management.
- Monitoring and alerting: Integrate monitoring tools (e.g., Prometheus, Grafana) into pipelines to track the health and performance of deployed applications.
in conclusion
Automate Kubernetes deployments using CI/CD tools, e.g. GitLab and Jenkins Simplify the development lifecycle by allowing faster and more reliable deployment of containerized applications. By following the best practices outlined in this article, you can set up an efficient CI/CD pipeline to ensure consistent and secure deployment of Kubernetes clusters.