Tutorial: Publish a Quarkus application in Kubernetes, Minikube and Dockerhub
Notes
This is my first English post, then please give me your feedback, and suggestion to correct any mistakes.
Introduction
The article purpose to create a simple application in Quakus and publish in Minikube (Kubernetes for the local test) and finally publish the image Docker to in Dockerhub.
For more information on these technologies: Kubernetes (K8S), Docker, Minikube, Kubectl e do Quarkus, please visit these sites:
Docker: https://www.docker.com/
Kubernetes: https://kubernetes.io/pt/
Minikube: https://minikube.sigs.k8s.io/docs/start/
Minikube (Kubernetes): https://kubernetes.io/pt/docs/tutorials/kubernetes-basics/create-cluster/cluster-intro/
Quarkus: https://quarkus.io/
Minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes. It is not recommending to production environment.
Prerequisites
- Install Docker: https://docs.docker.com/engine/install/
- Install Kubernetes: https://kubernetes.io/pt/docs/setup/
- Install Kubeclt: https://kubernetes.io/docs/tasks/tools/
- Install Minikube: https://minikube.sigs.k8s.io/docs/start/
- Install Open JDK 11+: https://openjdk.java.net/install/
Let’s go! We will create a new simple Quarkus application.
Create a simple Quarkus application by command line
mvn io.quarkus:quarkus-maven-plugin:1.13.0.Final:create \\\\
-DprojectGroupId=br.com.mp \\\\
-DprojectArtifactId=kubernetes-quarkus \\\\
-DclassName="br.com.mp.kubernetes.quarkus.rest.GreetingResource" \\\\
-Dpath="/hello" \\\\
-Dextensions="resteasy,kubernetes,jib"
Access the project folder
cd kubernetes-quarkus
In the next step, let’s go to test the project
mvn quarkus:dev
Result in terminal (console)
mvn quarkus:add-extension -Dextensions="quarkus-minikube"
Or you can insert in pom.xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-minikube</artifactId>
</dependency>
It is possible to create Quarkus application by command line, adding Minikube extensions.
Creating a local Container Registry in Minikube
It is necessary to set the environment variable with “eval” command.
eval $(minikube -p minikube docker-env)
Create Docker image with Quarkus by command line
Next step, create a maven package with Quarkus parameter to build a container image.
mvn package -Dquarkus.container-image.build=true
Kubernetes and Minikube manifest create
After execute the last command, maven and Quarkus will create the manifest files, located in the folder “/target/kubernetes”, as you see in the image below.
List of all docker images
//List all docker images
docker image ls[output]
REPOSITORY TAG IMAGE ID CREATED SIZE
marcus/kubernetes-quarkus-teste 1.0.0-SNAPSHOT e574987218c4 3 minutes ago 199MB
In the next step, we need to apply Minikube manifest to publish in Minikube Kubernetes
kubectl apply -f target/kubernetes/minikube.yml[output]
service/kubernetes-quarkus-teste created
deployment.apps/kubernetes-quarkus-teste created
After a few seconds or minutes, the application is available on Minikube,
Pods are the smallest, most basic deployable objects in Kubernetes (https://cloud.google.com/kubernetes-engine/docs/concepts/pod)
List of all pods
kubectl get po[output]
NAME READY STATUS RESTARTS AGE
kubernetes-quarkus-teste-54db4f8df8-gf7nf 1/1 Running 0 6m52s
Access the application at the local machine
To access on the local machine, it is required to redirect Minikube address to localhost, type this command. (Each Pod there is a unique name)
kubectl port-forward pod/kubernetes-quarkus-teste-54db4f8df8-gf7nf 8080:8080[output]
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
Handling connection for 8080
Access application in browser
Open your browser and insert this address:http://localhost:8080
Publish the image to Dockerhub
From now, we are sending our docker image to Dockerhub, with maven command and Quarkus parameter (container image).
mvn clean package -Dquarkus.container-image.push=true
In my case, it happened this error
Caused by: com.google.cloud.tools.jib.api.RegistryUnauthorizedException:Unauthorized for registry-1.docker.io/marcus/kubernetes-quarkus-teste
[ERROR] at com.google.cloud.tools.jib.registry.RegistryEndpointCaller.call(RegistryEndpointCaller.java:164)
[ERROR]
For fix this problem, please follow these steps:
1 — Type this command to login on Dockerhub services
docker login
2 — The local Docker image was created with username machine, in my case is marcus/kubernetes-quarkus, but the correct Dockerhub username is marcuspaulo.
Inside the Quarkus project, search application.properties file and insert the line above (with your Dockerhub username)
quarkus.container-image.group=marcuspaulo
Publish in the Dockerhub
mvn clean package -Dquarkus.container-image.push=true
Now, open your browser and access this address: https://hub.docker.com/repositories
I hope that you enjoy this tutorial. Thanks for your attention. Please like and share this post. See you soon.
Source code
https://github.com/marcuspaulo/kubernetes-quarkus
References
https://quarkus.io/guides/container-image#quarkus-container-image_quarkus.container-image.registry
https://quarkus.io/guides/deploying-to-kubernetes
https://quarkus.io/guides/kubernetes-client
https://minikube.sigs.k8s.io/docs/handbook/registry/
https://cloud.google.com/kubernetes-engine/docs/concepts/pod