Overview of Java Microservices DeploymentRunning 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 ContainersTo follow this guide you need -
- Shared Persistent Storage- Option are GlusterFS, Ceph FS, AWS EBS,AzureDisk etc.
- Java Application Source Code
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["java", "-jar", "target/<name jar="" of="" your="">.jar"]
Building Java Application ImageThe below-mentioned command will build your application container image.
$ docker build - t < name of your java application > : < version of application >
Publishing Container ImageNow 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.
Now we need to retag java application image and push them to docker hub container registry. To Retag application container image
$ docker login
To Push application container Images
$ docker tag < name of your application > : < version of your application > < your docker hub account > /<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.
$ docker push < your docker account > /<name of="" repository="" your="">:<version application="" of="" your="">
Setting Persistent VolumePersistent 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 KubernetesDeploying application on kubernetes with ease using deployment and service files either in JSON or YAML format.
- Deployment File
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
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 KubernetesJava 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.
Now we have successfully deployed Java Application on Kubernetes.
$ kubectl create -f <name application="" of="">.deployment.yml $ kubectl create -f <name application="" of="">.service.yml
VerificationWe 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.
Result of above command
$ kubectl get po--namespace = < namespace of kubernetes > | grep < application name >
TestingGet 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>
- Check Status of Pods.
- Check Logs of Pods/Containers.
- Check Service Port Status.
- Check requirements/dependencies of application.