Kubernetes control pod name

7/10/2020

Kubernetes is a very sophisticated tool, but some of us are a bit crude, and so we get in trouble.

I'm trying to run a simple kubernetes job on a pod in my cluster, and in the kubernetes yaml config file i define the name of the pod under metadata like

apiVersion: batch/v1
kind: Job
metadata:
  name: my-job

Then when I create this job I see that the name of the pod is not really my-job. It's:

my-job-'randomstuff' 

I understand this is very cool for replicasets and whatnots, but I need my pod to be named what I tell it to be named, because I use that name in callbacks function further down the road..

It seems to me to be strange, that I can't have complete control over what I want to call my pod when I create it.

I tell myself that it must be possible, but I've googledfrenzied for an hour..

Thank you very much for any ideas :)

-- 1233023
kubernetes

3 Answers

7/10/2020

Please ask yourself these questions:

  1. What's a job?
  2. Who should communicate with who? A Job can communicate with others (services, external,...) or others can communicate with Job.

A Job is an ephemeral process/ephemeral pod. It is designed to be completed once the job of Job :) is done.

Because it's ephemeral (temporary), it is expected to reach others, and not others will reach it.

If we agree on this, assigning exact name to a pod created by Job should be useless.

If you insist that you have some edge-cases where others are waiting this job to be up so they can communicate with its pod. in this case, think about using service (kind: Service) in the front of this pod.


Is it doable? Yes.

Does it make sense? No.

-- Abdennour TOUMI
Source: StackOverflow

7/10/2020

There could be other way to solve it depending on what you are trying to achieve in the callback function.

For example you could add any label or annotation on the pod and use those in the callback function to refer to the pod or a group of pods with same label or annotation.

-- Arghya Sadhu
Source: StackOverflow

7/10/2020

Jobs are designed to have random suffixes because they may have multiple completions.

Example:

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  completions: 5
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4   

As you can see, this Job will be executed until it achieves 5 competitions, and it would not be possible if the name didn't have a random suffix attached to it. Check the result of the execution of the Job of the example:

$ kubectl get pods 
NAME                            READY   STATUS      RESTARTS   AGE

pi-7mx4k                        0/1     Completed   0          3m9s
pi-bfw6p                        0/1     Completed   0          3m17s
pi-ls9lh                        0/1     Completed   0          3m43s
pi-njfpq                        0/1     Completed   0          3m35s
pi-ssn68                        0/1     Completed   0          3m27s

So, the answer to your question is no, you can't force it to use a "fixed" name.

If you need to have control under the name, consider using a Pod instead (kind Pod).

-- Mark Watney
Source: StackOverflow