DEV Community

Pratik Singh
Pratik Singh

Posted on

Deploy your first Java Application on K8s

This article will help you deploy a Java Application on Kubernetes.

Prerequisites :

  • A compiling Java application.
  • Basic of Docker
  • Basics of Kuebernetes

Being in 2023, it's hard to recall a time without Java. But now it is harder to imagine a time without Kubernetes. Thus, today we will be looking into how to utilize the most trusted runtime code, with the most optimized deployment solution.

Overview

We are planning to utilize Kubernetes to deploy a Java application. To do this, we will containerize your application using Docker and create an image that can be pushed to DockerHub or any other image repository. Following that, we will create a Kubernetes cluster and deploy this Docker image to it.


Let's get started!

Steps to follow:

1. Build your Application:

First of all, ensure the Java Application you built compiles and builds on your system.

2. Creating a DockerFile

This would be the most crucial part of the process. As a Developer, you create the DockerFile. This file is like the recipe for anyone in the world who wants to run/cook your code.

Make a file in your project directory called DockerFile. Please make sure this file has no extension. It is only called Dockerfile

This can be referred to as a base DockerFile to build on:

FROM <choose your OS>

# Copy local code to the container image.
WORKDIR /app
COPY . .

# Build a release artifact.
RUN mvn package -DskipTests

# Run the web service on container startup.
CMD ["java", "-jar", "/app/target/my-app-1.0-SNAPSHOT.jar"]
Enter fullscreen mode Exit fullscreen mode

You can refer to the official docs: Here

Docker has simplified the process further by introducing the command of docker init. This automates the process of creating a DockerFile while following best practices.
You can refer to the official docs Here

3. Building the Docker Image:

Now you create a Docker Image out of the DockerFile you just created. Suppose the DockerFile was the recipe of your dish and this is a sample made following it.

Run this command in the same directory:

docker build -t my-java-app . 
Enter fullscreen mode Exit fullscreen mode

Replace my-java-app with a name you wish

Your Docker Image is built and saved on your local. Verify by running this command:

 docker images
Enter fullscreen mode Exit fullscreen mode

You should see a my-java-app name in the output
If you see some errors at this step. Ensure the base image is present on your system. In most cases, it's the DockerFile that has the error.

4. Pushing the Image

Just like your code is saved on Code Repositories like GitHub or GitLab. The Docker Images also have something similar called a Image Repository. Connect to the Image Repository of your choice.
For personal usage, most devs prefer the DockerHub. The official docs to connect to DockerHub: Here

Create a Tag for the Image you created in the last step using this command:

docker tag my-java-app:latest yourusername/my-java-app:latest
Enter fullscreen mode Exit fullscreen mode

Push this Image to the image repository:

docker push yourusername/my-java-app:latest
Enter fullscreen mode Exit fullscreen mode

Now anyone in the world can run your application. Now we deploy this Docker Image on K8s.

5. Creating a Kubernetes Cluster

Create a Cluster with Google's GKE, Azure's AKS, AWS's EKS or something on local system with help of kind or minikube.

Create a Kubernetes cluster and establish a connection to run kubectl commands. See the docs of your respective Kubernetes provider. In case of kind or minikube you don't need that.

6. Creating a Deployment yaml

You write this YAML file to tell Kubernetes how to run your application. You provide the location of the Docker Image and specify other deployment parameters in this file.

There are always a lot of things you can add to a Deployment YAML in Kubernetes. Make a file .yaml and write these parameters in it.
But this is the most simplified YAML for a simple deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-java-app-deployment
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-java-app
        image: yourusername/my-java-app:latest
        ports:
        - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode

Once you have made the required changes, save the file and run this command:

kubectl apply -f <your-file-name>.yaml
Enter fullscreen mode Exit fullscreen mode

Congratulations!

You just deployed your first Java application on a Kubernetes Cluster.

There are several best practices in building the Dockerfile and the YAML that we missed in this article. These are needed for building production-grade systems.

Hope this helps :)

Top comments (0)