Home > Blockchain >  Kubernetes: how to get pod name I'm running in?
Kubernetes: how to get pod name I'm running in?

Time:08-11

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?

CodePudding user response:

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()

CodePudding user response:

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

CodePudding user response:

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 .

CodePudding user response:

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

  • Related