在Kubernetes Pod部署名称上添加随机字符串 [英] Add random string on Kubernetes pod deployment name

查看:495
本文介绍了在Kubernetes Pod部署名称上添加随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板,基本上是一个实用容器,用于在容器中运行kubectl.

我想做的是能够使用相同的模板进行多个部署,并且使用不同的名称,例如"utilitypod-randomID".

有没有一种方法可以通过kubectl和一些shell脚本或类似的方法来实现?

当前模板如下:

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"

解决方案

您可以将name替换为generateName,这会添加一个随机后缀.您的模板如下所示:

 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"
 

请记住,这仅适用于kubectl create -f template.yaml,而不适用于apply,因为apply按名称查找资源并尝试比较其定义,但是此模板不包含特定名称. /p>

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"

解决方案

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.

这篇关于在Kubernetes Pod部署名称上添加随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