https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/
K8S中获取pod相关信息的方式:
1、环境变量
2、volume挂载文件
[root@hanyu-210 ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-pod
spec:
containers:
- name: busybox-container
image: busybox
command: [ "/bin/sh", "-c", "sleep 1000" ]
resources:
requests:
memory: "32Mi"
cpu: "125m"
env:
- name: status.podIP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: spec.nodeName
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: status.hostIP
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: spec.serviceAccountName
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
- name: metadata.name
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: requests.cpu
valueFrom:
resourceFieldRef:
containerName: busybox-container
resource: requests.cpu
- name: limits.cpu
valueFrom:
resourceFieldRef:
containerName: busybox-container
resource: limits.cpu
- name: requests.memory
valueFrom:
resourceFieldRef:
containerName: busybox-container
resource: requests.memory
- name: limits.memory
valueFrom:
resourceFieldRef:
containerName: busybox-container
resource: limits.memory
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations
- path: "cpu_limit"
resourceFieldRef:
containerName: busybox-container
resource: limits.cpu
divisor: 1m
- path: "cpu_request"
resourceFieldRef:
containerName: busybox-container
resource: requests.cpu
divisor: 1m
- path: "mem_limit"
resourceFieldRef:
containerName: busybox-container
resource: limits.memory
divisor: 1Mi
- path: "mem_request"
resourceFieldRef:
containerName: busybox-container
resource: requests.memory
divisor: 1Mi
restartPolicy: Never
kubectl apply -f pod.yaml后待pod Running后