Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set a specific name to a pod created from a yaml with an apply command

I would like to know if there is a way to launch a pod from a yaml but with a custom name.

The objective of all this is to be able to add to the pod name something like a user id and then be able to delete it when the user requires it, having this way an unequivocal way to find the pod.

It would be something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

kubectl apply -f ./foo.yaml -name custom-name

Thanks in advance!

>Solution :

You can’t do that with kubectl, but you can:

  • Perform the substitution with something like sed and pipe the result to kubectl apply:

    sed 's/PODNAME/custom-name/' foo.yaml |
      kubectl apply -f-
    

    (This assumes that you have name: PODNAME in the original manifest.)

  • Use a YAML processing tool to make the change. For example, using yq (note that there are multiple commands with that name; hence the link).

    yq '.metadata.name = "custom-name"' foo.yaml |
      kubectl apply -f-
    

    This has the advantage that you don’t need a specific sentinel name like PODNAME in the source file.

  • Create a Helm template and deploy the manifest(s) with helm, in which case you could set the value on the command line.

    helm install <your/repository> --set-string podname=custom-name
    
  • Use some other templating tool, such as Ansible’s k8s module.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading