Deploy Springboot Microservices App into Amazon EKS Cluster using Jenkins Pipeline

Containerize Springboot App and Deploy into EKS Cluster using Jenkins Pipeline

Deploy Springboot Microservices App into Amazon EKS Cluster using Jenkins Pipeline

Animation GIF

We will learn how to automate spring-boot microservices builds using the Jenkins pipeline and Deploy it into AWS EKS Cluster with the help of the Kubernetes CLI plug-in.

We will use Springboot Microservices-based Java application. I have already created a repo with source code + Dockerfile. The repo also has Jenkinsfile for automating the following:

- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into AWS ECR
- Automating Docker Containers Deployments to Kubernetes Cluster

Deployment Architecture

Make sure you fork my repo: github.com/Biswajit-Mohapatra2/My-SpringBoo..


Pre-requisites:

1. Amazon EKS Cluster is set up and running. Click here to learn how to create an Amazon EKS cluster.

2. Create an ECR repo in AWS

  1. Jenkins Master is up and running

4. Docker installed on the Jenkins instance

5. Docker, Docker pipeline and Kubernetes CLI plug-ins are installed in Jenkins

Plugin

  1. Install kubectl on your instance.

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


Step #2 - Create Credentials for connecting to Kubernetes Cluster using kubeconfig

Click on Add Credentials, and use Kubernetes configuration from the dropdown.

use a secret file from the drop-down.

Execute the below command to log in as jenkins user.

sudo su - jenkins

You should see the nodes running in the EKS cluster.

kubectl get nodes

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.

Enter ID as K8S and choose to enter directly and paste the above file content and save.


Step # 3 - Create a pipeline in Jenkins

Create a new pipeline job.


Step # 4 - Copy the pipeline code from below:

Make sure you change the yellow highlighted values below as per your settings:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied.

pipeline {

tools {

maven 'Maven3'

}

agent any

environment {

registry = "account_id.dkr.ecr.us-east-2.amazonaws.com/my-docker-.."

}

stages {

stage('Cloning Git') {

steps {

checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'github.com/Biswajit-Mohapatra2/My-SpringBoo..']]])

}

}

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-2 | docker login --username AWS --password-stdin account_id.dkr.ecr.us-east-2.amazonaws.com'

sh 'docker push account_id.dkr.ecr.us-east-2.amazonaws.com/my-docker-..

}

}

}

stage('K8S Deploy') {

steps{

script {

withKubeConfig([credentialsId: 'K8S', serverUrl: '']) {

sh ('kubectl apply -f eks-deploy-k8s.yaml')

}

}

}

}

}

}


Step # 5 - Build the pipeline

Once you create the pipeline and changes values per your configuration, click on Build Now:

Pipeline Build


Step # 6 - Verify deployments to K8S

kubectl get pods
kubectl get deployments
kubectl get services

Step # 7 - 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

loadbalancer_ip_address

You should see a page like below:

Final output

Note:

Make sure you fork my repo github.com/Biswajit-Mohapatra2/My-SpringBoo.. and make changes in eks-deploy-k8s.yaml to pull Docker image from your AWS ECR repo.

Deployment image


GIF

cheers🤟

Did you find this article valuable?

Support Biswajit Mohapatra by becoming a sponsor. Any amount is appreciated!