来自Pod内的Kubernetes部署名称吗? [英] Kubernetes deployment name from within a pod?

查看:90
本文介绍了来自Pod内的Kubernetes部署名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取来自Pod内产生当前Pod的Kubernetes部署/职位名称?

How can I to source the Kubernetes deployment/job name that spawned the current pod from within the pod?

推荐答案

在许多情况下,Pod的主机名等于Pod的名称(您可以通过HOSTNAME环境变量访问该主机名).但是,这不是确定Pod身份的可靠方法.

In many cases the hostname of the Pod equals to the name of the Pod (you can access that by the HOSTNAME environment variable). However that's not a reliable method of determining the Pod's identity.

您将希望使用向下API ,该API可让您将元数据公开为环境变量和/或卷上的文件.

You will want to you use the Downward API which allows you to expose metadata as environment variables and/or files on a volume.

Pod的名称和名称空间可以作为环境变量(字段:metadata.namemetadata.namespace)公开,但是有关Pod创建者的信息(即 annotation )只能显示为文件.

The name and namespace of a Pod can be exposed as environment variables (fields: metadata.name and metadata.namespace) but the information about the creator of a Pod (which is the annotation kubernetes.io/created-by) can only be exposed as a file.

示例:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: busybox
  labels: {app: busybox}
spec:
  selector: {matchLabels: {app: busybox}}
  template:
    metadata: {labels: {app: busybox}}
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
        - "sh"
        - "-c"
        - |
          echo "I am $MY_POD_NAME in the namespace $MY_POD_NAMESPACE"
          echo
          grep ".*" /etc/podinfo/*
          while :; do sleep 3600; done
        env:
        - name: MY_POD_NAME
          valueFrom: {fieldRef: {fieldPath: metadata.name}}
        - name: MY_POD_NAMESPACE
          valueFrom: {fieldRef: {fieldPath: metadata.namespace}}
        volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo/
      volumes:
        - name: podinfo
          downwardAPI:
            items:
              - path: "labels"
                fieldRef: {fieldPath: metadata.labels}
              - path: "annotations"
                fieldRef: {fieldPath: metadata.annotations}

也看不到输出:

$ kubectl logs `kubectl get pod -l app=busybox -o name | cut -d / -f2`

输出:

I am busybox-1704453464-m1b9h in the namespace default

/etc/podinfo/annotations:kubernetes.io/config.seen="2017-02-16T16:46:57.831347234Z"
/etc/podinfo/annotations:kubernetes.io/config.source="api"
/etc/podinfo/annotations:kubernetes.io/created-by="{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"busybox-1704453464\",\"uid\":\"87b86370-f467-11e6-8d47-525400247352\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"191157\"}}\n"
/etc/podinfo/annotations:kubernetes.io/limit-ranger="LimitRanger plugin set: cpu request for container busybox"
/etc/podinfo/labels:app="busybox"
/etc/podinfo/labels:pod-template-hash="1704453464"

这篇关于来自Pod内的Kubernetes部署名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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