This project demonstrates a complete CI/CD pipeline setup using Jenkins, Docker, and Kubernetes. The pipeline is divided into two main parts: Continuous Integration (CI) and Continuous Deployment (CD).
- GitHub: Version control and source code repository.
- Maven: Build automation tool for Java projects.
- Docker: Containerization platform.
- Docker Hub: Docker image repository.
- Kubernetes: Container orchestration platform.
- Jenkins: Automation server for CI/CD.
The CI pipeline performs the following steps:
- Cloning the Git repository.
- Building the project using Maven.
- Creating a Docker image.
- Pushing the Docker image to Docker Hub.
- Triggering the CD pipeline.
The CD pipeline performs the following steps:
- Cloning the Git repository.
- Deploying the application using Kubernetes.
-
Launch an Amazon EC2 Instance
- Instance Type:
t2.medium
- Connect to the instance using Mobaxterm/Putty
- Instance Type:
-
Install Java
sudo apt install fontconfig openjdk-17-jre
-
Install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins
-
Start Jenkins
sudo systemctl start jenkins
-
Install Maven
sudo apt install maven -y
-
Setup Docker in Jenkins
curl -fsSL get.docker.com | /bin/bash sudo usermod -aG docker jenkins sudo systemctl restart jenkins
-
Create EKS Management Host
- Launch a new EC2 instance (
t2.micro
) - Install kubectl
curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/kubectl chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin
- Install AWS CLI
- Install eksctl
- Launch a new EC2 instance (
-
Create IAM Role with the following permissions and Attach it to EKS Host
- IAM - full Access
- VPC - full Access
- EC2 - full Access
- CloudFormation - full Access
- Administrator - full Access
-
Copy EKS Cluster Config File to Jenkins EC2 Instance This is required to allow Jenkins to access our Kubernetes cluster.
- Execute the following command on the EKS management host and copy the contents:
cat .kube/config
- Execute the following commands on the Jenkins server and paste the copied contents:
cd ~ ls -la sudo vi .kube/config
- Go to Jenkins dashboard, click on "New Item", and select "Pipeline".
- Name the job
JAVA_WEB_APP_CI_JOB
. - Configure Git and Docker credentials as variables to ensure credentials are not exposed in the pipeline script.
- In the pipeline script section, use the following script:
pipeline { agent any stages { stage('Git Clone') { steps { git credentialsId: 'GIT-CREDENTIALS', url: 'https://github.com/asishpa/MAVEN-WEB-APP.git' } } stage('Maven Build') { steps { sh 'mvn clean package' } } stage('Create Image') { steps { sh "docker build -t asishdevops/maven-web-app ." } } stage('Push Image') { steps { withCredentials([string(credentialsId: 'Docker-Acc-Pwd', variable: 'dockerpwd')]) { sh "docker login -u <username> -p ${dockerpwd}" sh "docker push asishdevops/maven-web-app" } } } stage('Trigger CD') { steps { build 'JAVA_WEB_APP_CD_JOB' } } } }
- Go to Jenkins dashboard, click on "New Item", and select "Pipeline".
- Name the job
JAVA_WEB_APP_CD_JOB
. - In the pipeline script section, use the following script:
pipeline { agent any stages { stage('Git Clone') { steps { git credentialsId: 'GIT-CREDENTIALS', url: 'https://github.com/asishpa/MAVEN-WEB-APP.git' } } stage('Deploy') { steps { sh 'kubectl apply -f Deployment.yml' } } } }
- Go to the Jenkins dashboard and select
JAVA_WEB_APP_CI_JOB
. - Click on "Build Now".
- The CI job will trigger the CD job upon successful completion.
This setup provides a comprehensive CI/CD pipeline using Jenkins, Docker, and Kubernetes. The steps outlined above cover the process from setting up the Jenkins server to deploying the application on a Kubernetes cluster, ensuring a seamless integration and deployment process.