Deploy a Java Application on Kubernetes with Helm
Running a Java application in a production environment like Kubernetes is the best way to automate the deployment, scaling and management of the application. Helm charts make it easy to deploy and manage the application in a Kubernetes cluster.
This guide walks you through the process of deploying and running a Java Web application on a Kubernetes cluster. It uses Bitnami’s Apache Tomcat Helm chart, which is based on Bitnami’s Apache Tomcat container.
Assumptions and prerequisites
This guide makes the following assumptions:
- You have a WAR file suitable for deployment on Apache Tomcat. If you don’t, you can download an example WAR file.
- You have a Docker environment.
- You have an account with a container registry. This guide assumes that you are using Docker Hub.
- You have a Kubernetes cluster running with Helm v3.x installed.
- You have the kubectl command line (kubectl CLI) installed.
Step 1: Build and test a custom Docker image
The first step is to create a custom Docker image with your application’s WAR file. Bitnami’s Apache Tomcat image is a good starting point, since it comes with Apache Tomcat pre-configured.
- Begin by copying your application WAR file to your project working directory. In this tutorial, the WAR file is named example.war.
Create a Dockerfile that starts with Bitnami’s Apache Tomcat container and adds your application to the Tomcat webapps_default/ directory, as follows:
FROM bitnami/tomcat:latest COPY example.war /opt/bitnami/tomcat/webapps_default
Build the Docker image, replacing the USERNAME placeholder in the command below with your Docker Hub username:
$ docker build -t USERNAME/java-app .
Run the Docker image to confirm that your application is successfully deployed on Apache Tomcat:
$ docker run USERNAME/java-app
By default, Bitnami’s Apache Tomcat container will log output to the console. Inspect the console output and confirm that you see a line indicating the successful deployment of your WAR file, such as the example below. Alternatively, browse to the application URL to confirm that it works.
INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/bitnami/tomcat/data/example.war]
Step 2: Publish the Docker image
Once the Docker image is built with your application code, upload it to a public registry. This tutorial uses Docker Hub, but you can select others, such as:
To upload the image to Docker Hub, follow the steps below:
Log in to Docker Hub:
$ docker login
Push the image to your Docker Hub account. Replace the USERNAME placeholder with your Docker username:
$ docker push USERNAME/java-app
Confirm that you see the image in your Docker Hub repositories dashboard before proceeding to the next step.
Step 3: Deploy the application on Kubernetes
The next step is to deploy the application on your Kubernetes cluster. Bitnami’s Apache Tomcat Helm chart makes it very easy to do this, because it has built-in support for custom Docker images. To deploy the example application using the current Helm chart, follow these steps:
Make sure that you can to connect to your Kubernetes cluster by executing the command below:
$ kubectl cluster-info
Deploy the application by executing the following (replace the USERNAME placeholder with your Docker username):
$ helm repo add bitnami https://charts.bitnami.com/bitnami $ helm install my-tomcat-app --set image.repository=USERNAME/java-app --set image.tag=latest --set image.pullPolicy=Always bitnami/tomcat
This will create a pod within the cluster for the Tomcat service.
TIP: If you use a different repository or have multiple tagged versions of the custom container, you can also specify a different container repository or tag. See the complete list of supported parameters for the chart.
Once the chart has been installed, you will see some information about the deployment. Run the kubectl get pods command to see pod status and get a list of running pods:
$ kubectl get pods
To obtain the application URL, run the commands shown in the “Notes” section and browse to the WAR application at the resulting service IP address, such as http://IP-ADDRESS/example. If you used the example WAR file, here are some screenshots of what you should see:
You’ve now successfully deployed your Java/Tomcat application on Kubernetes.
To learn more about the topics discussed in this guide, use the links below: