Springboot+Docker+Minikube+Kubectl - Continuous Integration(CI) Local
Tags: java, springboot, minikube, k8s
Published: 2021-10-08 13:15:25

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](https://dev.to/sanjaybsm/installing-minikube-kubectl-with-chocolatey-windows-5gc7) 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"]
```
3. 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,
![Intellij](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jexohq3bru0w1sgvtky.png)

`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,

![Intellij](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgovr9gr2ub7ih7aa31d.png)
 
```
[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
![services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht1f5qfuyh8mqbz0vcxt.png)


_if it's pending it means that your tunnel is not running or some issue with your minikube_

Happy learning!

#reference
https://minikube.sigs.k8s.io/docs/handbook/accessing/