在容器内获取Pod的信息
每个Pod被创建出来之后,都会被系统分配唯一的名字、IP地址,并且处于某个Namespace中,而使用Downward API就能在Pod的容器内获取Pod的这些重要信息。
apiVersion: v1 kind: Pod metadata: name: dapi-get-info spec: containers: - name: test-get-info image: registry.cn-hangzhou.aliyuncs.com/google_containers/busybox command: ['/bin/sh','-c','env'] env: - name: MY_NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName - name: MY_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: MY_POD_IP valueFrom: fieldRef: fieldPath: status.podIP
查看env信息
KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_SERVICE_PORT=443 HOSTNAME=dapi-get-info SHLVL=1 MYWEB_SERVICE_HOST=10.111.193.215 MYWEB_PORT_8080_TCP_ADDR=10.111.193.215 HOME=/root MYWEB_PORT_8080_TCP_PORT=8080 MYWEB_PORT_8080_TCP_PROTO=tcp MYWEB_PORT=tcp://10.111.193.215:8080 MYWEB_SERVICE_PORT=8080 MYWEB_PORT_8080_TCP=tcp://10.111.193.215:8080 MY_POD_IP=10.38.0.5 MYSQL_PORT_3306_TCP_ADDR=10.109.55.96 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin KUBERNETES_PORT_443_TCP_PORT=443 MYSQL_PORT_3306_TCP_PORT=3306 KUBERNETES_PORT_443_TCP_PROTO=tcp MYSQL_SERVICE_HOST=10.109.55.96 MYSQL_PORT_3306_TCP_PROTO=tcp MY_NODE_NAME=node1 MYSQL_SERVICE_PORT=3306 MYSQL_PORT=tcp://10.109.55.96:3306 MYSQL_PORT_3306_TCP=tcp://10.109.55.96:3306 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 PWD=/ KUBERNETES_SERVICE_HOST=10.96.0.1 MY_POD_NAME=dapi-get-info