1

So I am not a good coder in python or an kubernetes expert but I have a project that need to do this:

  • In python, I want to connect to the BMC (ilo interface of of a baremetal node) to get some hardware info. My goal is to create a daemonset so the code can run on every node of the k8s cluster and retreive some hardware info. Now, I need the code to detect on which node the daemon is currently running so I can use this a way to connect to the node bmc interface with some API calls (like, if the node detected is node1.domain.com, I can then check node1.bmc.domain.com for ex). If my question is not clear enough, please let me know. If you can give me some code sample that could acheive this, it will very appreciated :) Thanks!

Right now, I have only in python a way to connect to the K8s api and get the list of nodes of a cluster but I do not found a way to detect while running as a pod, which node the pod is currently running. Found some infos here https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod but not sure how to combine runing the code in a pod and getting the pod own info.. I saw this also how to get the host name of the node where a POD is running from within POD but not sure if I have to add something to the pod or the info comes as a environement variable already in a pod.

  • The second question you link to describes using the Kubernetes "downward API" to inject runtime environment variables into the container. You would need to include that setup in your pod spec, but once you have that, it should be an ordinary environment variable you can see in Python `os.environ`. – David Maze Feb 18 '23 at 12:02

1 Answers1

0

You can use the downward API to get pod details to specific container as below:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-daemon-set
  namespace: my-namespace
spec:
  selector:
    matchLabels:
      name: app-name
  template:
    metadata:
      labels:
        name: app-name
    spec:
      containers:
      - name: my-image-name
        image: my-image:v1
        env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi

More info in Expose Pod Information to Containers Through Environment Variables

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43