DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

Creating Ci/CD pipeline using following tools

Creating Ci/CD pipeline using following tools

1) Maven
2) Git Hub
3) Jenkins
4) Docker
5) Kubernetes

Step - 1 : Create EKS Management Host in AWS

1) Launch new Ubuntu VM using AWS Ec2 ( t2.micro )

2) Connect to machine and install kubectl using below commands

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
kubectl version --short --client
Enter fullscreen mode Exit fullscreen mode

3) Install AWS CLI latest version using below commands

sudo apt install unzip 
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
Enter fullscreen mode Exit fullscreen mode

4) Install eksctl using below commands

curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
eksctl version
Enter fullscreen mode Exit fullscreen mode

Step - 2 : Create IAM role & attach to EKS Management Host & Jenkins Server

1) Create New Role using IAM service ( Select Usecase - ec2 )

2) Add below permissions for the role

- IAM - fullaccess

- VPC - fullaccess

- EC2 - fullaccess

- CloudFomration - fullaccess

- Administrator - acces

3) Enter Role Name (eksrole)
4) Attach created role to EKS Management Host (Select EC2 => Click on Security => Modify IAM Role => attach IAM role we have created)
5) Attach created role to Jenkins Machine (Select EC2 => Click on Security => Modify IAM Role => attach IAM role we have created)

Step - 3 : Create EKS Cluster using eksctl #

Syntax:

eksctl create cluster --name cluster-name \
--region region-name \
--node-type instance-type \
--nodes-min 2 \
--nodes-max 2 \
--zones ,

eksctl create cluster --name psait-cluster --region ap-south-1 --node-type t2.medium  --zones ap-south-1a,ap-south-1b
Enter fullscreen mode Exit fullscreen mode
kubectl get nodes  
Enter fullscreen mode Exit fullscreen mode

Step-4 : Jenkins Server Setup in Linux VM

1) Create Ubuntu VM using AWS EC2 (t2.medium)

2) Enable 8080 Port Number in Security Group Inbound Rules
3) Connect to VM using MobaXterm
4) Install Java

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
Enter fullscreen mode Exit fullscreen mode

5) 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
Enter fullscreen mode Exit fullscreen mode

6) Start Jenkins

sudo systemctl enable jenkins
sudo systemctl start jenkins
Enter fullscreen mode Exit fullscreen mode

7) Verify Jenkins

sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

8) Open jenkins server in browser using VM public ip

http://public-ip:8080/
Enter fullscreen mode Exit fullscreen mode

9) Copy jenkins admin pwd

sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode

10) Create Admin Account & Install Required Plugins in Jenkins

Step-5 : Configure Maven as Global Tool in Jenkins

1) Manage Jenkins -> Tools -> Maven Installation -> Add maven

Step-6 : Setup Docker in Jenkins

curl -fsSL get.docker.com | /bin/bash
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
sudo docker version
Enter fullscreen mode Exit fullscreen mode

Step - 8 : Install AWS CLI in JENKINS Server

URL : https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Execute below commands to install AWS CLI

sudo apt install unzip 
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
Enter fullscreen mode Exit fullscreen mode

Step - 9 : Install Kubectl in JENKINS Server

Execute below commands in Jenkins server to 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
kubectl version --short --client
Enter fullscreen mode Exit fullscreen mode

Step - 10 : Update EKS Cluster Config File in Jenkins Server

1) Execute below command in Eks Management host & copy kube config file data

$ cat .kube/config

2) Execute below commands in Jenkins Server and paste kube config file

$ cd /var/lib/jenkins

$ sudo mkdir .kube

$ sudo vi .kube/config

3) Execute below commands in Jenkins Server and paste kube config file for ubuntu user to check EKS Cluster info

$ cd ~

$ ls -la

$ sudo vi .kube/config

4) check eks nodes

$ kubectl get nodes

Note: We should be able to see EKS cluster nodes here.

Step - 11 : Create Jenkins CI CD Job

  • Stage-1 : Clone Git Repo
  • Stage-2 : Maven Build
  • Stage-3 : Create Docker Image
  • Stage-4 : Push Docker Image to Registry
  • Stage-5 : Deploy app in k8s eks cluster
pipeline {
    agent any

    tools{
        maven "Maven-3.9.9"
    }

    stages {
        stage('Clone Repo') {
            steps {
                git 'https://github.com/pankajmutha14/docker-test.git'
            }
        }
        stage('Maven Build') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Docker Image') {
            steps {
                sh 'docker build -t psait/pankajsiracademy:latest .'
            }
        }
        stage('k8s deployment') {
            steps {
                sh 'kubectl apply -f k8s-deploy.yml'
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Step - 12 : Access Application in Browser

Top comments (0)