Add random string on Kubernetes pod deployment name

旧街凉风 提交于 2021-02-18 06:36:09

问题


I have a template that is basically an utility container for running kubectl inside a pod.

What I want to do, is to be able to have multiple deployments of that same template, with different names, as in "utilitypod-randomID".

Is there a way to do that, via kubectl and some shell scripting, or something similar?

The current template looks like this:

apiVersion: v1
kind: Pod
metadata:
  name: utilitypod
  namespace: blah-dev
labels:
  purpose: utility-pod
spec:
  containers:
  - name: utilitypod
  image: blahblah/utilitypod:latest
  command: [ "/bin/bash", "-c", "--" ]
  args: [ "while true; do sleep 28800; done;" ]
  env: 
  - name: KUBERNETES_SERVICE_HOST
    value: "api.dev.blah.internal"
  - name: KUBERNETES_SERVICE_PORT
    value: "443"

回答1:


You can replace name with generateName, which adds a random suffix. Your template will look like this:

apiVersion: v1
kind: Pod
metadata:
  generateName: utilitypod-
  namespace: blah-dev
  labels:
    purpose: utility-pod
spec:
  containers:
    - name: utilitypod
      image: blahblah/utilitypod:latest
      command: [ "/bin/bash", "-c", "--" ]
      args: [ "while true; do sleep 28800; done;" ]
      env:
        - name: KUBERNETES_SERVICE_HOST
          value: "api.dev.blah.internal"
        - name: KUBERNETES_SERVICE_PORT
          value: "443"

Mind you, this will only work with kubectl create -f template.yaml, not apply, as apply looks for a resource by its name and tries to compare their definitions, but this template doesn't contain a specific name.



来源:https://stackoverflow.com/questions/48023475/add-random-string-on-kubernetes-pod-deployment-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!