【云原生 Kubernetes 系列】K8s 实战 如何给应用注入数据 II 将pod数据传递给容器_pod容器传参

42 阅读1分钟
$ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-pod.yaml

  1. 验证 Pod 中的容器运行正常:
$ kubectl get pods

  1. 查看容器日志:
$ kubectl logs dapi-envars-fieldref

  1. 输出信息显示了所选择的环境变量的值:

minikube
dapi-envars-fieldref
default
172.17.0.4
default

要了解为什么这些值在日志中,请查看配置文件中的command 和 args字段。 当容器启动时,它将五个环境变量的值写入 stdout。每十秒重复执行一次。

接下来,通过打开一个 Shell 进入 Pod 中运行的容器:

$ kubectl exec -it dapi-envars-fieldref -- sh

在 Shell 中,查看环境变量:

/# printenv

输出信息显示环境变量已经设置为 Pod 字段的值。

MY_POD_SERVICE_ACCOUNT=default

MY_POD_NAMESPACE=default
MY_POD_IP=172.17.0.4

MY_NODE_NAME=minikube

MY_POD_NAME=dapi-envars-fieldref

1.1、用 Container 字段作为环境变量的值

上面我们将 Pod 字段作为环境变量的值。 现在我们将用 Container 字段作为环境变量的值。在目录 pods/inject/dapi-envars-container.yaml 中创建一个包含容器的 Pod 的配置文件:


spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox:1.24
      command: [ "sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_CPU_REQUEST MY_CPU_LIMIT;
          printenv MY_MEM_REQUEST MY_MEM_LIMIT;
          sleep 10;
        done;
      resources:
        requests:
          memory: "32Mi"
          cpu: "125m"
        limits:
          memory: "64Mi"
          cpu: "250m"
      env:
        - name: MY_CPU_REQUEST
          valueFrom:
            resourceFieldRef:
              containerName: test-container
              resource: requests.cpu
        - name: MY_CPU_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: test-container
              resource: limits.cpu
        - name: MY_MEM_REQUEST
          valueFrom:
            resourceFieldRef:
              containerName: test-container
              resource: requests.memory
        - name: MY_MEM_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: test-container
              resource: limits.memory
  restartPolicy: Never

这个配置文件中,你可以看到四个环境变量。env 字段是一个 EnvVars. 对象的数组。数组中第一个元素指定 MY_CPU_REQUEST 这个环境变量从 Container 的 requests.cpu 字段获取变量值。同样,其它环境变量也是从 Container 的字段获取它们的变量值。

  1. 创建Pod:
$ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-container.yaml

  1. 验证 Pod 中的容器运行正常:
$ kubectl get pods

  1. 查看容器日志:
$ kubectl logs dapi-envars-resourcefieldref

  1. 输出信息显示了所选择的环境变量的值:

1
1
33554432
67108864

二、通过文件将 Pod 信息呈现给容器

前面我们学习了使用环境变量的方式,现在我们学习通过文件的方式。
在目录 pods/inject/dapi-volume.yaml 中创建一个包含一个容器的 Pod,并将 Pod 级别的字段作为文件映射到正在运行的容器中。 Pod 的清单如下:

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: k8s.gcr.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

在 Pod 清单中,你可以看到 Pod 有一个 downwardAPI 类型的卷,并且挂载到容器中的 /etc/podinfo 目录。

查看 downwardAPI 下面的 items 数组。 数组的每个元素定义一个 downwardAPI 卷。 第一个元素指示 Pod 的 metadata.labels 字段的值保存在名为 labels 的文件中。 第二个元素指示 Pod 的 annotations 字段的值保存在名为 annotations 的文件中。

  1. 创建 Pod:
$ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume.yaml

  1. 验证Pod中的容器运行正常:
$ kubectl get pods

  1. 查看容器的日志:
$ kubectl logs kubernetes-downwardapi-volume-example

  1. 输出显示 labels 和 annotations 文件的内容:

cluster=“test-cluster1”
rack=“rack-22”
zone=“us-est-coast”

build=“two”
builder=“john-doe”

  1. 进入 Pod 中运行的容器,打开一个 Shell:
$ kubectl exec -it kubernetes-downwardapi-volume-example -- sh

  1. 在该 Shell中,查看 labels 文件:

/# cat /etc/podinfo/labels

  1. 输出显示 Pod 的所有标签都已写入 labels 文件。

cluster=“test-cluster1”
rack=“rack-22”
zone=“us-est-coast”

  1. 同样,查看 annotations 文件:

/# cat /etc/podinfo/annotations

  1. 查看 /etc/podinfo 目录下的文件:

/# ls -laR /etc/podinfo

在输出中可以看到,labels 和 annotations 文件都在一个临时子目录中。 在这个例子,…2982_06_02_21_47_53.299460680。 在 /etc/podinfo 目录中,…data 是一个指向临时子目录 的符号链接。/etc/podinfo 目录中,labels 和 annotations 也是符号链接。

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

用符号链接可实现元数据的动态原子性刷新;更新将写入一个新的临时目录, 然后通过使用 rename(2) 完成 …data 符号链接的原子性更新。

  1. 退出 Shell:

/# exit

2.1、存储容器字段

使用 downward API 使 Pod 级别的字段可以被 Pod 内正在运行的容器访问。 接下来我们将只传递由 Pod 定义的部分的字段到 Pod 内正在运行的容器中,但这些字段取自特定容器而不是整个 Pod。 下面是目录 pods/inject/dapi-volume-resources.yaml 中一个同样只有一个容器的 Pod 的清单:

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example-2
spec:
  containers:
    - name: client-container
      image: k8s.gcr.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


在这个清单中,你可以看到 Pod 有一个 downwardAPI 卷, 并且这个会挂载到 Pod 内的单个容器的 /etc/podinfo 目录。

查看 downwardAPI 下面的 items 数组。 数组的每个元素定义一个 downwardAPI 卷。

第一个元素指定在名为 client-container 的容器中, 以 1m 所指定格式的 limits.cpu 字段的值应推送到名为 cpu_limit 的文件中。 divisor 字段是可选的,默认值为 1,1 的除数表示 CPU 资源的核心或内存资源的字节。

  1. 创建Pod:
$ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume-resources.yaml

  1. 打开一个 Shell,进入 Pod 中运行的容器:
$ kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh

  1. 在 Shell 中,查看 cpu_limit 文件:
# 在容器内的 Shell 中运行

$ cat /etc/podinfo/cpu_limit