Java Microservices Application Deployment on Kubernetes

Reading Time: 6 Minutes

by  | December 17, 2017  Java Microservices Application Deployment on Kubernetes

Overview of Deploying Microservices Based Java Application

Running Containers at any real-world scale requires container orchestration and scheduling platform like Docker Swarm,Apache 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.


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 command line interface to manage 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 died.we will be using GlusterFS as the persistent data store for kubernetes container applications.

Application Source Code is 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.

  • Private Docker Hub
  • AWS ECR
  • Docker Store
  • Google Container registry

Creating 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 Don

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

You May also Love to Read Kubernetes Overview, Monitoring & Security


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 >

Setting Persistent Volume For Kubernetes


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.


How Can Don Help You?

Our DevOps Consulting Services provides DevOps Assessment and Audit of your existing Infrastructure, Development Environment and Integration.

Our DevOps Professional Services includes - 

Continuous Integration & Continuous Deployment

Enable Secure Continuous Integration and Continuous Delivery Pipeline with Jenkins/Bamboo/TeamCity for delivery, automation, self-service environment, and On-demand. Deploy Java Application to production by decreasing the time by the automation of entire delivery pipeline comprising build, deploy, test, and release.

Microservices Architecture

Take a cloud-native approach to building Enterprise Applications for Web and Mobile with a Microservices Architecture. Run each service inside a container and combine all those containers to form a complex Microservices Application.

Cloud Infrastructure Services

Develop, Deploy and Manage your Java Application on leading Cloud Service Providers - AWS, Microsoft Azure, Google Cloud, and Container Environment - Kubernetes and Docker.

Serverless Computing

Build and run your applications and services without thinking about the servers with Don Serverless Computing Services. Serverless Computing offers No Server Management, Flexible Scaling, High Availability, No Idle Capacity.