How to use the instance ID in Daemonset config file in Kubernetes?

2/5/2019

I'd like to get the instance ID (e.g. AWS EC2 instance ID like i-19a9fa9s8df9a8, not the private dns node name) of where my pod is running from within my k8s config file, but couldn't find any documentation on how to do that. Anybody know how to use the reportingInstance field in https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#event-v1-core?

I'm getting this error:

The DaemonSet  is invalid:
spec.template.spec.containers[0].env[8].valueFrom.fieldRef.fieldPath: 
Invalid value: "core.reportingInstance": 
error converting fieldPath: field label not supported: core.reportingInstance

This is what I tried:

    - name: INSTANCE_ID
      valueFrom:
        fieldRef:
          fieldPath: core.reportingInstance

I already have this in my yaml file but that gives the private dns name not the instance ID

   - name: NODE_NAME
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName
-- user3226932
amazon-ec2
daemonset
kubernetes

1 Answer

2/5/2019

Let me answer this with my understanding of your question. i think you want your node name value set as env variable in your container. If this is true you can use below code for that

 - name: Node_Name
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName

If you want some other information about pod or container, please understand that you can get only limited information within container using downward api and that is limited to below mentioned field.

 fieldRef     <Object>
 Selects a field of the pod: supports metadata.name, metadata.namespace,
 metadata.labels, metadata.annotations, spec.nodeName,
 spec.serviceAccountName, status.hostIP, status.podIP.


resourceFieldRef     <Object>
 Selects a resource of the container: only resources limits and requests
 (limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu,
 requests.memory and requests.ephemeral-storage) are currently supported.
-- Rajesh Deshpande
Source: StackOverflow