需要传递给相关 pods 的部署名称

Need a deployment name passed to related pods

我有 docker 个包含 nodejs 应用程序的映像,我通过 kubernetes 部署部署了它,我有 3 个 pods。我需要的是将每个部署的名称传递给相关的 pods 这样我就有了每个部署的唯一 ID,它仅对该部署唯一,并且该部署中的所有 pods 都可以使用那个身份证。 这个也没有太大帮助:

我知道我可以在 :

env:
  - name: unique id
    value: {{uuidv4}}

但我更愿意:

env:
  - name: deployment name
    value: a way to get the deployment name

知道它是否可以实现吗?

因此,根据 Kubernetes 部署文档,一个用例是:

Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment. A new ReplicaSet is created and the Deployment manages moving the Pods from the old ReplicaSet to the new one at a controlled rate. Each new ReplicaSet updates the revision of the Deployment.

所以知道部署是不相关的,除非你想回滚。所以你需要的是让 pods 看到对方。在这种情况下,您需要一个无头服务。

https://dev.to/kaoskater08/building-a-headless-service-in-kubernetes-3bk8

在那里你可以获得 pod DNS 并通过 IP 或 DNS 在你的 Redis 中标记它们

编辑:

为了获得部署,每个 pod 都有一个名为 HOSTNAME 的环境变量,例如(在我的环境中):

HOSTNAME=stella-api-8675fcf6df-rm2m7

其中 stella-api 是当前部署名称,stella-api-8675fcf6df 是当前副本集,最后,stella-api-8675fcf6df-rm2m7 将是pod 特定 id

希望对您有所帮助。