Home > other >  kubernetes pod names Keep changing
kubernetes pod names Keep changing

Time:07-03

I need to make some automation testing on the Kubernetes pod but the pods names keeps changing like at first it was hisham-7cc8f99597 then after some time it is like this hisham-7cc8f99597-8j8lj is there a way I can know what is the name of the pod right now so I can use the name in the automation scenario ?

CodePudding user response:

Pods are cattle, not pets (devops.stackexchange.com). Thus, pods do not have an identity. We should therefore never directly communicate with a pod, but rather expose pods through a Service and communicate with the service instead. The service has a well-defined and constant name.

CodePudding user response:

To get the predictable pod names you must use state full sets

CodePudding user response:

It depends on what your tests do and why do you need to know the Pod's name.

If you just need the Pod's name:

Assuming you have access to the cluster and kubectl in your tests, and that your pod is part of a deployment that has a specific label like app=nginx, you can query for its name like this:

kubectl get po --selector=app=nginx-o=name

The output will be something like

pod/yourDeploymentName-7bd6c579fd-7gxzn

You can trim the leading pod/ through awk:

kubectl get po --selector=app=nginx-o=name | awk -F "/" '{print $2}'

CodePudding user response:

The variance in the name is due to you using Deployments, and is by design.

If you really need to use automation, don't go by pod name, go via the deployments. So instead of using

kubectl exec -it -n {namespace} {podname} -- {command}

Use

kubectl exec -it -n {namespace} deployments/{name-of-deployment} -- {command}

This will randomly pick one of the deployment pods to run the command.

If you need a specific pod, then you should use a statefulset which will name your pods in a specific order: {statefulset}-0, {statefulset}-1 and so on and you can then go to a specific one for your command.

CodePudding user response:

Here just answering your answering as you have asked, otherwise it's good to use the service name for communication not POD name if you are using that.

Either you can run your workload as POD however it's not good idea if you are running it as deployment.

If you are running as POD you can maintain the single name each time you want.

Extra :

You can also use the metadata or downwardAPI to get the details of the existing POD.

name will get injected into the environment variable (MY_POD_NAME) and your automation can get it automatically, use it with os.env.

Example :

apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
          printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
          sleep 10;
        done;
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName

Read more at : https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

  • Related