Building a Complete CI/CD Pipeline: Git ➔ Jenkins ➔ Docker ➔ DockerHub ➔ Kubernetes

Parth SikkaParth Sikka
4 min read

Introduction:

In this post, we’ll walk through creating a full CI/CD pipeline to automate deployments from code in Git to a live application on Kubernetes. We’ll cover each step: setting up Jenkins, configuring Docker images, pushing to DockerHub, and finally deploying to a Kubernetes cluster.


Step 1: Uploading Code to Git

  1. Set Up a Git Repository:

    • Create a new repository on GitHub (or any preferred Git platform).

    • Initialize your local project with Git, commit your code, and push it to the remote repository.

  2. Connect Your Local Project to Git:

    • Initialize Git in your local project:

        bashCopy codegit init
        git add .
        git commit -m "Initial commit"
        git remote add origin <your-repo-URL>
        git push -u origin main
      

Step 2: Launch an EC2 Instance

  1. Launch an EC2 Instance on AWS:

    • Go to the AWS Management Console, navigate to EC2, and launch a new instance.

    • Select an appropriate instance type (e.g., t2.micro for small projects).

    • Choose an Amazon Machine Image (AMI) that supports Docker and Jenkins setups, like Ubuntu 20.04.

  2. Configure Security Groups:

    • Ensure the security group allows inbound access to port 8080 (for Jenkins) and port 22 (for SSH).

Step 3: Install Java and Jenkins on EC2

  1. SSH into the Instance:

     bashCopy codessh -i "your-key.pem" ubuntu@your-ec2-ip
    
  2. Install Java:
    Jenkins requires Java, so install it with:

     bashCopy codesudo apt update
     sudo apt install openjdk-11-jdk -y
    
  3. Install Jenkins:

    • Add Jenkins repository and install:

        bashCopy codewget -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
      
  4. Start Jenkins and Get Admin Password:

     bashCopy codesudo systemctl start jenkins
     sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
    • Use the output as the initial admin password when accessing Jenkins.
  5. Access Jenkins:

    • Open your browser and go to http://<your-ec2-ip>:8080 to complete the Jenkins setup.

Step 4: Create a Jenkins Pipeline Job

  1. Install Plugins:

    • In Jenkins, go to Manage Jenkins > Manage Plugins and install the Maven Integration and Docker plugins.
  2. Create a New Job:

    • Go to New Item in Jenkins and create a new Pipeline job.

    • Name your job and select Pipeline.

  3. Add Git Repository to Jenkins Job:

    • Under Pipeline configuration, select Pipeline script from SCM, choose Git, and paste your repository URL.

Step 5: Configure Jenkinsfile for the Pipeline

  1. Create a Jenkinsfile in Git Repository:

    • In the root of your Git project, create a Jenkinsfile with stages to automate the CI/CD process:
    groovyCopy codepipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh 'mvn clean package'
                }
            }
            stage('Build Docker Image') {
                steps {
                    script {
                        docker.build("your-image-name")
                    }
                }
            }
            stage('Push to DockerHub') {
                steps {
                    withCredentials([string(credentialsId: 'dockerhub-credentials', variable: 'DOCKER_HUB_PASSWORD')]) {
                        sh 'echo $DOCKER_HUB_PASSWORD | docker login -u your-dockerhub-username --password-stdin'
                        sh 'docker push your-image-name'
                    }
                }
            }
            stage('Deploy to Kubernetes') {
                steps {
                    sh 'kubectl apply -f k8s/deployment.yaml'
                }
            }
        }
    }
  1. Define Pipeline Stages in Detail:

    • Build Stage: Uses Maven to compile your code.

    • Build Docker Image Stage: Creates a Docker image from your project.

    • Push to DockerHub Stage: Pushes the Docker image to DockerHub.

    • Deploy to Kubernetes Stage: Deploys the image on Kubernetes using kubectl.

Step 6: Configure Docker and Kubernetes

  1. Set Up DockerHub Credentials in Jenkins:

    • Go to Manage Jenkins > Manage Credentials and add DockerHub credentials with an ID of dockerhub-credentials.
  2. Push Docker Image to DockerHub:

    • Jenkins will handle this automatically during the pipeline. Ensure the DockerHub repository is set up to receive the image.
  3. Set Up Kubernetes Deployment:

    • In your repository, add Kubernetes deployment files (e.g., deployment.yaml), specifying image details and other deployment parameters.
  4. Deploy to Kubernetes:

    • Jenkins will use kubectl apply -f deployment.yaml to deploy the application to Kubernetes.

Step 7: Access Your Deployed Application

  1. Get the Application URL:

    • After deployment, run the following to get the external IP of your Kubernetes service:

        bashCopy codekubectl get svc
      
    • Access the application using the external IP.


Conclusion:
You’ve now set up a full CI/CD pipeline, transforming your code changes into a live application deployed on Kubernetes. This project illustrates the power of DevOps automation, from version control in Git to deployment on a cloud-based Kubernetes cluster.

0
Subscribe to my newsletter

Read articles from Parth Sikka directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Parth Sikka
Parth Sikka