Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Current Pod in Kubernetes Java Application

I am deploying my Microservices on Kubernetes Cluster. Every Application has 4 replicas or PODS. For a REST API in 1 application, I want to track which POD addressed my request. e.g. my /app/deploy(body contains app_id) request is handled by POD1.

For the same, I have imported Kubernetes jar in my application. In my code, I want to check the current POD on which this code is running. I want an API like kubernetesDiscoveryClient.getCurrentPOD(), something of this sort.

like image 637
SID Avatar asked Jul 30 '18 11:07

SID


People also ask

How do I check my pods in Kubernetes?

Using kubectl describe pods to check kube-system If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment.

What is Kubernates in Java?

Kubernetes, in short, is a system for orchestration of containerized applications across a cluster of nodes, including networking and storage infrastructure. Some of the most important features are: Resource scheduling: it ensures, that Pods are distributed optimally over all available nodes.

How do you check which pod is running on which node Kubernetes?

To find the cluster IP address of a Kubernetes pod, use the kubectl get pod command on your local machine, with the option -o wide . This option will list more information, including the node the pod resides on, and the pod's cluster IP. The IP column will contain the internal cluster IP address for each pod.


1 Answers

You do not need Kubernetes Jar in your Java application. A simple System.getenv("HOSTNAME") will give you the name of your Pod. Works on all platform, and since Kubernetes version 1.6 at least.

More formally, you could use the following in your Kube spec (detailed reference), and then read the environment using System.getenv("MY_POD_NAME") in Java.

 env:
 - name: MY_POD_NAME
   valueFrom:
     fieldRef:
       fieldPath: metadata.name     
like image 142
Bloodysock Avatar answered Oct 16 '22 06:10

Bloodysock