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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With