kubernetes 容器内获取Pod信息(包括:宿主主机IP)
kubernetes 自从1.7开始,可以在pod 的container 内获取pod的spec,metadata 等信息。具体方法可以通过env获取:env:- name: MY_NODE_NAMEvalueFrom:fieldRef:fieldPath: spec.nodeName...
·
全栈工程师开发手册 (作者:栾鹏)
架构系列文章
kubernetes 自从1.7开始,可以在pod 的container 内获取pod的spec,metadata 等信息。
为啥可以获取
容器中进程启动顺序:先调度确定pod部署在哪个机器上,部署pod,部署container,启动进程。
所以在启动进程前,就已经有了pod的信息,所以可以获取,然后通过进程启动前设置环境变量的形式来实现。
可以传递的信息
先随便部署一个pod,通过
kubectl get pod -n your-namespace -o yaml your-app
获取的pod信息为
apiVersion: v1
kind: Pod
metadata:
annotations:
cni.projectcalico.org/podIP: 172.16.162.2/32
creationTimestamp: "2020-03-06T15:11:40Z"
generateName: your-app-57846cfdbf-
labels:
app: your-app
pod-template-hash: 57846cfdbf
name: your-app-57846cfdbf-x6nj6
namespace: your-namespace
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: your-app-57846cfdbf
uid: 8b88c1f5-9cce-49bf-8b7d-7f90c67483af
resourceVersion: "62526266"
selfLink: /api/v1/namespaces/your-namespace/pods/your-app-57846cfdbf-x6nj6
uid: e86bd373-25bb-4a2d-8a18-49ced8809771
spec:
containers:
- command:
- python
- server.py
env:
- name: user
value: aaaaa
- name: password
value: bbbbb
image: xxxxxxxxxxxxxxxxxx
imagePullPolicy: Always
livenessProbe:
failureThreshold: 3
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 100
periodSeconds: 300
successThreshold: 1
timeoutSeconds: 5
name: your-app
ports:
- containerPort: 80
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
resources:
limits:
cpu: "5"
memory: 5000Mi
requests:
cpu: 10m
memory: 100Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/localtime
name: tz-config
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-sq77d
readOnly: true
workingDir: /app/
dnsPolicy: ClusterFirst
enableServiceLinks: true
imagePullSecrets:
- name: hubsecret
nodeName: ch009022000050
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- name: default-token-sq77d
secret:
defaultMode: 420
secretName: default-token-sq77d
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-03-06T15:11:41Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-03-09T02:43:51Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-03-09T02:43:51Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-03-06T15:11:40Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://59b535d6fe210f70b6172a68729d99d7af57489564754a2d97fb224034c2b437
image: xxxxxxxxxxxxxxxxxx
imageID: xxxxxxxxxxx-pipline@sha256:812e9d09f8fe8577b5f9c6aa212194c624eea5c7111261d9a8503026a29e6da7
lastState:
terminated:
containerID: docker://a70c979c114ca0131ffeac04e0016c3a0dea9d266f98042e333e9048f3cfa92c
exitCode: 1
finishedAt: "2020-03-09T02:43:15Z"
reason: OOMKilled
startedAt: "2020-03-09T02:40:11Z"
name: your-app
ready: true
restartCount: 2
state:
running:
startedAt: "2020-03-09T02:43:30Z"
hostIP: 9.22.0.50
phase: Running
podIP: 172.16.162.2
qosClass: Burstable
startTime: "2020-03-06T15:11:41Z"
这上面的信息都可以获取。并传给自定义环境变量
获取pod信息,传递给环境变量
具体方法可以通过env获取:
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: MY_POD_SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
spec.nodeName : pod所在节点的IP、宿主主机IP
status.podIP :pod IP
metadata.namespace : pod 所在的namespace
注意:此示例中的字段是Pod字段。它们不是Pod中“容器”的字段。
更多参数:https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
更多推荐
已为社区贡献24条内容
所有评论(0)