skip to Main Content

I want to deploy two containers in a pod through a deployment. But I want the pod to have exactly the name yoda. But in my case, a random string is always append after yoda like that yoda-f8bcb7bf4-khml6. Is it possible to force the pod name? I try the following but I did not get what I expected.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: yoda
  name: yoda
spec:
  replicas: 1
  selector:
    matchLabels:
      app: yoda
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      name: yoda
      labels:
        app: yoda
    spec:
      containers:
      - image: busybox
        name: anakin
        resources: {}
      - image: nginx
        name: obiwan
        resources: {}
status: {}

Regards,
Benoît

2

Answers


  1. This may not be the answer you expect but with Kubernetes pods should not be seen as pets, i. e. they should not receive a lot of attention but considered as highly replaceable. The name generation is part of this consideration among others to avoid conflicts.

    Almost all ways of Kubernetes involve a kind of decoupling, including container rollouts. If a pod always receives the same name it cuts itself from things like rolling deployment strategies, in which on pod terminates while another spawns. Alternatively a conflict would be the alternative.

    Without a deeper discussion why the pod should be maintained by hand I am not sure you will find a proper solution.

    To give some perspective:

    Labels (which you already use) give a good way to select a certain pod. If you change the deployment with a different image there might be two pods be selectable with your yoda label.

    So, if you want to select either the older or the newer pod (but not both), adding another label with the respective version could solve the distinguishing problem (if that is what you want). See the template metadata section below.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: yoda
      name: yoda
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: yoda
      strategy: {}
      template:
        metadata:
          name: yoda
          labels:
            app: yoda
            app.version: 2.0.0
        spec:
          containers:
          - image: busybox
            name: anakin
            resources: {}
          - image: nginx
            name: obiwan
            resources: {}
    

    I hope this helps.

    Login or Signup to reply.
  2. I am not sure if statefulset can solve your issue. But the statefulset always retain pod name.How ever it also append a numeric number(start from 0) after the pod name & goes upto no of replicas you define in the yaml definition file.

    For example, if you define the replica count to 3 in statefulset definition yaml file, then pod’s name will be listed below.

    [podName]-0

    [podName]-1

    [podName]-2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search