Deploy SpringBoot application to Kubernetes pod with Jenkins and Docker
1 min readJun 30, 2022
Hi, in this tutorial I will teach you how to deploy a springboot application to a kubernetes pod in 10 minutes guaranteed.
Repo link to be added.
First get a spring boot project.
Then install minikube, docker desktop, and jenkins.
Then open up Jenkins, create a Multibranch pipeline and link your GitHub repository. (Single branch)
Add a Jenkinsfile to your repo. (Replace sarthak-demo-api with your project name)
node{
//Checkout Code from Git
checkout scm
//Stage 1 : Build the docker image.
stage('Build image') {
sh("./mvnw spring-boot:build-image")
}
//Stage 2 : Push the image to docker registry
stage('Push image to registry') {
sh('docker tag sarthak-demo:0.0.1-SNAPSHOT sagraw47/sarthak-demo-api')
sh("docker push sagraw47/sarthak-demo-api")
}
//Stage 3 : Deploy Application
stage('Deploy Application') {
sh("kubectl apply -f deployment.yaml")
sh("kubectl rollout restart deployment sarthak-demo-api")
}
}
Add a deployment.yaml file to your repo. (Replace sarthak-demo-api with your project name)
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: sarthak-demo-api
name: sarthak-demo-api
spec:
replicas: 1
selector:
matchLabels:
app: sarthak-demo-api
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: sarthak-demo-api
spec:
containers:
- image: sagraw47/sarthak-demo-api
name: sarthak-demo-api
imagePullPolicy: Always
resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: sarthak-demo-api
name: sarthak-demo-api
spec:
ports:
- name: 3001-3001
port: 3001
protocol: TCP
targetPort: 3001
selector:
app: sarthak-demo-api
type: NodePort
status:
loadBalancer: {}
Run your jenkins job!