Operations 5 min read

How to Automate Code Deployment with Kubernetes and CI/CD Pipelines

This guide walks you through setting up an automated code deployment workflow using Docker, Kubernetes Deployments, and a CI/CD tool like Jenkins, covering environment preparation, image building, deployment configuration, pipeline scripting, monitoring, and rollback strategies.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
How to Automate Code Deployment with Kubernetes and CI/CD Pipelines

In modern software development, automating code deployment improves efficiency and reduces errors. Kubernetes (K8s) serves as a powerful container orchestration platform to build such automated pipelines.

1. Preparation

Ensure you have:

A running Kubernetes cluster.

A code repository (e.g., GitHub) for your application source.

A CI/CD tool such as Jenkins for automated build and deployment.

2. Build a Docker Image

Package your application into a Docker image. Example Dockerfile for a Node.js app:

# Dockerfile example
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]

This creates an image containing the app and its dependencies.

3. Create a Kubernetes Deployment

Define a Deployment resource to manage replicas and specify the Docker image:

# deployment.yaml example
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-image:tag
        ports:
        - containerPort: 3000

This creates a Deployment named my-app with three replicas using the previously built image.

4. Set Up CI/CD Pipeline

Configure your CI/CD tool to automate the following steps:

Checkout code from the repository.

Build the Docker image.

Push the image to a container registry.

Deploy the new image to the Kubernetes cluster with kubectl apply -f deployment.yaml.

Example Jenkins Pipeline:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                // Pull code from repository
                checkout scm
            }
        }
        stage('Build and Deploy') {
            steps {
                // Build Docker image and push
                sh 'docker build -t your-image:tag .'
                sh 'docker push your-image:tag'
                // Deploy to Kubernetes
                sh 'kubectl apply -f deployment.yaml'
            }
        }
    }
}

5. Monitoring and Rollback

Kubernetes offers built‑in monitoring and automatic rollback. Integrate tools like Prometheus to observe performance and health. If a failure occurs, Kubernetes can revert to the previous stable replica set.

6. Conclusion

By leveraging Kubernetes and a CI/CD system, you can establish a highly automated deployment pipeline that speeds delivery, improves reliability, and reduces manual errors. Apply these steps to your projects to reap the benefits of automated code releases.

DockerCI/CDautomationKubernetesJenkins
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.