How to Install Jenkins and Set Up Blue-Green Deployment on Ubuntu 22.04 LTS

Setting up Jenkins for blue-green deployments can greatly improve the reliability and efficiency of your CI/CD pipeline by allowing seamless transitions between application versions. In this guide, we explored how to install Jenkins on Ubuntu 22.04 LTS and configure it for blue-green deployment using Docker.


Prerequisites

Ensure the following are in place before beginning:

  • Administrative access to the Ubuntu server.
  • Docker installed on your system.
  • Basic knowledge of CI/CD concepts and Jenkins.

Step-by-Step Guide

Step 1: Install Jenkins

First, install Jenkins on your Ubuntu 22.04 LTS system:

sudo apt update && sudo apt install jenkins -y

Start and enable the Jenkins service:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Access Jenkins through your web browser at http://your-server-ip:8080.

Step 2: Initial Configuration of Jenkins

  • Log in to Jenkins using the initial admin password, which you can find in /var/lib/jenkins/secrets/initialAdminPassword.
  • Install recommended plugins as prompted.
  • Set up your admin user and finalize the configuration.

Step 3: Install Necessary Jenkins Plugins

Navigate to Manage Jenkins > Manage Plugins and install the following plugins:

  • Pipeline
  • Docker Pipeline

These plugins are essential for integrating Docker commands within your Jenkins pipeline.

Step 4: Set Up Blue-Green Deployment with Docker

Create a docker-compose.yml file to define your blue-green deployment structure:

version: '3'
services:
  blue:
    image: <your-old-version-image>
    ports:
      - "8080:80"
    networks:
      - deployment-net

  green:
    image: <your-new-version-image>
    ports:
      - "8081:80"
    networks:
      - deployment-net

networks:
  deployment-net:
    driver: bridge

Explanation:

  • The blue service represents the current live version.
  • The green service represents the new version to be deployed.

Run docker-compose to start the containers:

docker-compose up -d

Step 5: Create a Jenkins Pipeline for Deployment

Create a Jenkinsfile in your project repository:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    sh 'docker-compose build'
                }
            }
        }
        stage('Deploy to Green') {
            steps {
                script {
                    sh 'docker-compose up -d green'
                }
            }
        }
        stage('Switch Traffic') {
            input {
                message "Ready to switch traffic to the green environment?"
            }
            steps {
                script {
                    sh 'docker-compose stop blue && docker-compose start green'
                }
            }
        }
    }
}

Explanation:

  • The Build stage builds the Docker images.
  • The Deploy to Green stage deploys the new version to the green environment.
  • The Switch Traffic stage prompts a manual input to switch traffic from blue to green.

Step 6: Test Your Blue-Green Deployment

Verify that both versions are running:

  • Visit http://your-server-ip:8080 for the blue environment.
  • Visit http://your-server-ip:8081 for the green environment.

Check that the new version works as expected before switching traffic.


Best Practices

  • Use secure credentials: Store sensitive information using Jenkins Credentials Manager.
  • RBAC: Implement role-based access control for users and jobs.
  • Backup and Monitoring: Regularly back up Jenkins data and monitor system logs for potential issues.

Troubleshooting Tips

  • Container Issues: Check Docker container logs using docker logs <container_name>.
  • Jenkins Pipeline Failures: Review the Jenkins build logs for errors related to the Jenkinsfile syntax or Docker commands.

Conclusion

By following this guide, you’ve set up Jenkins on Ubuntu 22.04 LTS and configured a blue-green deployment pipeline using Docker. This deployment strategy minimizes downtime and reduces risk during application updates, ensuring a seamless user experience. Integrate these practices into your workflow for more reliable and efficient deployments.

Next Steps:

  • Enhance your pipeline with automated tests and notifications.
  • Integrate Jenkins with additional tools like Kubernetes for more scalable deployments.
  • Explore more advanced deployment strategies like canary releases.