【问题标题】:container labels in kubernetesKubernetes 中的容器标签
【发布时间】:2018-12-28 22:21:12
【问题描述】:

我正在使用 jenkins 构建我的 docker 映像:

docker build --build-arg VCS_REF=$GIT_COMMIT \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg BUILD_NUMBER=$BUILD_NUMBER -t $IMAGE_NAME\ 

我使用的是 Docker,但我正在迁移到 k8。

使用 docker,我可以通过以下方式访问这些标签:

docker inspect --format "{{ index .Config.Labels \"$label\"}}" $container

如何使用 Kubernetes 访问这些标签?

我知道要在我的 yaml 文件的 .Metadata.labels 中添加这些标签,但我不太喜欢它,因为 - 它将这些信息链接到部署而不是容器本身
- 可以随时修改
...

kubectl describe pods

谢谢

【问题讨论】:

  • 我有同样的问题,暂停容器可以从 Pod 获取这些标签,但我的应用程序容器不能。

标签: docker kubernetes


【解决方案1】:

Kubernetes 不会公开这些数据。如果是这样,它将成为PodStatus API 对象(及其嵌入的ContainerStatus)的一部分,这是Pod 数据的一部分,将被kubectl get pod deployment-name-12345-abcde -o yaml 转储出来。

你可以考虑在 Docker 镜像标签中编码一些数据;例如,如果 CI 系统正在构建一个标记提交,则使用源代码管理标记名称作为图像标记,否则使用提交哈希或序列号。另一个典型的路径是使用像 Helm 这样的部署管理器作为部署的主要事实来源,如果你这样做,那么可以有一条从你的 CD 系统到 Helm 到 Kubernetes 的路径,可以传递标签或注释。您通常还可以设置软件以在构建时了解其自己的构建日期和源代码控制提交 ID,然后通过仅供参考的 API(如 HTTP GET /_version 调用或类似的)公开该信息。

【讨论】:

    【解决方案2】:

    我将添加另一个选项。

    我建议阅读 K8S 的 Recommended Labels

    Key                           Description                     
    app.kubernetes.io/name        The name of the application     
    app.kubernetes.io/instance    A unique name identifying the instance of an application  
    app.kubernetes.io/version     The current version of the application (e.g., a semantic version, revision hash, etc.)
    app.kubernetes.io/component   The component within the architecture 
    app.kubernetes.io/part-of     The name of a higher level application this one is part of
    app.kubernetes.io/managed-by  The tool being used to manage the operation of an application
    

    所以你可以使用标签来描述一个 pod:

    apiVersion: apps/v1
    kind: Pod # Or via Deployment
    metadata:
      labels:
        app.kubernetes.io/name: wordpress
        app.kubernetes.io/instance: wordpress-abcxzy
        app.kubernetes.io/version: "4.9.4"
        app.kubernetes.io/managed-by: helm
        app.kubernetes.io/component: server
        app.kubernetes.io/part-of: wordpress
    

    并使用downward api(其工作方式类似于编程语言中的反射)。

    有两种方法可以将 Pod 和 Container 字段暴露给正在运行的 Container:

    1) 环境变量。
    2 ) 卷文件。

    以下是使用卷文件的示例:

    apiVersion: v1
    kind: Pod
    metadata:
      name: kubernetes-downwardapi-volume-example
      labels:
        version: 4.5.6
        component: database
        part-of: etl-engine
      annotations:
        build: two
        builder: john-doe
    spec:
      containers:
        - name: client-container
          image: k8s.gcr.io/busybox
          command: ["sh", "-c"]
          args:  # < ------ We're using the mounted volumes inside the container
          - while true; do
              if [[ -e /etc/podinfo/labels ]]; then
                echo -en '\n\n'; cat /etc/podinfo/labels; fi;
              if [[ -e /etc/podinfo/annotations ]]; then
                echo -en '\n\n'; cat /etc/podinfo/annotations; fi;
              sleep 5;
            done;
          volumeMounts:
            - name: podinfo
              mountPath: /etc/podinfo
      volumes:   # < -------- We're mounting in our example the pod's labels and annotations
        - name: podinfo
          downwardAPI:
            items:
              - path: "labels"
                fieldRef:
                  fieldPath: metadata.labels
              - path: "annotations"
                fieldRef:
                  fieldPath: metadata.annotations
    

    请注意,在示例中,我们访问了传递并安装到 /etc/podinfo 路径的标签和注释。

    除了标签和注释之外,向下 API 还提供了多个附加选项,例如:

    • pod 的 IP 地址。
    • pod 的服务帐户名称。
    • 节点的名称和 IP。
    • 容器的 CPU 限制、CPU 请求、内存限制、内存请求。

    查看here中的完整列表。


    (*) 一个讨论向下 API 的 nice blog


    (**) 您可以使用

    查看所有 pod 标签
    $ kubectl get pods --show-labels
    NAME                       ...         LABELS
    my-app-xxx-aaa                         pod-template-hash=...,run=my-app
    my-app-xxx-bbb                         pod-template-hash=...,run=my-app
    my-app-xxx-ccc                         pod-template-hash=...,run=my-app 
    fluentd-8ft5r                          app=fluentd,controller-revision-hash=...,pod-template-generation=2
    fluentd-fl459                          app=fluentd,controller-revision-hash=...,pod-template-generation=2
    kibana-xyz-adty4f                      app=kibana,pod-template-hash=...
    recurrent-tasks-executor-xaybyzr-13456 pod-template-hash=...,run=recurrent-tasks-executor
    serviceproxy-1356yh6-2mkrw             app=serviceproxy,pod-template-hash=...
    

    或仅查看带有$ kubectl get pods -L &lt;label_name&gt; 的特定标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2023-02-05
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      相关资源
      最近更新 更多