Interested in Solving your Challenges with XenonStack Team

Get Started

Get Started with your requirements and primary focus, that will help us to make your solution

Proceed Next

DevOps

Deploying Java Microservices Application on Kubernetes

Navdeep Singh Gill | 29 October 2024

Java Microservices Application Deployment on Kubernetes

Overview of Java Microservices Deployment

Running Containers at any real-world scale requires container orchestration and scheduling platform like Docker Swarm,  Mesos, AWS ECS but the most popular out of it is Kubernetes. Kubernetes is an open source system for automating deployment and management of containerized applications. In this post, We’ll share the process how you can Develop and Deploy Microservices based Java Application on the Container Environment -  Docker and Kubernetes and adopt DevOps in existing Java Application.
Serverless computing helps users to write and deploy without being worrying about the underlying infrastructure. Click to explore about, Microservices with Python on Kubernetes

Prerequisites for Running Containers

To follow this guide you need -
  • Kubernetes
  • Kubectl
  • Shared Persistent Storage- Option are GlusterFS, Ceph FS, AWS EBS,AzureDisk etc.
  • Java Application Source Code
  • Dockerfile
  • Container-Registry
Kubernetes is an open-source platform that automates container operations and Minikube is best for testing kubernetes. Kubectl is a command line interface to manage the kubernetes cluster either remotely or locally. To configure kubectl on your machine follow this link. Shared Persistent Storage is permanent storage that we can attach to the kubernetes container so that we don`t lose our data even container dies. We will be using GlusterFS as the persistent data store for kubernetes container applications. Application Source Code is the source code that we want to run inside a kubernetes container. Dockerfile contains a bunch of commands to build java application. The Registry is an online image store for container images. The below-mentioned options are few most popular registries.
Continuous integration is an approach in which developers merge their code into a shared repository several times a day. Click to explore about, Continuous Integration and Continuous Delivery

How to create a Dockerfile?

The below-mentioned code is sample dockerfile for Java applications. In which we are using Maven 3 as the builder and OpenJDK 8 as Java development environment with Alpine Linux due to its very compact size.
FROM maven: 3 - alpine

MAINTAINER XenonStack

# Creating Application Source Code Directory
RUN mkdir - p / usr / src / app

# Setting Home Directory
for containers
WORKDIR / usr / src / app

# Copying src code to Container
COPY. / usr / src / app

# Building From Source Code
RUN mvn clean package

# Setting Persistent drive
VOLUME["/data"]

# Exposing Port
EXPOSE 7102

# Running Java Application
CMD[&quot;java&quot;, &quot;-jar&quot;, &quot;target/<name jar="" of="" your="">.jar&quot;]

Building Java Application Image

The below-mentioned command will build your application container image.
$ docker build - t < name of your java application > : < version of application >

Publishing Container Image

Now we publish our Java application container images to any container registry like Docker Hub, AWS ECR, Google Container Registry, Private Docker Registry. I am using docker hub registry to publish images to the Kubernetes cluster. Create a account on docker hub and create a Public/Private Repository of your application name. To login to your docker hub account. Execute the below-mentioned command.
$ docker login
Now we need to retag java application image and push them to docker hub container registry. To Retag application container image
$ docker tag &lt; name of your application &gt; : &lt; version of your application &gt; &lt; 
your docker hub account &gt; /<name of="" repository="" your="">:
<version application="" of="" your="">
To Push application container Images
$ docker push &lt; your docker account &gt; /<name of="" repository="" your="">:
<version application="" of="" your="">
Similarly, we can push images to any of above-mentioned container registry like Docker Hub, AWS ECR, Google Container Registry, Private Docker Registry etc.

Setting Persistent Volume

Persistent Volume is only required if your application has to save some data other than a database like documents, images, video etc then we need to use the persistent volume that kubernetes support like was AWS EBC, CephFS, GlusterFS, Azure Disk, NFS etc. I will be attaching GlusterFS volume as a persistent volume to the kubernetes container. GlusterFS volumes are very simple and easy to create. Follow the below mentioned commands.
$ gluster volume create < application name > replica 2 transport tcp < node 1 > : /mnt/brick
1 / < application name > < node 2 > : /mnt/brick
1 / < application name >
 $ gluster volume start < application name >
 $ gluster volume info < application name >

Creating Deployment files for Kubernetes

Deploying application on kubernetes with ease using deployment and service files either in JSON or YAML format.
  • Deployment File
Following Content is for “ <name of application>.deployment.yml” file of python container application.
apiVersion: extensions / v1beta1
kind: Deployment
metadata:
 name: < name of application >
 namespace: < namespace of Kubernetes >
 spec:
 replicas: 1
template:
 metadata:
 labels:
 k8s - app: < name of application >
 spec:
 containers:
 -name: < name of application >
 image: < image name > : < version tag >
 imagePullPolicy: "IfNotPresent"
ports:
 -containerPort: 7102
volumeMounts:
 -mountPath: /data
name: < name of application >
 volumes:
 -name: < name of application >
 glusterfs:
 endpoints: glusterfs - cluster
path: < name of application >
 readOnly: false
  • Service File
Following Content is for “ <name of application>.service.yml” file of python container application.
apiVersion: v1
kind: Service
metadata:
 labels:
 k8s - app: < name of application >
 name: < name of application >
 namespace: < namespace of Kubernetes >
 spec:
 type: NodePort
ports:
 -port: 7102
selector:
 k8s - app: < name of application >

Running Java Application on Kubernetes

Java Container Application can be deployed either by kubernetes Dashboard or Kubectl (Command line). I`m explaining command line that you can use in production kubernetes cluster.
$ kubectl create -f <name application="" of="">.deployment.yml
$ kubectl create -f <name application="" of="">.service.yml
Now we have successfully deployed Java Application on Kubernetes.

