In this blog, we will try to learn springboot with docker and deploying it to local k8s cluster hosted with minikube.
Setup
Make sure you have minikube installed
Make sure you have kubectl installed
Make sure you have docker installed
Dockerhub account to push the images
Refer my blog for installing via chocolatey for windows
Steps
1.Create springboot project with following dependencies
from start.spring.io
2.Sample Docker file reference to create the image
FROM openjdk:13
EXPOSE 9000
ARG JAR_FILE= ./target/spring-dockr-k8s-0.0.1-SNAPSHOT.jar
ADD ./target/spring-dockr-k8s-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- Pom file changes to build and push the image to dockerhub
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version> <executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<username>username</username>
<password>******</password>
<repository>sanjaybsm/spring-dockr-k8s</repository>
<tag>latest</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Build, Install and Push
If your pom changes are correct then,
mvn clean install' should already build an image for you, and you should see below logs.
`
[INFO] Successfully built sanjaybsm/spring-dockr-k8s:latest
Push
Below snap from inteliij, but you can manually also run the commands from command line to push the image,
[INFO] Image 54ab11ed5591: Preparing
[INFO] Image b8cb43830a76: Preparing
[INFO] Image 93c304238ab8: Preparing
[INFO] Image 12a9cd7d069e: Preparing
[INFO] Image 93c304238ab8: Layer already exists
[INFO] Image b8cb43830a76: Layer already exists
[INFO] Image 54ab11ed5591: Pushing
[INFO] Image 12a9cd7d069e: Layer already exists
[INFO] Image 54ab11ed5591: Pushed
[INFO] latest: digest: sha256:f2ddde51cbd4005ec90c25698649e1cf960cd1fbeb308f1de4b05b9a53606c48 size: 1166
Start minikube and run the image locally
Start minikube minikube start
Start the minikube tunnel so that you can access the app outside k8s cluster minikube tunnel
keep the tunnel open, and this change is needed for windows.
Create deployment
kubectl create deployment testspringk8s --image={dockerhub_username}/{image_name}:{version}
e.g.kubectl create deployment testspringk8s --image=sanjaybsm/spring-dockr-k8s
Expose Deployment
kubectl expose deployment testspringk8s --type=LoadBalancer --port=9000
Run this command to know the externalIp assigned
kubectl get svc
It should show something like below
if it's pending it means that your tunnel is not running or some issue with your minikube
Happy learning!
Top comments (0)