How to Deploy Java Application in Kubernetes?

Kubernetes is a platform for automating deployment, scaling, and management of containerized applications. Pods are basic units that hold one or more containers. Deployments manage application deployment and updates. Services provide stable endpoints for accessing pods. Ingress manages external access to services. Together, they streamline application deployment and ensure efficient communication within the cluster.

Step-by-step to deploy Java Application in Kubernetes

Step 1: Create the repository and push the code into the repository.

Step 2: Here is the GitHub repository for the following application.

Step 3: Used here maven as a build and build was successfully refer the below image for your reference.

Step 4: Here is the docker image was successfully build and here are the image.

Step 5: Here it is the Dockerfile to build the docker image.

FROM tomcat:8.0.20-jre8
COPY target/maven-web-app*.war /usr/local/tomcat/webapps/maven-web-application.war
  1. FROM tomcat:8.0.20-jre8: This line is a Dockerfile instruction. It specifies the base image that will be used to build the new image. In this case, it uses the Tomcat version 8.0.20 with Java Runtime Environment 8 (JRE8) as the base image. Docker will look for this base image locally, and if it’s not found, it will download it from Docker Hub.
  2. COPY target/maven-web-app*.war /usr/local/tomcat/webapps/maven-web-application.war: This line is another Dockerfile instruction. It copies the Maven web application’s WAR (Web Application Archive) file from the local filesystem into the Tomcat webapps directory within the container. Here’s the breakdown of each part:
    • COPY: This Dockerfile instruction is used to copy files or directories from the build context (local filesystem) into the container.
    • target/maven-web-app*.war: This specifies the path to the WAR file in the local filesystem. It uses wildcard (*) to match any filename starting with “maven-web-app” and ending with “.war” in the “target” directory.
    • /usr/local/tomcat/webapps/maven-web-application.war: This specifies the destination path inside the container where the WAR file will be copied. It renames the WAR file to “maven-web-application.war” within the container’s webapps directory.

Step 6: Here is the deployment file to deploy the java application into the kuberenetes.

Here it the complete code of the deployment file for your reference. And exposed the service outside of the node using the node exporter.

apiVersion: apps/v1
kind: Deployment
metadata:
name: maven-deployment
spec:
replicas: 1
selector:
matchLabels:
app: maven
template:
metadata:
labels:
app: maven
spec:
containers:
- name: maven-container
image: mavenimage
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: maven-service
spec:
selector:
app: maven
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: NodePort

Here is the detail explanation about the deployment file each and every step.

  • apiVersion: Indicates the Kubernetes API version that the object uses. In this case, it’s apps/v1.
  • kind: Specifies the kind of object, which is a Deployment.
  • metadata: Contains metadata about the Deployment, including its name.
  • spec: Describes the desired state for the Deployment.
  • replicas: Specifies the desired number of replicas for the pods managed by this Deployment. In this case, it’s set to 1.
  • selector: Defines how the Deployment selects which pods to manage.
  • matchLabels: Specifies that pods managed by this Deployment should have labels matching the provided labels. Here, it selects pods with the label app: maven.
  • template: Describes the pod template used by the Deployment to create new pods.
  • metadata: Contains labels to apply to pods created from this template.
  • spec: Specifies the specification for the containers within the pod.
  • containers: Describes the containers to run in the pod.
  • name: Name of the container.
  • image: Specifies the Docker image to use for the container.
  • ports: Specifies the ports that the container exposes.
  • containerPort: Specifies the port number that the container listens on. Here, it’s set to 8080.

Here is the Service file for detail explanation.

  • apiVersion: Indicates the Kubernetes API version that the object uses. In this case, it’s v1.
  • kind: Specifies the kind of object, which is a Service.
  • metadata: Contains metadata about the Service, including its name.
  • spec: Describes the desired state for the Service.
  • selector: Specifies how the Service selects which pods to target.
  • app: maven: Selects pods with the label app: maven, which matches the label used in the Deployment.
  • ports: Specifies the ports that the Service exposes.
  • protocol: Specifies the protocol used for the port (TCP in this case).
  • port: Specifies the port number exposed by the Service.
  • targetPort: Specifies the port on the pods to which traffic should be forwarded. Here, it’s set to 8080, matching the container port in the Deployment.
  • type: Specifies the type of Service. In this case, it’s NodePort, which exposes the Service on a port on each node in the cluster.

Step 7: Here is the deployments after applying the deployment file.

Step 8: Here is the service exposed over the node using the node exporter.

Step 9: Here is the application exposed over the internet using the node port refer the below image for your reference.

Conclusion

A repository must be created, the code must be pushed, the application must be built using Maven, a Dockerfile must be created, a Docker image must be created, and Kubernetes deployment and service files must be defined before a Java application can be deployed in Kubernetes. The number of replicas, container specs, ports, and selectors, as well as the desired deployment state, are all specified in these files. Developers may ensure effective communication and faster deployment procedures within the cluster by automating the deployment, scaling, and management of containerized applications by utilizing Kubernetes.

Deploy Java Application in Kubernetes – FAQs

How do I deploy a Java application to Kubernetes?

Using a Kubernetes deployment YAML file that specifies the Docker image and required configuration for your Java application, you may deploy it to Kubernetes. Then, you can apply it with the kubectl apply -f <filename.yaml> command.

How do I deploy a Java application?

A Java program can be deployed to a Java application server such as Apache Tomcat or WildFly by first compiling it into a JAR or WAR file.

How do I deploy an application on Kubernetes?

An application can be deployed on Kubernetes by making a deployment manifest file that contains the configuration details, then deploying it with the kubectl apply -f <manifest-file.yaml> command.

How to deploy a Java application on a Docker container?

A Dockerfile containing the base Java image must be created before copying the application’s JAR or WAR file into the container, building, and running the container. This is how to deploy a Java application on a Docker container.

Where can I deploy my Java application?

Your Java program can be deployed on a number of platforms, such as virtual private servers (VPS) or dedicated servers provided by hosting companies, as well as cloud services like AWS, Azure, or Google Cloud. Furthermore, systems for containerization such as Docker and Kubernetes offer flexible deployment choices.