We will learn how to automate spring-boot microservices builds using the Jenkins pipeline and Deploy it into the AWS EKS Cluster with the help of the Kubernetes CLI plug-in.
Table of Contents:
Setting Up AWS EC2 Instance
Installing Maven
Installing and Configuring Jenkins
Installing and Setting Up AWS CLI
Installing and Configuring Kubectl
Installing and Setting Up Eksctl
Creating an IAM Role with Administrator Access
Assigning the IAM Role to the EC2 Instance
Creating an Amazon EKS Cluster
Creating an Amazon ECR Repository
Installing Docker
Configuring Jenkins Plugins
Creating a Jenkins Pipeline
Building the Pipeline
Verifying Deployments in Kubernetes
Accessing the Spring Boot App in the K8S Cluster
Clean-up and Resource Removal
Setup an AWS EC2 Instance
Log in to an AWS account using a user with admin privileges and ensure your region is set to us-east-1 N. Virginia. Move to the EC2 console. Click Launch Instance.
For the name use Springboot_Microservices_App_EKS_Server
Select AMIs as Ubuntu and select Instance Type as t3.medium. Create new Key Pair and Create a new Security Group with traffic allowed from ssh, http and https.
Install Maven
sudo hostname Jenkins
sudo apt update
sudo apt install maven -y
mvn --version
Install and Setup Jenkins
Follow the steps for installing Jenkins on the EC2 instance.
sudo apt update
sudo apt install openjdk-17-jre -y
java -version
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
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 -y
Install Java and Jenkins on Jenkins Server and configure port 8080 as we have done so many times in our previous blogs. You can follow my previous blogs: https://nahid0002.hashnode.dev/mastering-cicd-with-jenkins-building-and-deploying-applications-with-efficiency
Also you can follow jenkins officials documents: https://www.jenkins.io/doc/book/installing/linux/
Install and Setup AWS CLI
Login to your console and enter these commands
sudo apt-get update
sudo apt install awscli -y
aws --version
You will see that AWS CLI is now installed
Goto Access Keys → Create Access Keys →Download CSV File. Remember to download the CSV File so that it can be in your downloads section.
Now, go to your AWS Console login, and type below command
aws configure
You will need to enter the details
Install and setup Kubectl
Kubectl is a command-line interface (CLI) tool that is used to interact with Kubernetes clusters. It allows users to deploy, inspect, and manage Kubernetes resources such as pods, deployments, services, and more. Kubectl enables users to perform operations such as creating, updating, deleting, and scaling Kubernetes resources.
Run the following steps to install kubectl on EC2 instance.
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin
kubectl version
The output would look like this
Install and setup eksctl
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
Create an IAM Role with Administrator Access
We need to create an IAM role with the AdministratorAccess policy.
Go to the AWS console, IAM, and click on Roles. create a role. Then select AWS services, Click EC2, Click on Next permissions.
Now search for AdministratorAccess policy and click
Skip on create a tag. Now, give a role name and create it.
Assign the role to EC2 instance
Go to AWS console, click on EC2, select EC2 instance, and Choose Security.
Click on Modify IAM Role
Choose the role you have created from the dropdown. Select the role and click on Apply.
Now, the IAM role is attached to the Jenkins EC2 Instance.
Create an Amazon EKS cluster using eksctl
Switch from Ubuntu user to Jenkins user
sudo su - jenkins
Now in this step, we are going to create Amazon EKS cluster using eksctl
You need the following in order to run the eksctl command
Name of the cluster : — demo-app-eks
Version of Kubernetes : — version 1.24
Region : — region us-east-1
Nodegroup name/worker nodes : — nodegroup-name worker-nodes
Node Type : — nodegroup-type t3.small
Number of nodes: — nodes 2
eksctl create cluster --name demo-app-eks --version 1.24 --region us-east-1 --nodegroup-name worker-nodes --node-type t3.small --managed --nodes 2
It took me 20 minutes to complete this EKS cluster. If you get any error for not having sufficient data for the mentioned availability zone then try it again.
Now, when you go to your AWS Console, you will see the EKS and Worker Nodes created under Compute
Create an Amazon ECR
First, connect to your EC2 instance and install docker using these commands
sudo apt update
sudo apt install docker.io -y
docker --version
Create a Repository in AWS ECR
Create an IAM role with ContainerRegistryFullAccess
Attach an IAM role to the EC2 instance
Click on Create an Private Repository, and Enter a name for your repository
Create an IAM Role of ContainerRegistryFullAccess and attach the same to your EC2 Instance
Install Docker
Now Login to Jenkins EC2 instance, and execute below commands:
sudo apt-get update
sudo apt install docker.io -y
sudo usermod -a -G docker jenkins
sudo service jenkins restart
sudo systemctl daemon-reload
sudo service docker stop
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Install the following plugins on Jenkins
Docker
Docker Pipeline
Kubernetes CLI
Let's Create a Pipeline:
Step # 1 - Create Maven3 variable under Global tool configuration in Jenkins
Make sure you create a Maven3 variable under the Global tool configuration
Create Credentials for connecting to Kubernetes Cluster using kubeconfig
Execute the below command to log in as jenkins user.
sudo su - jenkins
Execute the below command to get kubeconfig info, and copy the entire content of the file:
cat /var/lib/jenkins/.kube/config
Open your text editor or notepad, copy and paste the entire content and save in a file. We will upload this file.
Enter ID as K8S and choose File and upload the file and save.
Create a pipeline in Jenkins
Copy the pipeline code from below:
pipeline {
tools {
maven 'Maven3'
}
agent any
environment {
registry = "517787923146.dkr.ecr.us-east-1.amazonaws.com/my-docker-app"
}
stages {
stage('Cloning Git') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://github.com/nahidkishore/Springboot_Microservices_App_EKS']]])
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
// Building Docker images
stage('Building image') {
steps {
script {
dockerImage = docker.build registry
}
}
}
// Uploading Docker images into AWS ECR
stage('Pushing to ECR') {
steps {
script {
sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 517787923146.dkr.ecr.us-east-1.amazonaws.com'
sh 'docker push 517787923146.dkr.ecr.us-east-1.amazonaws.com/my-docker-app:latest'
}
}
}
// K8S Deploy
stage('K8S Deploy') {
steps {
script {
// Set the exec-api-version explicitly here
withKubeConfig([credentialsId: 'K8S', serverUrl: '']) {
sh ('kubectl apply -f eks-deploy-k8s.yaml')
}
}
}
}
}
}
Build the pipeline
Once you create the pipeline and change values per your configuration, click on Build Now:
Verify deployments to K8S
Switch from Ubuntu user to Jenkins user
sudo su - jenkins
kubectl get pods
kubectl get deployments
kubectl get services
Access SpringBoot App in the K8S cluster
Once the build is successful, go to the browser and enter the master or worker node public IP address along with the port number mentioned above
This type of http://a2fea6c95b92e484a83ff956540b5d2c-612778417.us-east-1.elb.amazonaws.com/
Also, you can navigate loadbalancer service and copy the URL
You should see a page like below:
Note:
Make sure you fork my repo https://github.com/nahidkishore/Springboot_Microservices_App_EKS
and make changes in eks-deploy-k8s.yaml to pull the Docker image from your AWS ECR repo.
Clean UP
In this stage, you're going to clean up and remove all resources that we created during the session. So that it will not be charged to you afterwards.
Delete the EKS cluster with the following command.
eksctl delete cluster --name demo-app-eks
Troubleshooting:
If you face this type of error then use the bellow command for aws cli install:
sudo apt install awscli
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install --update
aws --version
Also, you can see these references:
https://github.com/kubernetes/kubectl/issues/747#issuecomment-548150850
As our journey comes to a close, we've unlocked the door to an exciting world of automation and containerization. You've learned how to seamlessly integrate Jenkins, Kubernetes, and AWS services to orchestrate your microservices with grace and efficiency.
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!