Deploy Spring Boot Application Using Jenkins

Rasalingam Ragul
6 min readApr 3, 2023

--

In this article I’ll give step by step explanation for deploy Spring Boot Application on Kubernetes using Jenkins. If you new to Jenkins better go through with my previous article Jenkins From Beginning. Before start we need to create simple Spring Boot Application I’ll provide my project link end of the article you can use it for demo purpose.

Prerequisites

  1. Java
  2. Docker
  3. Kubernetes
  4. Minikube

Before start this process you should have the above software and services on your system. For Java you need at least version 11 or above, you should have Docker CLI and Docker desktop and also you should have Kubernetes and finally you need minikube for creating a local Kubernetes environment. I’ll provide the links for Download the perquisites below:

Plugins

In this process you need install some plugins in Jenkins

  1. Docker plugin
  2. Kubernetes Continuous Deploy Plugin

you can’t find the Kubernetes Continuous Deploy Plugin in Jenkins Plugin Manager you can download from this link after download it you need to follow below steps

  1. First go to Manage Jenkins from Dashboard then go to Plugin Manager then click Advanced settings in that screen.

2. Scroll down to Deploy Plugin and select the downloaded file using Browse option and click Deploy button.

Add Credentials

During this process you need to push your docker image to your Docker Hub and deploy the application on Kubernetes for this you have to add your credentials in Manage Credentials section in Jenkins

  1. Add Credentials for Docker Hub login

give any name to ID and Description but you have to remember it because it is necessary for later part and put your Docker Hub password in the secret section and click save button.

2. Add Credentials for Kubernetes

same as earlier give any name to ID and Description then select Enter directly in the Kubeconfig section in hear you need past some thing that you get from config file that you can find it in your Home directory .kube folder and click save button.

Create Spring Boot project

Create your spring boot project using Spring Initializr and do the below changes in your code

@SpringBootApplication
@RestController
public class SpringJenkinsApplication {

@GetMapping("/")
public String displayMessage(){
return "Jenkins demo whith Springboot";
}
public static void main(String[] args) {
SpringApplication.run(SpringJenkinsApplication.class, args);
}

}

you have to change default Spring Boot Application running port to 8090 for this you need to add one line of code in the application.properties that is in ~/src/main/resources/application.properties

server.port=8090

Create Dockerfile

Dockerfile is a simple text file that consists of instructions to build Docker images. In this step you need to create new file called Dockerfile and put the below code in the file

FROM openjdk:11
ADD target/spring-jenkins-0.0.1-SNAPSHOT.jar spring-jenkins-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/spring-jenkins-0.0.1-SNAPSHOT.jar"]

Create deploymentservice.yaml

For deploy our application on Kubernetes we have to give instructions about deployment using deploymentservice.yaml file.

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: //add your dockerhub repository image with tag
resources:
limits:
memory: "512Mi"
cpu: "500m"
ports:
- containerPort: 8090

---
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
type: NodePort
selector:
app: myapp
ports:
- port: 80
targetPort: 8090

in the image section you need to add your docker hub repository name with tag for that first you need create new public repository named devops-integration or your wish.

if you need more details visit this link.

Create Jenkinsfile

Now you need to create new file and named it as Jenkinsfile because this file is responsible for the flow of the pipeline.

pipeline {
agent any
tools{
maven 'M3'
}
stages{
stage('Build Maven'){
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/rragul/spring-jenkins']]])
sh 'mvn clean install'
}
}
stage('Build Image'){
steps{
script{
sh 'docker build -t ragul05/devops-integration:v1.$BUILD_ID .'
sh 'docker build -t ragul05/devops-integration:latest .'
}
}
}
stage('Push image to Hub'){
steps{
script{
withCredentials([string(credentialsId: 'dockerhub-pwd', variable: 'dockerhubpwd')]) {
sh 'docker login -u ragul05 -p ${dockerhubpwd}'

}
sh 'docker push ragul05/devops-integration:v1.$BUILD_ID'
sh 'docker push ragul05/devops-integration:latest'
}
}
}
stage('Deploy to k8s'){
steps{
script{
kubernetesDeploy (configs: 'deploymentservice.yaml',kubeconfigId: 'k8sconfigpwd')
}
}
}
}
}

you have to change the url in the checkout section and add your GitHub repository link and in the Push image to Hub you should add your Docker Hub user name after -u and also replace ragul05 with your Docker Hub user name in all place.

Now all set to start so now you need to add your Spring Boot project to GitHub public repository(for avoid to add extra credential).

Create New Job

Create new Pipeline job and give name to it

after that click Configure button then scroll down to Pipeline section and change Definition as Pipeline Script From SCM then put your git repository link in the Repository URL section and put your branch name in the Branch Specifier section then leave others as default and click save.

Start minikube

Now you need to start minikube on your machine in my case i am using docker as driver so i run below commend in my terminal

minikube start --driver=docker

you can find different ways using this link.

Build Your Job

Now you need click Build Now button and need to wait few seconds for start build after few minutes you will get

now you click the marked icon and view console out put then scroll down to end you can find nodePort and get the port number then find your ip address using ifconfig command.

View Your Application

Now open your browser and in the address bar paste your ip address and at the end add ‘:’ this and paste your nordPort like below:

http://192.168.8.153:31384/

Now you got your running application

--

--