In this article, we had a quick look at how to deploy a Spring Boot  app on a local Kubernetes cluster using Minikube.

Before your begin :

You need to have a

We covre in this article :

  • Set up the application on a one-node cluster using Minikube
  • Deploy the application using config files
  • Create an External Load Balancer

Let's start :

I prepared an microservice spring boot  service see on this GitHub URL

$ git clone https://github.com/Reddah-Cherara/rcherara-api-book.git

First, mike sur after clonning the projet from github is running good.

Build and run the app using maven

$ mvn package
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ micro-service-book ---
[INFO] Building jar: /Users/rcherara/Projects-Minikube/rcherara-api-book/target/micro-service-book-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) @ micro-service-book ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 48.500 s
[INFO] Finished at: 2018-12-21T23:20:56-05:00
[INFO] ------------------------------------------------------------------------

Capture-d--cran-2018-12-21---23.20.17
Capture-d--cran-2018-12-21---23.21.12
$ java -jar target/micro-service-book-0.0.1-SNAPSHOT.jar
Capture-d--cran-2018-12-21---23.23.31
Capture-d--cran-2018-12-21---23.24.49

As you see : Tomcat started on port(s): 56810 (http)

http://localhost:56810

# Alternatively, we can run the app directly without packaging with :
$ mvn spring-boot:run

Building and running the Docker image

Now that we have defined the Dockerfile, let’s build a docker image for our application. Type the following command from the root directory of the project to build the docker image -

$ docker build -t micro-service-book .
$ docker images

Capture-d--cran-2018-12-22---18.34.01

Capture-d--cran-2018-12-22---18.31.31

# Running the docker image
$ docker run -p 56811:56810 micro-service-book

In the run command, we have specified that the port 56810 on the container should be mapped to the port 56811 on the Host OS.

You can use the -d option in docker run command to run the container in the background  :

$ docker run -d -p 56811:56810 micro-service-book
$ docker container ls

Build a Docker Image with Maven

$ docker build -t micro-service-book .
$ mvn dockerfile:push
$ mvn package dockerfile:build

Pushing the docker image to docker hub

Login with your Docker Id

$ docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (mydockerreddah): mydockerreddah
Password:
Login Succeeded

Tag the image

To push a local image to docker registry, you need to associate the local image with a repository on the docker registry. The notation for the repository on docker registry is username/repository:tag.

To tag an image, we use the docker tag command -

$ docker tag image username/repository:tag
$ docker tag micro-service-book mydockerreddah/micro-service-book:0.0.1-SNAPSHOT
$ docker image ls

Push the image to docker hub

Finally, use the docker push command to push the tagged image to docker hub like so -

$ docker push mydockerreddah/micro-service-book:0.0.1-SNAPSHOT