Retrieving Cluster name using Java

Hello, My Springboot Java application which process many records, is deployed to 3 clusters in Kuberenetes. I am in need to find which cluster is processing which record and store the cluster name in the table against the record. But I am unable to find a way to get the cluster name during runtime using my springboot Java code. I could see ways to find Pod name in runtime but not the cluster name. (Just Pod name doesn’t help me as I have 4 pods in each cluster)

Can Someone please help me out on this? Thank you!

Hi,
I assume the best way would be to read clusterName property from kubeadm-config config map

kubectl -n kube-system get cm kubeadm-config -o yaml
apiVersion: v1
data:
  ClusterConfiguration: |
    apiServer:
      extraArgs:
        authorization-mode: Node,RBAC
      timeoutForControlPlane: 4m0s
    apiVersion: kubeadm.k8s.io/v1beta3
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controllerManager: {}
    dns: {}
    etcd:
      local:
        dataDir: /var/lib/etcd
    imageRepository: registry.k8s.io
    kind: ClusterConfiguration
    kubernetesVersion: v1.29.0
    networking:
      dnsDomain: cluster.local
      podSubnet: 10.244.0.0/16
      serviceSubnet: 10.96.0.0/12
    scheduler: {}

Thank you so much for your reply!

But I wanted to get the cluster name using my java code during runtime and not by kubectl command. Can you please help me with that?

Hi,
You can try using GitHub - kubernetes-client/java: Official Java client library for kubernetes