作者 | 江小南
来源 | 江小南和他的小伙伴们
引言
前两天,公司有个新同事愁眉苦脸,看起来心事重重,我去问他怎么回事,原来是有个需求犯了难:一次部署起四个pod,每个pod名称还不一样,怎么判断是哪个pod产生的日志呢?听到这,我微微一笑,给他讲解了Dowanward API的妙用,他一听,紧缩的眉头缓缓舒展开来,眼看着他将日志名称写成$MY_POD_NAME.log
,我轻轻点头表示满意。答应的周末请客吃饭把我也乐了。今天就将Dowanward API的功能介绍给大家。
总体思路
通过配置环境变量或者挂载Dowanward API卷的形式来暴露pod的元数据,使pod能够获得相应的信息。
环境准备
准备三台服务器搭建kubernetes集群。
节点名称 | IP |
---|---|
k8s-master | 172.31.0.2 |
k8s-worker1 | 172.31.0.3 |
k8s-worker2 | 172.31.0.4 |
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 5d4h v1.20.9
k8s-worker1 Ready <none> 5d4h v1.20.9
k8s-worker2 Ready <none> 5d4h v1.20.9
[root@k8s-master ~]#
准备yaml来部署应用。
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploynamespace: defaultlabels:app: nginx-deploy
spec:selector:matchLabels:app: nginx-deployreplicas: 4template:metadata:labels:app: nginx-deployspec:containers:- name: mynginximage: nginximagePullPolicy: IfNotPresentcommand: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
测试
通过上面deployment.yaml部署的应用是不能获取到pod名称的,但是使用Dowanward API就可以轻松搞定。有两种方式供大家参考。
一、配置环境变量
将上面的yaml进行改造。
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploynamespace: defaultlabels:app: nginx-deploy
spec:selector:matchLabels:app: nginx-deployreplicas: 4template:metadata:labels:app: nginx-deployspec:containers:- name: mynginximage: nginximagePullPolicy: IfNotPresentcommand: [ "/bin/bash", "-ce", "tail -f /dev/null" ]env:- name: MY_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: MY_POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: LIMITS_MEMORYvalueFrom:resourceFieldRef:resource: limits.memory
我们添加了环境变量env
,分别取pod名称和ip。
[root@k8s-master test]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy created
[root@k8s-master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-6f4c989cff-2mcm5 1/1 Running 0 55s
nginx-deploy-6f4c989cff-mjx9n 1/1 Running 0 55s
nginx-deploy-6f4c989cff-vhncx 1/1 Running 0 56s
nginx-deploy-6f4c989cff-wtjzd 1/1 Running 0 57s
[root@k8s-master test]#
通过改造的yaml成功部署了4个pod,进入到容器内部一探究竟。
[root@k8s-master test]# kubectl exec -it nginx-deploy-6f4c989cff-2mcm5 -c mynginx -- /bin/bash
root@nginx-deploy-6f4c989cff-2mcm5:/# echo $MY_POD_NAME
nginx-deploy-6f4c989cff-2mcm5
root@nginx-deploy-6f4c989cff-2mcm5:/# echo $MY_POD_IP
192.168.126.42
root@nginx-deploy-6f4c989cff-2mcm5:/# echo $HOST_IP
172.31.0.4
root@nginx-deploy-6f4c989cff-2mcm5:/#
成功获取到了pod名称和ip,而且宿主机的ip也能获取到,在编写代码的时候就可以使用$MY_POD_NAME
来获取到相应的值了,非常灵活方便。
二、挂载Dowanward API卷
还是将我们初始的deployment.yaml进行改造。
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploynamespace: defaultlabels:app: nginx-deploy
spec:selector:matchLabels:app: nginx-deployreplicas: 4template:metadata:labels:app: nginx-deployspec:containers:- image: nginximagePullPolicy: IfNotPresentcommand: [ "/bin/bash", "-ce", "tail -f /dev/null" ]name: mynginxvolumeMounts:- name: podinfomountPath: /etc/podinforeadOnly: falsevolumes:- name: podinfodownwardAPI:items:- path: namefieldRef:fieldPath: metadata.name- path: namespacefieldRef:fieldPath: metadata.namespace
[root@k8s-master test]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy configured
[root@k8s-master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-d5fc6886d-c7fvh 1/1 Running 0 4m8s
nginx-deploy-d5fc6886d-wgzvs 1/1 Running 0 4m6s
nginx-deploy-d5fc6886d-xc699 1/1 Running 0 4m6s
nginx-deploy-d5fc6886d-xvgj7 1/1 Running 0 4m8s
[root@k8s-master test]#
同样创建了4个pod,我们进入到容器内部。
[root@k8s-master test]# kubectl exec -it nginx-deploy-d5fc6886d-c7fvh -c mynginx -- /bin/bash
root@nginx-deploy-d5fc6886d-c7fvh:/# cd /etc/podinfo/
root@nginx-deploy-d5fc6886d-c7fvh:/etc/podinfo# ls
name namespace
root@nginx-deploy-d5fc6886d-c7fvh:/etc/podinfo# cat name
nginx-deploy-d5fc6886d-c7fvh
root@nginx-deploy-d5fc6886d-c7fvh:/etc/podinfo# cat namespace
default
root@nginx-deploy-d5fc6886d-c7fvh:/etc/podinfo#
同样获取到了pod名称,但是这种方式不是环境变量,在编写代码时使用到的话需要做一些处理。
推荐使用配置环境变量的形式。
梳理总结
Dowanward API常用的字段如下:
使用fieldRef可以声明使用的字段:
字段 | 含义 |
---|---|
spec.nodeName | 宿主机名称 |
status.hostIP | 宿主机IP |
metadata.name | Pod的名称 |
metadata.namespace | Pod所属的Namespace |
status.podIP | Pod的IP |
spec.serviceAccountName | Pod的Service Account的名称 |
metadata.uid | Pod的UID |
metadata.labels['<KEY>'] | 指定<KEY>的Label值 |
metadata.annotations['<KEY>'] | 指定<KEY>的Annotation值 |
metadata.labels | Pod的所有Label |
metadata.annotations | Pod的所有Annotation |
使用resourceFieldRef可以声明使用的字段:
字段 | 含义 |
---|---|
limits.cpu | 容器的CPU limit |
requests.cpu | 容器的CPU request |
limits.memory | 容器的memory limit |
requests.memory | 容器的memory request |
往期推荐
Docker 那些事儿:如何安全地停止、删除容器?
使用 nginx 轻松管理 kubernetes 资源文件
Redis 内存满了怎么办?这样置才正确!
实战 Kubectl 创建 Deployment 部署应用
点分享
点收藏
点点赞
点在看