前提条件:

  • K8S集群中正确部署了virtual-kubelet(serverless Kubernetes 默认集成)

ECI像原生K8S一样支持DownwardAPI,目前已经支持通过DownwardAPI传递以下信息到ECI实例:

  • metadata.annotations
  • metadata.labels
  • metadata.name
  • metadata.namespace
  • metadata.uid

本文以传递annotations/labels为例,将以下内容保存为pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-downwardapi
  labels:
    region: cn-beijing
    zone: cn-beijing-h
  annotations:
    eci/test: virtual-kubelet
spec:
  nodeName: virtual-kubelet
  containers:
    - name: nginx
      image: nginx
      command: ["sh", "-c"]
      args:
      - while true; do cat /etc/podinfo/labels; sleep 5; cat /etc/podinfo/annotations; 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

使用kubect创建ECI实例:

# kubectl create -f pod.yaml
pod/test-downwardapi created
# kubectl get pod test-downwardapi
NAME               READY   STATUS    RESTARTS   AGE
test-downwardapi   1/1     Running   0          32s
# kubectl logs -f test-downwardapi
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"
zone="cn-beijing-h"eci/test="virtual-kubelet"region="cn-beijing"