Is there way to access region/zone information of pod inside container through some environment variable?

Hi I was trying to get pod zone/region information using environment variable inside container. I was able to get pod name using below config. Please help me to get zone/region information.

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: rc-sample
  namespace: default
  labels:
    app: rc-sample
  annotations:
    deployment.kubernetes.io/revision: '1'
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rc-sample
  template:
    metadata:
      name: rc-sample
      labels:
        app: rc-sample
        version: v1
    spec:
      containers:
      - name: rc-sample
        image: mli
        env: 
        - name: eureka_instance_hostname
          value: 'rc-sample'
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

Hi!

Some tools, like kops, add that information as labels or annotations to the nodes. If you have them, maybe you can access them?

For those kinds of things I find looking at the API reference very helpful as it is complete with every field that exist (or should :)): https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#envvar-v1-core

I think there is none for the node labels, but if the use case is really good and generally useful it can probably be added.

I see in that link that you have the nodeName. I don’t find the node labels. As a work around, maybe you can save the node names and regions and access that, or even use the kubernetes API (a service account to access it)

Would something like that help?

I dont want to make kubernetes API request. I want to get region/Zone information to be added to the application logs. So basically region/zone information should be made available on pod creation into environment variables. If something similar to Podname which I extracted is available it would be helpful.
I see there is a configMapref for envVar in the link u sepcified. Do you know any configMap which has mapping of Node to region/zone? which I could use since I know the nodename from Pod.

To my knowledge there’s no way of passing node labels. It has come up as an issue, but there hasn’t been much traction on it. One possible idea would be to use something like metacontroller or Open Policy Agent to inject that information into the pod by means of a Mutating Admission Webhook.

For sure there is no such a configmap created automatically. And, of course, that is dynamic in most environments, so it should be updated dynamically too.

Bob’s idea seem more appropriate if you really want that.

Can I ask what is the underlying issue you want to solve? The root? If that is it, okay, but if there is some other problem that you want to solve, there might be another way to tackle it besides having an env var with the region.

I just want region in logs. Underlying issue is to debug node/pod shutdowns based on region/Zone. since on pod/node restart nodename and pod Id changes, the only metadata independent of pod/Node restarts is region.

Yes, we could inject some agents as @mrbobbytables suggested which would pull node region/zone from kubernetes. But why should I use some third party agents since its a metadata of pod I would expect kubernetes to provide a way to access it.

@Cognitive_Genie I would chime in your support/use case for this feature in the current driving issue. There has been activity in some of the other issues, but they seem to be converging on #40610 and plan on bringing it up at a sig-node meeting.

#40610 seems like the right answer.

1 Like

Another option I just run into might be this:

https://github.com/coreos/afterburn/blob/master/README.md

I never used it, so not sure if it will work. But seems worth to consider :slight_smile: