Overview
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
To follow this guide you need -
- Kubernetes
- Kubectl
- Shared Persistent Storage — Option are GlusterFS , Ceph FS, AWS EBS, AzureDisketc.
- 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.
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 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 ["java", "-jar", "target/<name of your jar>.jar"]
Building Java Application Image
$ docker build -t <name of your java application>:<version of application>
The below-mentioned command will build your application container image.
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 <name of your application>:<version of your application> <your docker hub account >/<name of your repository >:<version of your application>
To Push application container Images
$ docker push <your docker account >/<name of your repository >:<version of your application>
Similarly, we can push images to any of above-mentioned container registry like Docker Hub, AWS ECR, Google Container Registry, Private Docker Registry etc.
No comments:
Post a Comment