DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Use Jenkins Effectively With ECS/EKS Cluster
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • How To Use Docker Volume To Build Angular Application With Jenkins Pipeline
  • Dockerizing an Ansible Playbook, Part 2

Trending

  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • The Cost of Knowing: When Observability Becomes the Outage
  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags

Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags

Automate build, test, and deploy with Jenkins, Docker, and Kubernetes, using feature flags for flexible, controlled releases. Ideal for rapid CI/CD workflows.

By 
Kuppusamy Vellamadam Palavesam user avatar
Kuppusamy Vellamadam Palavesam
·
Dec. 02, 24 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

A CI/CD pipeline is essential for automating the process of building, testing, and deploying applications quickly and consistently. By integrating Jenkins with Docker and Kubernetes, we can automate these processes, manage multiple deployments, and control feature releases through feature flags.

Technologies Covered

  • Jenkins: Automates build, test, and deploy stages.
  • Docker: Containerizes the application for portability and efficiency.
  • Kubernetes: Orchestrates and manages containerized deployments.
  • Feature Flags: Enable or disable specific app features without redeployment.

Setting Up Jenkins for Automated Pipelines

Step 1: Install Jenkins

Jenkins is the core of this CI/CD pipeline, managing build and deployment jobs.

1. Install Jenkins   

Shell
 
sudo apt update && sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update && sudo apt install jenkins -y


2. Access Jenkins

   Start Jenkins and visit http://localhost:8080 to configure it.

Step 2: Create a Jenkins Pipeline Job

Define the pipeline stages in a Jenkinsfile. This file outlines the steps Jenkins will follow to build, test, and deploy your app:

Plain Text
 
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building Docker image...'
                sh 'docker build -t my-app .'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'docker run my-app npm test'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying to Kubernetes...'
                sh 'kubectl apply -f k8s-deployment.yaml'
            }
        }
    }
}


  • Build Stage: Jenkins builds a Docker image.
  • Test Stage: Runs tests within the container.
  • Deploy Stage: Deploys the image to Kubernetes.

Containerizing the Application With Docker

Docker ensures your app is portable and reproducible across environments.

Step 1: Create a Dockerfile

Here’s a sample Dockerfile for a Node.js application:

Dockerfile
 
# Use Node.js image as a base
FROM node:14

# Set working directory
WORKDIR /app

# Copy files and install dependencies
COPY . .

RUN npm install

# Expose the port the app will run on
EXPOSE 3000

# Start the app
CMD ["npm", "start"]


Step 2: Build the Docker Image

Build the image locally.

Shell
 
docker build -t my-app .


This image is used in the Jenkins pipeline and can be deployed to any environment without additional setup.

Deploying to Kubernetes

Kubernetes manages application containers, scaling, and deployment for high availability and reliability.

Step 1: Create a Deployment and Service Configuration

In a k8s-deployment.yaml file, define the Kubernetes deployment and load-balanced service for your application:

YAML
 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app
          ports:
            - containerPort: 3000
--- 
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer


  • Deployment: Specifies the app’s Docker image, replicas, and labels.
  • Service: Exposes the app on a load-balanced IP, ensuring users can access it.

Step 2: Deploy to Kubernetes

Apply the configuration to your Kubernetes cluster: 

Shell
 
 kubectl apply -f k8s-deployment.yaml


Kubernetes will now manage your app, keeping it available and balanced across multiple instances.

Feature Flags for Flexible Feature Management

Feature flags let you toggle app features on or off without redeploying, offering greater control over releases.

Step 1: Add Feature Flag Logic in Your Code

Here’s a basic feature flag using environment variables in a JavaScript app: 

JavaScript
 
const featureEnabled = process.env.NEW_FEATURE === 'true';

if (featureEnabled) {

  console.log("New Feature is enabled!");

} else {

  console.log("Running default version.");

}


Step 2: Set the Feature Flag in Kubernetes

Modify k8s-deployment.yaml to include a feature flag environment variable:

YAML
 
containers:
  - name: my-app
    image: my-app
    env:
      - name: NEW_FEATURE
        value: "true" # Toggle this to "false" to disable the feature


Applying this change will toggle the feature without modifying the code, letting you test features with specific users or in specific environments.

Observability for Real-Time Insights

While basic, adding observability helps monitor app health.

Step 1: Set Up Basic Metrics With Prometheus

Use Helm to install Prometheus:

Shell
 
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

helm install prometheus prometheus-community/prometheus


Step 2: Access Prometheus and Visualize With Grafana

  1. Access Prometheus and Grafana, and add Prometheus as a data source in Grafana.
  2. Create a basic dashboard to monitor app CPU and memory usage.

Conclusion: Efficient CI/CD in a Few Steps

This streamlined pipeline enables you to:

  • Automate build, test, and deploy with Jenkins.
  • Deploy and scale seamlessly with Kubernetes.
  • Control features with feature flags for easy experimentation.
  • Gain observability to monitor app health and performance. 

This simple setup is ideal for rapid deployment workflows, giving development teams both control and flexibility. Additional observability and security steps can be added as the pipeline matures.

Kubernetes Docker (software) feature flag Jenkins (software) Pipeline (software)

Opinions expressed by DZone contributors are their own.

Related

  • How to Use Jenkins Effectively With ECS/EKS Cluster
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • How To Use Docker Volume To Build Angular Application With Jenkins Pipeline
  • Dockerizing an Ansible Playbook, Part 2

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook