Deploying to Kubernetes
You will need a public container registry to store your image. If you don’t have an account, we recommend you to create a free account at http://quay.io. |
Our examples will be using the quay.io
container registry and the myrepo
organization, but you should change it to match your configuration.
Adding the Kubernetes and Jib extensions
You need a container registry that is accessible from your Kubernetes cluster to deploy the application container image in it.
In this chapter we’ll be using the Quarkus Kubernetes Extension to create the Kubernetes deployment file, and the Quarkus Jib Extension to create and push the container image to your container registry without the need of a running local docker daemon.
Let’s add the required extensions:
Adding the configuration properties
Add the following properties to your application.properties
so that you can push the container to the correct location:
# Configuration file
# key = value
greeting=Hello y'all!
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
quarkus.datasource.db-kind=h2
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.sql-load-script=import.sql
quarkus.container-image.registry=quay.io(1)
quarkus.container-image.group=myrepo(2)
quarkus.container-image.name=tutorial-app(3)
quarkus.container-image.tag=1.0-SNAPSHOT(4)
quarkus.kubernetes.service-type=load-balancer
1 | Registry where image is pushed. By default is Docker Hub. |
2 | Group name of the container image. |
3 | Container name. By default is the artifactId element of pom.xml . |
4 | Tag of the container image. By default is the version element of pom.xml . |
Change quay.io to your container registry and myrepo to your organization.
If you don’t, your push will fail.
|
Authenticating and pushing the image to your container registry
In order to push the container image, you need to authenticate to your container registry:
docker login quay.io
Now create and push your container image using jib:
mvn clean package -DskipTests -Dquarkus.container-image.push=true
quarkus image push --also-build --no-tests
[INFO] [io.quarkus.container.image.jib.deployment.JibProcessor] Using base image with digest: sha256:b459cc59d6c7ddc9fd52f981fc4c187f44a401f2433a1b4110810d2dd9e98a07
[INFO] [io.quarkus.container.image.jib.deployment.JibProcessor] Container entrypoint set to [java, -Dquarkus.http.host=0.0.0.0, -Djava.util.logging.manager=org.jboss.logmanager.LogManager, -cp, /app/resources:/app/classes:/app/libs/*, io.quarkus.runner.GeneratedMain]
[INFO] [io.quarkus.container.image.jib.deployment.JibProcessor] Pushed container image quay.io/myrepo/tutorial-app:1.0-SNAPSHOT (sha256:6651a2f85f8f53ef951b3398d00f1c7da73bd0e8b21f87584d5a1c0e99aae12c)
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 14804ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.431 s
[INFO] Finished at: 2020-05-11T00:05:25-04:00
[INFO] ------------------------------------------------------------------------
Deploy your application to your Kubernetes cluster
When a Kubernetes extension is present in the classpath, a Kubernetes deployment file is scaffolded for you during the package phase.
kubectl apply -f target/kubernetes/kubernetes.yml
serviceaccount/tutorial-app created
service/tutorial-app created
deployment.apps/tutorial-app created
You might need to wait for some seconds until your application is up and running.
IP=$(minikube ip)
PORT=$(kubectl get service/tutorial-app -o jsonpath="{.spec.ports[*].nodePort}")
echo $IP:$PORT
If using a hosted Kubernetes cluster like OpenShift then use curl and the EXTERNAL-IP address with port 8080
or get it using kubectl
:
IP=$(kubectl get service tutorial-app -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
PORT=$(kubectl get service tutorial-app -o jsonpath="{.spec.ports[*].port}")
echo $IP:$PORT
If using a hosted Kubernetes cluster like OpenShift then use curl and the EXTERNAL-IP address with port 8080
or get it using kubectl
:
IP=$(kubectl get service tutorial-app -o jsonpath="{.status.loadBalancer.ingress[0].hostname}")
PORT=$(kubectl get service tutorial-app -o jsonpath="{.spec.ports[*].port}")
echo $IP:$PORT
Curl the Service:
curl $IP:$PORT/hello
Hello y'all!
You can build, push and deploy the container image by running: ./mvnw clean package -Dquarkus.kubernetes.deploy=true
|