Deploy Spring boot application into AWS EKS using Jenkins CICD

Harshal Kathar
4 min readJul 6, 2021

--

CICD Flow

CICD Flow:

  1. Git: developers push their change to the git.
  2. Jenkins: Using the CICD server(Jenkins) we pulled the code from git, and compile the code and create a docker image.
  3. ECR: Newly created docker image we push to the AWS ECR repo.
  4. EKS: Deploy the docker image into the EKS using k8s deployment YAML.

Prerequisites:

  1. Set up Jenkins server and install docker on it.
  2. Kubernetes(EKS) cluster up and running.

Deploying a Kubernetes Cluster with Amazon EKS | by Harshal Kathar | Medium

3. Install below Jenkins plugins:

1. Amazon ECR plugin

2. Docker Pipeline

Getting started:

1: Create a repository for storing the docker images

here we create an aws ecr repo for storing the docker images.

Path: AWS ->Services -> ECR.

ECR Repo

2.Creating Jenkins prerequisites.

step1: maven3 installation on Jenkins server:

  1. We build and deploy the spring boot application using maven so we install or add maven packages using Jenkins global tool configuration.

path: Jenkins -> managed Jenkins -> global tool configuration -> maven

Step 2. Add Aws credentials to Jenkins.

Enter the Access ID and Secret Access Key for the AWS user that has access to the ECR repository.

Path: managed Jenkins -> managed credentials -> global credentials -> Add credentials

select kind= AWS Credentials

3. Set up Jenkins server to deploy the application on EKS Cluster.

Steps:
Install kubectl on a Jenkins server.
2. switched to Jenkins user: sudo -i -u jenkins
3. create a .kube folder into the Jenkins home directory
: cd ~
: mkdir .kube
4. create a config file and copy the master kube config file content.
5.Check Jenkins user able to access pod, deployment using kubectl comments.

Note: Add the ec2-user, Jenkins user to the docker group so you can execute Docker commands without using sudo.

sudo usermod -a -G docker ec2-usersudo usermod -a -G docker jenkins

Jenkins CICD Pipeline:

  1. Create a Jenkins job, of type pipeline.

name: spring-boot-cicd

type: pipeline

job

Add Jenkins file:

1. Go to pipeline(job) -> configure

2. In the pipeline session select pipeline script.

3. Write a Jenkins file

Jenkinsfile:

node {
def image
//1// stage ('checkout') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://github.com/harshalkathar/myfirstapp.git']]])
}

//2// stage ('Build') {
def mvnHome = tool name: 'maven', type: 'maven'
def mvnCMD = "${mvnHome}/bin/mvn "
sh "${mvnCMD} clean package"
}


//3// stage ('Docker Build') {

docker.build('springboot')
}
//4// stage ('Docker push')
docker.withRegistry('https://<accountno>.dkr.ecr.ap-south-1.amazonaws.com', 'ecr:ap-south-1:test-ecr') {
docker.image('springboot').push('latest')
}

//5// stage ('K8S Deploy'){
sh 'kubectl apply -f spring-boot.yaml'
}
}

Jenkinsfile overview:

Stage 1: git clone happened here, Jenkins pull the source code in this stage.

  1. I have written a simple spring boot code and I have the src, pom.xml, docker file, Kubernetes manifest file. we clone all these files here using Jenkins CI.
git

stage 2: maven compile the code and create a package.

we install maven using Jenkins global tool configuration, now we install the maven packages that we need to compile our code and as an end result we got a jar file.

note: def mvnHome = tool name: ‘maven’: the tool name must same as the name given by you at the time of maven installation using Jenkins global tool configuration.

stage 3: building Docker images with the help of Dockerfile.

Dockerfile
Note: docker.build(‘springboot’) : springboot is a ECR repo name.

stage 4: pushing the docker image to AWS ECR.

note: docker.withRegistry(‘https://<accountno>.dkr.ecr.ap-south-1.amazonaws.com, ‘ecr:ap-south-1:test-ecr’) : here test-ecr is a aws credentials id that we store into the jenkins.

stage 5: Deploy the application using the k8s deployment YAML file. (CD)

In my git repo, I also write 1 Kubernetes manifest file using this manifest file Jenkins does the deployment here.

using Jenkins CICD we successfully deploy our sample spring boot application to the Kubernetes.

We expose our application using a load balancer on port 8080.

Thank you, if you have any doubt reach out to me

LinkedIn : linkedin.com/in/harshal-kathar-b2a19b118

--

--

Harshal Kathar
Harshal Kathar

Written by Harshal Kathar

DevOps Consultant | CKA |AWS | GCP| Terraform Certified

Responses (2)