Verification

We can verify application deployment either by using Kubectl or Kubernetes Dashboard. Below mentioned command will show you running pods of your application with status running/terminated/stop/created.
$ kubectl get po--namespace = < namespace of kubernetes > | grep < application name >
Result of above command Information of Kubernetes Pods

Testing

Get the External Node Port using the below-mentioned command.External Node Port is in the range from 30000 to 65000. Launch web Browser and open any of the below-mentioned URLs.
$ kubectl get svc --namespace=<namespace kubernetes="" of=""> | grep <application name="">
  • http://<kubernetes master ip address >: <application service port number>
  • http://<cluster ip address >: <application port number>

Troubleshooting

  • Check Status of Pods.
  • Check Logs of Pods/Containers.
  • Check Service Port Status.
  • Check requirements/dependencies of application.

    Managing Applications using prometheus and grafana
    Prometheus is best for open source Cloud Native Systems and service monitoring. The main component of Prometheus consists of Prometheus Server responsible for monitoring. It is a Time-Series database used to scrape data at regular intervals having container based infrastructure. It scrapes and stores data by HTTP pull and GET requests. Grafana models collected data into useful and excellent dashboards for visualization. Web UI used for graph expressions and Alert Manager to trigger alerts from Prometheus Server.

    Visualization layer with Grafana

    Grafana is an open source software for Time-Series Analytics. It sets alert rules based on metrics data. On alert state change, it notifies through various channels.

    Prometheus and Grafana Real-Time Applications

    • Banking Sector for performance reporting.
    • Alert Noise management through Prometheus Server.
    • Containers CheckUp.
    • Kubernetes Integration.
    • Software as a Service.
    • Data Storage and Migration.

Compressive Strategy

DevOps Consulting Services provides DevOps Assessment and Audit of your existing Infrastructure, Development Environment and Integration that includes continuous integration and Deployment, microservices architecture, cloud infrastructure, and serverless computing. To know more about this holistic approach, you are advised to look into the below steps:

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now