k8s cronjob – 从配置映射获取镜像(标签)?

huangapple go评论93阅读模式
英文:

k8s cronjob - get image (tag) from config map?

问题

在一个Kubernetes的CronJob中,我可以在作业执行时(而不是在部署时使用Helm等工具)从ConfigMap(或类似的东西)动态地拉取正在使用的镜像(或只是标签)吗?

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:{my-dynamic-tag-here}

我的用例是,我有很多Cron作业,它们在不同的时间创建,我需要在以后的时间更改用于它们的镜像/标签。

英文:

In a k8s cronjob, can I pull the image (or just the tag) which is being used dynamically (at job execution time, not at deployment time using helm etc.) from a configmap (or something similar)?

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:{my-dynamic-tag-here}

My use case is that I have a lot of cron jobs which are getting created at different times and I would need to make changes to the image/tag that is being used for them at a later time.

答案1

得分: 1

image: 的值必须直接在Kubernetes清单中指定。Kubernetes本身不执行任何查找、替换或间接引用以修改此值的操作。

设置此镜像是像Helm或Kustomize等包装工具的最有用的功能之一。我最熟悉的是Helm。在那里,您可以使用Helm的模板语言在部署时注入镜像标签:

# templates/cronjob.yaml
image: busybox:{{ .Values.dynamicTag }}

然后,当您实际进行部署时,可以在命令行中指定该值:

helm upgrade my-app . --set-string dynamicTag=20230710

还可以通过传递一个YAML(或JSON)部署时配置值文件的路径,如果您的CI工具可以编写此文件,这样可以更清晰。

Kustomize有一个特定的路径来更改image:的值。同样,这涉及到您的CI工具编写Kustomization捆绑包,或者在部署时运行kustomize edit CLI工具来修改它。

英文:

The image: value must be directly specified in the Kubernetes manifest. Nothing in Kubernetes proper does any sort of lookup, substitution, or indirect reference to modify this value.

Setting this image is one of the most useful abilities of wrapper tools like Helm or Kustomize. I'm most familiar with Helm. There you'd use Helm's templating language to inject the image tag at deployment time

# templates/cronjob.yaml
image: busybox:{{ .Values.dynamicTag }}

and then when you actually go to deploy it, you can specify that value at the command line

helm upgrade my-app . --set-string dynamicTag=20230710

There is also a path to pass a file of YAML (or JSON) deploy-time configuration values, which can be clearer if your CI tool can write this file.

Kustomize has a specific path to change the image: value. Again, this involves your CI tool writing out the Kustomization bundle, or running the kustomize edit CLI tool to modify it at deploy time.

huangapple
  • 本文由 发表于 2023年7月10日 17:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定