2

I want to run Python code inside a pod. The pod is created by airflow that I don't control.

I want to somehow get the name of the pod I'm running in.

How can it be done?

Felix
  • 3,351
  • 6
  • 40
  • 68

4 Answers4

6

You can tell kuberenetes to mount an env variable for you:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

and then in python you can access it like:

import os
pod_name = os.environ['MY_POD_NAME']

Or you can just open and read /etc/hostname:

f = open('/etc/hostname')
pod_name = f.read()
f.close()
testfile
  • 2,145
  • 1
  • 12
  • 31
  • I do not think he really need to set this, its available by default `os.environ['HOSTNAME']` – Adiii Aug 11 '22 at 12:07
  • @Adiii the HOSTNAME environment variable is not standard across shell types. Only `bash` creates that variable. Imo its better to rely on standards given by kubernetes – testfile Aug 11 '22 at 12:10
  • no, this is injected by kubernetes "The hostname of a Container is the name of the Pod in which the Container is running. It is available through the hostname command or the gethostname function call in libc." https://kubernetes.io/docs/concepts/containers/container-environment/ – Adiii Aug 11 '22 at 12:13
  • the hostname is set. Not the environment variable. – testfile Aug 11 '22 at 12:14
  • environment variable also, `command: [ "/bin/sh", "-c", "echo $HOSTNAME" ]` try this in your deployment, change image to alpine which does not come with bash. it will still work – Adiii Aug 11 '22 at 12:15
1

Exposing Pod and Cluster Vars to Containers

Lets say you need some data about Pod or K8s environment in your application to add Pod informnation as metada tp logs. such as e.g.

  • Pod IP
  • Pod Name
  • Service Account of Pod

NOTE: All Pod information can be made available in the config file.

There are 2 ways to expose Pod fields into a running Container:

  1. Environment Variables
  2. Volume Files

Example of Environment Variable

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-env
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
        - name: log-sider
          image: busybox
          command: [ 'sh', '-c' ]
          args:
            - while true; do
              echo sync app logs;
              printenv POD_NAME POD_IP POD_SERVICE_ASCCOUNT;
              sleep 20;
              done;
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_SERVICE_ASCCOUNT
              valueFrom:
                fieldRef:
                  fieldPath: spec.serviceAccountName

Kubernets Docs

Source

Ali
  • 922
  • 1
  • 9
  • 24
-1

one way can be

So in the Kubernetes cluster you are operating in do

kubectl get pods

now see the yaml of all pods by

oc get pods <pod-name> -o yaml

then in that find the container images being used by the pods . identify the image tag that belongs to your container creation. That means that when you build you image , image has a name and a tag , which is further pushed to some cloud hub from where your pod will pull the image and start the container . you need to find the image tag and name in pod yaml using the above commands given .

Abhijeet Jha
  • 119
  • 1
  • 6
-1

Try the below :

# List all pods in all namespaces
kubectl get pods --all-namespaces 

# List all pods in the current namespace          
kubectl get pods -o wide 

Then u can see more details using the below :

kubectl describe pod <pod-name>

Also you can refer to the following stackoverflow question and the related answers.

get-current-pod

Som
  • 1,522
  • 1
  • 15
  • 48