Deploy your first Java Application on K8s
Published: 2024-04-07 05:19:44

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"]
```

You can refer to the official docs: [Here](https://docs.docker.com/language/java/build-images/#create-a-dockerfile-for-java)

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](https://docs.docker.com/engine/reference/commandline/init/)

## 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 . 
```

> 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
```
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](https://docs.docker.com/docker-hub/quickstart/#:~:text=Run%20docker%20run%20%3Cyour_username%3E%2Fmy-private-repo%20to%20test%20your%20Docker,Docker%20Hub.%20You%20should%20see%20output%20similar%20to%3A)

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
```

Push this Image to the image repository:
```
docker push yourusername/my-java-app:latest
```

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](https://cloud.google.com/kubernetes-engine/), [Azure's AKS](https://learn.microsoft.com/en-us/azure/aks/), [AWS's EKS](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html) or something on local system with help of [kind](https://kind.sigs.k8s.io/) or [minikube](https://minikube.sigs.k8s.io/docs/).

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 _<your-file-name>.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
```

Once you have made the required changes, save the file and run this command:
```
kubectl apply -f <your-file-name>.yaml
```
---

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 :)