透過檔案將 Pod 資訊公開給容器

此頁面說明 Pod 如何使用 downwardAPI,將關於自身的資訊公開給 Pod 中執行的容器。downwardAPI 卷可以公開 Pod 欄位和容器欄位。

在 Kubernetes 中,有兩種方式可以將 Pod 和容器欄位公開給正在執行的容器

這兩種公開 Pod 和容器欄位的方式統稱為向下 API

開始之前

您需要有一個 Kubernetes 叢集,並且必須設定 kubectl 命令列工具以與您的叢集通訊。建議在至少有兩個不充當控制平面主機的節點的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用以下 Kubernetes playground 之一

儲存 Pod 欄位

在本練習的這部分,您將建立一個具有一個容器的 Pod,並將 Pod 層級的欄位作為檔案投影到正在執行的容器中。以下是 Pod 的 manifest

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example
  labels:
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  annotations:
    build: two
    builder: john-doe
spec:
  containers:
    - name: client-container
      image: registry.k8s.io/busybox
      command: ["sh", "-c"]
      args:
      - 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:
    - name: podinfo
      downwardAPI:
        items:
          - path: "labels"
            fieldRef:
              fieldPath: metadata.labels
          - path: "annotations"
            fieldRef:
              fieldPath: metadata.annotations

在 manifest 中,您可以看到 Pod 有一個 downwardAPI 卷,並且容器將該卷掛載在 /etc/podinfo

查看 downwardAPI 下的 items 陣列。陣列的每個元素都定義一個 downwardAPI 卷。第一個元素指定 Pod 的 metadata.labels 欄位值應儲存在名為 labels 的檔案中。第二個元素指定 Pod 的 annotations 欄位值應儲存在名為 annotations 的檔案中。

建立 Pod

kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume.yaml

驗證 Pod 中的容器正在執行

kubectl get pods

檢視容器的日誌

kubectl logs kubernetes-downwardapi-volume-example

輸出顯示 labels 檔案和 annotations 檔案的內容

cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"

build="two"
builder="john-doe"

取得進入 Pod 中正在執行的容器的 shell

kubectl exec -it kubernetes-downwardapi-volume-example -- sh

在您的 shell 中,檢視 labels 檔案

/# cat /etc/podinfo/labels

輸出顯示 Pod 的所有標籤都已寫入 labels 檔案

cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"

同樣地,檢視 annotations 檔案

/# cat /etc/podinfo/annotations

檢視 /etc/podinfo 目錄中的檔案

/# ls -laR /etc/podinfo

在輸出中,您可以看到 labelsannotations 檔案位於臨時子目錄中:在本範例中,為 ..2982_06_02_21_47_53.299460680。在 /etc/podinfo 目錄中,..data 是指向臨時子目錄的符號連結。同樣在 /etc/podinfo 目錄中,labelsannotations 也是符號連結。

drwxr-xr-x  ... Feb 6 21:47 ..2982_06_02_21_47_53.299460680
lrwxrwxrwx  ... Feb 6 21:47 ..data -> ..2982_06_02_21_47_53.299460680
lrwxrwxrwx  ... Feb 6 21:47 annotations -> ..data/annotations
lrwxrwxrwx  ... Feb 6 21:47 labels -> ..data/labels

/etc/..2982_06_02_21_47_53.299460680:
total 8
-rw-r--r--  ... Feb  6 21:47 annotations
-rw-r--r--  ... Feb  6 21:47 labels

使用符號連結可以動態原子地重新整理中繼資料;更新會寫入新的臨時目錄,並且 ..data 符號連結會使用 rename(2) 原子地更新。

退出 shell

/# exit

儲存容器欄位

在前面的練習中,您使用向下 API 使 Pod 層級的欄位可存取。在下一個練習中,您將傳遞屬於 Pod 定義一部分的欄位,但這些欄位取自特定的 容器,而不是來自整個 Pod。以下是 Pod 的 manifest,它再次只有一個容器

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example-2
spec:
  containers:
    - name: client-container
      image: registry.k8s.io/busybox:1.24
      command: ["sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          if [[ -e /etc/podinfo/cpu_limit ]]; then
            echo -en '\n'; cat /etc/podinfo/cpu_limit; fi;
          if [[ -e /etc/podinfo/cpu_request ]]; then
            echo -en '\n'; cat /etc/podinfo/cpu_request; fi;
          if [[ -e /etc/podinfo/mem_limit ]]; then
            echo -en '\n'; cat /etc/podinfo/mem_limit; fi;
          if [[ -e /etc/podinfo/mem_request ]]; then
            echo -en '\n'; cat /etc/podinfo/mem_request; fi;
          sleep 5;
        done;
      resources:
        requests:
          memory: "32Mi"
          cpu: "125m"
        limits:
          memory: "64Mi"
          cpu: "250m"
      volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "cpu_limit"
            resourceFieldRef:
              containerName: client-container
              resource: limits.cpu
              divisor: 1m
          - path: "cpu_request"
            resourceFieldRef:
              containerName: client-container
              resource: requests.cpu
              divisor: 1m
          - path: "mem_limit"
            resourceFieldRef:
              containerName: client-container
              resource: limits.memory
              divisor: 1Mi
          - path: "mem_request"
            resourceFieldRef:
              containerName: client-container
              resource: requests.memory
              divisor: 1Mi

在 manifest 檔案中,您可以看到 Pod 有一個 downwardAPI 卷宗,並且該 Pod 中的單一容器將該卷宗掛載在 /etc/podinfo

查看 downwardAPI 下的 items 陣列。陣列中的每個元素都定義了 downward API 卷宗中的一個檔案。

第一個元素指定在名為 client-container 的容器中,limits.cpu 欄位的值應以 1m 指定的格式發佈為名為 cpu_limit 的檔案。divisor 欄位是選填的,預設值為 1。除數為 1 表示 cpu 資源的核心數,或 memory 資源的位元組數。

建立 Pod

kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume-resources.yaml

取得進入 Pod 中正在執行的容器的 shell

kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh

在您的 Shell 中,檢視 cpu_limit 檔案

# Run this in a shell inside the container
cat /etc/podinfo/cpu_limit

您可以使用類似的指令來檢視 cpu_requestmem_limitmem_request 檔案。

將鍵投影到特定路徑和檔案權限

您可以根據每個檔案將鍵投影到特定路徑和特定權限。如需更多資訊,請參閱 Secrets

下一步

  • 閱讀 Pod 的 spec API 定義。這包括容器(Pod 的一部分)的定義。
  • 閱讀您可以使用 downward API 公開的 可用欄位 清單。

在舊版 API 參考文件中閱讀關於卷宗的資訊

  • 查看 Volume API 定義,其中定義了 Pod 中供容器存取的通用卷宗。
  • 查看 DownwardAPIVolumeSource API 定義,其中定義了包含 Downward API 資訊的卷宗。
  • 查看 DownwardAPIVolumeFile API 定義,其中包含對物件或資源欄位的參考,用於在 Downward API 卷宗中填入檔案。
  • 查看 ResourceFieldSelector API 定義,其中指定了容器資源及其輸出格式。
上次修改時間:2023 年 8 月 24 日下午 6:38 PST:Use code_sample shortcode instead of code shortcode (e8b136c3b3)