大网数据广告活动
服务器运维问答

How to expose image names to pod for use a version tag? Kubernetes

· 52CE 我爱测

Is there an easy way to use container image name or tag as a ENV variable so that I can use it to provide the image name and tag in a pod (for version information). I am new user. This idea is to work-around my lack of knowledge …

问题原文

原帖链接 · Tim Richardson · CC BY-SA 4.0

Is there an easy way to use container image name or tag as a ENV variable so that I can use it to provide the image name and tag in a pod (for version information). I am new user. This idea is to work-around my lack of knowledge in how kustomization.yaml can source "variables" from a config-map.yaml

Currently I set up an environment variable which I envsubt into kustomization.yaml, and from there I use that value twice: once for the image name change, and once to make a config map.

The actual objective is to enter this one piece of information only once, and have it be used in those two ways. If the image name was available to the container via the environment, that is one way since I would not need to inject the string into a config map to fill the container env; I could deduce it from the image metadata and therefore enter the value only once in kustomize, in the image name replacement. This would meet my objective, although it is not a general solution, it is a workaround specifically related to assuming image name metadata is available for free in the container. But this is not possible.

So another way is to template kustomization.yaml, which is what I have done: this is my current workaround. The philosophy of kustomize is no templating. However, avoiding duplicate entry of key literals is a higher priority for me.

In each "site" folder, I have something like this:

file: kustomization_pre_subst.yaml



resources:
  - site-namespace.yaml
  - site-config-map.yaml
  - site-secret.yaml
  - site-ingress.yaml
  - ../base
namespace: test


images:
  - name: django_api_sync-image
    newName: "...dkr.ecr.aws.com/django_api_sync:image_prefix-${version}"

configMapGenerator:
- name: version-configmap
  literals:
  - django_api_sync_version="${version}"

So I have a configmap just with the version string. In the base file this is used as part of the environment.

... fragment from a container spec:

  envFrom:
    - configMapRef:
        name: site-configmap
    - configMapRef:
        name: common-configmap
    - configMapRef:
        name: version-configmap
    - secretRef:
        name: common-secret
    - secretRef:
        name: site-secret

This so far is the easiest solution that I can understand which lets me define the version string once, and have it select the image I want, and be passed to the container in env.

What I was hoping for is that I could instead define it literally in the site-configmap and then used this to provide the correct image name, that is, use the site-configmap as a source for kustomization.yaml rather than having to inject it like this.

pseudo code fragment of kustomization.yaml showing how it can source from a config-map (instead of injecting into a config map), like:

images:
  - name: django_api_sync-image
    newName: "...dkr.ecr.aws.com/django_api_sync:image_prefix-${version}"
    env:
        source-from: site-configmap
        name: version
        key: version

The point of the being that the version needs to defined only once. However, even then it won't be as nice as doing envsubst.

I am surely missing something.

已采纳解答

原帖链接 · mdaniel · CC BY-SA 4.0

As best I can tell, kustomize believes that replacements: are the future of that process and I just tried it with the example below and "it works on my machine" (kubectl kustomize . with v1.25.3)

In this scenario, I went from the Pod's image: out to the ConfigMap because kustomize is good at decomposing things (that is, splitting the tag from the repository) but joining them seemed like a lot more work. It's like all things in software, there's likely several ways to accomplish this, each with their own tradeoffs

Given

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: version-configmap
data:
  something-else: showing that we can append keys
# pod.yaml <-- just using it to make the example less verbose
apiVersion: v1
kind: Pod
metadata:
  name: the-pod
spec:
  containers:
  - image: example.com:3.14.15
    name: the-container
# kustomization.yaml
namespace: test
resources:
  - configmap.yaml
  - pod.yaml
replacements:
- source:
    kind: Pod
    name: the-pod
    fieldPath: spec.containers.0.image
    options:
      delimiter: ":"
      index: 1
  targets:
  - select:
      kind: ConfigMap
      name: version-configmap
    fieldPaths:
      - data.django_api_sync_version
    options:
      create: true

Produces

apiVersion: v1
data:
  django_api_sync_version: 3.14.15
  something-else: showing that we can append keys
kind: ConfigMap
metadata:
  name: version-configmap
  namespace: test
---
apiVersion: v1
kind: Pod
metadata:
  name: the-pod
  namespace: test
spec:
  containers:
  - image: example.com:3.14.15
    name: the-container

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-10-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。