14.3. 使用 Downward API 消耗巨页资源
您可以使用 Downward API 注入容器消耗的巨页资源的信息。
您可以将资源分配作为环境变量、卷插件或两者都注入。您在容器中开发和运行的应用可以通过读取指定卷中的环境变量或文件来确定可用的资源。
流程
创建一个类似以下示例的
hugepages-volume-pod.yaml
文件:apiVersion: v1 kind: Pod metadata: generateName: hugepages-volume- labels: app: hugepages-example spec: containers: - securityContext: capabilities: add: [ "IPC_LOCK" ] image: rhel7:latest command: - sleep - inf name: example volumeMounts: - mountPath: /dev/hugepages name: hugepage - mountPath: /etc/podinfo name: podinfo resources: limits: hugepages-1Gi: 2Gi memory: "1Gi" cpu: "1" requests: hugepages-1Gi: 2Gi env: - name: REQUESTS_HUGEPAGES_1GI <.> valueFrom: resourceFieldRef: containerName: example resource: requests.hugepages-1Gi volumes: - name: hugepage emptyDir: medium: HugePages - name: podinfo downwardAPI: items: - path: "hugepages_1G_request" <.> resourceFieldRef: containerName: example resource: requests.hugepages-1Gi divisor: 1Gi
<.> 指定从
requests.hugepages-1Gi
读取资源使用,并将值公开为REQUESTS_HUGEPAGES_1GI
环境变量。< .> 指定从requests.hugepages-1Gi
读取资源使用,并将值公开为文件/etc/podinfo/hugepages_1G_request
。从
hugepages-volume-pod.yaml
文件创建 pod:$ oc create -f hugepages-volume-pod.yaml
验证
检查
REQUESTS_HUGEPAGES_1GI
环境变量的值:$ oc exec -it $(oc get pods -l app=hugepages-example -o jsonpath='{.items[0].metadata.name}') \ -- env | grep REQUESTS_HUGEPAGES_1GI
输出示例
REQUESTS_HUGEPAGES_1GI=2147483648
检查
/etc/podinfo/hugepages_1G_request
文件的值:$ oc exec -it $(oc get pods -l app=hugepages-example -o jsonpath='{.items[0].metadata.name}') \ -- cat /etc/podinfo/hugepages_1G_request
输出示例
2