Python微信订餐小程序课程视频
https://edu.csdn.net/course/detail/36074
Python实战量化交易理财系统
https://edu.csdn.net/course/detail/35475
Kubernetes在创建Pod时,会为Pod和容器设置一些额外的信息,比如Pod名称、Pod IP、Node IP、Label、Annotation、资源限制等,我们经常会在应用程序中使用到这些数据,比如利用Pod名称作为应用日志的字段,方便分析日志。为了能在容器内获取这些信息,我们可以使用Downward API机制来实现。
Downward API可以通过环境变量和Volume挂载这两种方式将Pod信息注入容器,我们分别来看一下:
一、环境变量方式
我们还是以Busybox为例进行演示,我们将Pod信息和Container信息以环境变量方式注入容器,在容器启动后通过env命令打印出来,我们Yaml文件内容如下:
apiVersion: v1
kind: Pod
metadata:
name: busybox-pod
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "env | grep VAR\_"]
resources:
requests:
memory: "16Mi"
cpu: "125m"
limits:
memory: "32Mi"
cpu: "250m"
env:
- name: VAR\_NODE\_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: VAR\_POD\_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: VAR\_POD\_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: VAR\_POD\_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: VAR\_SERVICE\_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
- name: VAR\_CPU\_REQUEST
valueFrom:
resourceFieldRef:
containerName: busybox
resource: requests.cpu
- name: VAR\_CPU\_LIMIT
valueFrom:
resourceFieldRef:
containerName: busybox
resource: limits.cpu
- name: VAR\_MEM\_REQUEST
valueFrom:
resourceFieldRef:
containerName: busybox