Deploy Jakarta EE application to Kubernetes

Deploy Jakarta EE application to Kubernetes


In this tutorial, I will show you how to deploy Jakarta EE application to Kubernetes.
Kubernetes doesn't run containers directly, instead it uses something called pod, which is a group of containers deployed together on the same host.
To follow this tutorial, you will need DockerKubernetesMaven and of course Java installed on your machine.
I will use my custom maven archetype to generate the Jakarta EE application.

Generate the application.


$> mvn archetype:generate -DarchetypeGroupId=com.kodnito \
-DarchetypeArtifactId=kodnito-jakartaee-archetype \
-DarchetypeVersion=1.0.5 -DgroupId=com.kodnito \
-DartifactId=hello-jakartaee -Dversion=1.0-SNAPSHOT


This command will generate a Jakarta EE application called hello-jakartaee.
Open the project in your IDE or editor of your choice.
The first file we will look at is the Dockerfile, you find the file in the root of the project.
This will create a Docker image based on the Payara Micro and will add the war file into a directory inside the Docker image.


FROM payara/micro
COPY target/hello-jakartaee.war $DEPLOY_DIR


Next file is the deployment.yml file, which is the Kubernetes deployment definition.


kind: Service
apiVersion: v1
metadata:
  name: hello-jakartaee
  labels:
    app: hello-jakartaee
spec:
  type: NodePort
  selector:
    app: hello-jakartaee
  ports:
  - port: 8080
    targetPort: 8080
    name: http
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: hello-jakartaee
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-jakartaee
  template:
    metadata:
      labels:
        app: hello-jakartaee
        version: v1
    spec:
      containers:
        - name: hello-jakartaee
          image: hello-jakartaee
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 45
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 45
      restartPolicy: Always


Service is a REST object and here we create a service called hello-jakartaee and targets port 8080 on any pod labeled app:hello-jakarta-ee.
type: NodePort means that any traffic sent to this port is forwarded to the service.
replicas: 2 specifies several pods replicas are running and here we specified two.
image: hello-jakartaee specifies the name of the image we are trying to pull from the Docker images.
Make sure to update this to look like this, because we are deploying locally.


image: localhost:5000/hello-jakartaee


imagePullPolicy: Always will force pull the image.
readinessProbe will check if the container is ready to use.
livenessProbe will check if the container has started an is alive.

When we are deploying locally, we will create our own Docker registry where we will deploy our images, before that you should run the following command to build the war file.


$> mvn clean package


We create our Docker registry with the following command.


$> docker run -d -p 5000:5000 --restart=always --name registry registry:2

The following command will build the image from the Dockerfile.


$> docker build -t hello-jakartaee .

Next command is used to tag the image so it points to our registry.


$> docker tag hello-jakartaee localhost:5000/hello-jakartaee

Now we can push the image.


$> docker push localhost:5000/hello-jakartaee

Now that we have Docker registry setup, we can move on to Kubernetes.
With this command, we will create the deployment.


$> kubectl apply -f deployment.yml

Now run the following command to see if the deployment was created.


$> kubectl get deployments

NAME              READY   UP-TO-DATE   AVAILABLE   AGE
hello-jakartaee   2/2     2            2           3m11s

The following command will list all the services.


$> kubectl get services

NAME              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
hello-jakartaee   NodePort    10.105.7.16           8080:30811/TCP   4m3s
kubernetes        ClusterIP   10.96.0.1             443/TCP          2d1h

When you run this command, you will see which port the app is running on Kubernetes.
Now is our Jakarta EE application deployed to Kubernetes.
Open your browser and point it to http://localhost:30811/hello-jakartaee/api/hello

 You can find the source code on GitHub.


Share this: