kubernetes pod yaml 设置为 env var pod ip 和端口

tos*_*osi 0 kubernetes

我有一个情况,我想将 pod 的 IP 地址设置为容器环境变量以及端口的一部分(例如,可以将其硬编码为 default8080)

类似于下面的内容,但我也需要附加端口作为其中的一部分。这样 APPLICATION_SERVER 的结果将是111。.000:8080我猜是这样的。

        - name: APPLICATION_SERVER
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
Run Code Online (Sandbox Code Playgroud)

str*_*gjz 5

该信息可在Downward API中找到

\n\n

您正在查找的值可以作为环境变量从 Downward API 中获取

\n\n
    \n
  • status.podIP - pod\xe2\x80\x99s IP 地址
  • \n
  • spec.serviceAccountName -\npod\xe2\x80\x99s 服务帐户名称
  • \n
  • spec.nodeName - 节点\xe2\x80\x99s 名称
  • \n
  • status.hostIP - 节点\xe2\x80\x99s IP
  • \n
\n\n

Podspec

\n\n
apiVersion: v1\nkind: Pod\nmetadata:\n  name: dapi-envars\nspec:\n  containers:\n    - name: test-container\n      image: k8s.gcr.io/busybox\n      command: [ "sh", "-c"]\n      args:\n      - while true; do\n          echo -en \'\\n\';\n          echo "Application Server $(APPLICATION_SERVER)";\n          sleep 10;\n        done;\n      env:\n        - name: MY_POD_IP\n          valueFrom:\n            fieldRef:\n              fieldPath: status.podIP\n        - name: APPLICATION_SERVER\n          value: "$(MY_POD_IP):8080"\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出

\n\n
kubectl logs dapi-envars\n\nApplication Server 10.244.0.7:8080\n\nApplication Server 10.244.0.7:8080\n\nApplication Server 10.244.0.7:8080\n\nApplication Server 10.244.0.7:8080\n\nApplication Server 10.244.0.7:8080\n
Run Code Online (Sandbox Code Playgroud)\n\n

https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

\n