• Kubernetes之通过环境变量将 Pod 信息呈现给容器


    用Pod字段作为环境变量的值

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-envars-fieldref
    spec:
      containers:
        - name: test-container
          image: k8s.gcr.io/busybox
          command: [ "sh", "-c"]
          args:
          - while true; do
              echo -en '
    ';
              printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
              printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
              sleep 10;
            done;
          env:
            - name: MY_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MY_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: MY_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: MY_POD_SERVICE_ACCOUNT
              valueFrom:
                fieldRef:
                  fieldPath: spec.serviceAccountName
      restartPolicy: Never

    进容器env查看就能看到MY_NODE_NAME、MY_POD_NAMESPACE、MY_POD_IP、MY_POD_SERVICE_ACCOUNT这些环境变量

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

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-envars-resourcefieldref
    spec:
      containers:
        - name: test-container
          image: k8s.gcr.io/busybox:1.24
          command: [ "sh", "-c"]
          args:
          - while true; do
              echo -en '
    ';
              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可以看到MY_CPU_REQUEST、MY_CPU_LIMIT、MY_MEM_REQUEST、MY_MEM_LIMIT这些环境变量

  • 相关阅读:
    win10电脑只有自带浏览器能上网
    Android Studio环境安装
    小白回归
    软件工程个人总结
    学习进度第十六周
    学习进度第十五周
    第二阶段冲刺(第十天)
    《大道至简》阅读笔记03——语言只是种工具
    学习进度第十四周
    第二阶段冲刺(第九天)
  • 原文地址:https://www.cnblogs.com/chadiandianwenrou/p/14062884.html
Copyright © 2020-2023  润新知