22.3.2. 使用卷插件
消耗 Downward API 的另一个机制是使用卷插件。Downward API 卷插件会创建一个将字段扩展到文件中的卷。VolumeSource
API 对象的 metadata
字段用于配置这个卷。插件支持以下字段:
- Pod 名称
- Pod 命名空间
- Pod 注解
- Pod 标签
例 22.1. Downward API 卷插件配置
例如:
创建
volume-pod.yaml
文件:kind: Pod apiVersion: v1 metadata: labels: zone: us-east-coast cluster: downward-api-test-cluster1 rack: rack-123 name: dapi-volume-test-pod annotations: annotation1: "345" annotation2: "456" spec: containers: - name: volume-test-container image: gcr.io/google_containers/busybox command: ["sh", "-c", "cat /tmp/etc/pod_labels /tmp/etc/pod_annotations"] volumeMounts: - name: podinfo mountPath: /tmp/etc readOnly: false volumes: - name: podinfo downwardAPI: defaultMode: 420 items: - fieldRef: fieldPath: metadata.name path: pod_name - fieldRef: fieldPath: metadata.namespace path: pod_namespace - fieldRef: fieldPath: metadata.labels path: pod_labels - fieldRef: fieldPath: metadata.annotations path: pod_annotations restartPolicy: Never
从
volume-pod.yaml
文件创建 pod:$ oc create -f volume-pod.yaml
检查容器的日志,并验证配置的字段是否存在:
$ oc logs -p dapi-volume-test-pod cluster=downward-api-test-cluster1 rack=rack-123 zone=us-east-coast annotation1=345 annotation2=456 kubernetes.io/config.source=api