当前位置:网站首页>Understand the wonderful use of dowanward API, and easily grasp kubernetes environment variables
Understand the wonderful use of dowanward API, and easily grasp kubernetes environment variables
2022-07-27 20:31:00 【Silly [email protected]】
introduction
Two days before , There is a new colleague in the company with a sad face , Looks preoccupied , I'll ask him what happened , It turned out that there was a demand that made it difficult : Deploy four at a time pod, Every pod The name is not the same , How to judge which pod The generated logs ? To hear that , I smile , Explained it to him Dowanward API The magic effect of , He listened to , The tight eyebrows slowly stretch out , I watched him write the name of the log as $MY_POD_NAME.log, I nodded my head gently to show my satisfaction . The promised weekend dinner party made me happy . Today will be Dowanward API To introduce the functions of .
General idea
By configuring environment variables or mounting Dowanward API In the form of a volume to expose pod Metadata , send pod Be able to obtain corresponding information .
Environmental preparation
Prepare three servers to build kubernetes colony .

| The name of the node | IP |
|---|---|
| k8s-master | 172.31.0.2 |
| k8s-worker1 | 172.31.0.3 |
| k8s-worker2 | 172.31.0.4 |
[[email protected] ~]# 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
[[email protected] ~]#
Get ready yaml To deploy the application .
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: default
labels:
app: nginx-deploy
spec:
selector:
matchLabels:
app: nginx-deploy
replicas: 4
template:
metadata:
labels:
app: nginx-deploy
spec:
containers:
- name: mynginx
image: nginx
imagePullPolicy: IfNotPresent
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
test
Pass above deployment.yaml Deployed applications cannot be obtained pod Name , But use Dowanward API It can be done easily . There are two ways for your reference .
One 、 Configure environment variables
The above yaml To transform .
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: default
labels:
app: nginx-deploy
spec:
selector:
matchLabels:
app: nginx-deploy
replicas: 4
template:
metadata:
labels:
app: nginx-deploy
spec:
containers:
- name: mynginx
image: nginx
imagePullPolicy: IfNotPresent
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
resource: limits.memory
We added environment variables env, Take... Separately pod Name and ip.
[[email protected] test]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy created
[[email protected] 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
[[email protected] test]#
Transformed yaml Successfully deployed 4 individual pod, We went inside the container to find out .
[[email protected] test]# kubectl exec -it nginx-deploy-6f4c989cff-2mcm5 -c mynginx -- /bin/bash
[email protected]:/# echo $MY_POD_NAME
nginx-deploy-6f4c989cff-2mcm5
[email protected]:/# echo $MY_POD_IP
192.168.126.42
[email protected]:/# echo $HOST_IP
172.31.0.4
[email protected]:/#
Successfully achieved pod Name and ip, And host ip You can also get , It can be used when writing code $MY_POD_NAME To get the corresponding value , Very flexible and convenient .
Two 、 mount Dowanward API volume
Or our initial deployment.yaml To transform .
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
namespace: default
labels:
app: nginx-deploy
spec:
selector:
matchLabels:
app: nginx-deploy
replicas: 4
template:
metadata:
labels:
app: nginx-deploy
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
name: mynginx
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: name
fieldRef:
fieldPath: metadata.name
- path: namespace
fieldRef:
fieldPath: metadata.namespace
[[email protected] test]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy configured
[[email protected] 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
[[email protected] test]#
Also created 4 individual pod, We go inside the container .
[[email protected] test]# kubectl exec -it nginx-deploy-d5fc6886d-c7fvh -c mynginx -- /bin/bash
[email protected]:/# cd /etc/podinfo/
[email protected]:/etc/podinfo# ls
name namespace
[email protected]:/etc/podinfo# cat name
nginx-deploy-d5fc6886d-c7fvh
[email protected]:/etc/podinfo# cat namespace
default
[email protected]:/etc/podinfo#
Also obtained pod name , But this method is not an environment variable , We need to do some processing when writing code .
It is recommended to use the form of configuring environment variables .
Sort out the summary
Dowanward API Common fields are as follows :
Use fieldRef You can declare the fields used :
| full name | Age |
|---|---|
| spec.nodeName | Host name |
| status.hostIP | The host machine IP |
| metadata.name | Pod The name of |
| metadata.namespace | Pod Of Namespace |
| status.podIP | Pod Of IP |
| spec.serviceAccountName | Pod Of Service Account The name of |
| metadata.uid | Pod Of UID |
| metadata.labels['<KEY>'] | Appoint <KEY> Of Label value |
| metadata.annotations['<KEY>'] | Appoint <KEY> Of Annotation value |
| metadata.labels | Pod All of the Label |
| metadata.annotations | Pod All of the Annotation |
Use resourceFieldRef You can declare the fields used :
| full name | Age |
|---|---|
| limits.cpu | Container of CPU limit |
| requests.cpu | Container of CPU request |
| limits.memory | Container of memory limit |
| requests.memory | Container of memory request |
This paper is written by mdnice Multi platform Publishing
版权声明
本文为[Silly [email protected][email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/199/202207151416543160.html
边栏推荐
- 为什么需要第三方支付?
- set--数据解构
- 获得微店商品详情 API
- Assignment 1 - Hello World ! - Simple thread Creation
- 数仓搭建——DWD层
- ES6 deleting attributes of objects_ ES6 delete an element "suggested collection" in the object
- JS array method foreach and map comparison
- shell
- Oracle simple advanced query
- JS jump to the page and refresh (jump to this page)
猜你喜欢

PyQt5快速开发与实战 4.7 QSpinBox(计数器) and 4.8 QSlider(滑动条)

If you want to switch to software testing, you should pass these three tests first, including a 3000 word super full test learning guide

想转行软件测试,先过这三关,包含一份3000字超全测试学习指南

Preprocessing and macro definition

Redis-基本了解,五大基本数据类型

MLX90640 红外热成像仪测温传感器模块开发笔记(七)

Oracle Xe installation and user operation

YY English learning about fish

Mlx90640 infrared thermal imager temperature sensor module development notes (VII)

Unity2d dynamic cartoon script (animation demonstration II for the chapter of Tiger Bridge)
随机推荐
调整数组使奇数全部都位于偶数前
预处理与宏定义
conda常用命令
You can understand it at a glance, eslint
一看就懂的ESLint
OA项目之我的审批(查询&会议签字)
What is a multi-layer perceptron (what is a multi-layer perceptron)
uva1377
Clickhouse implements materializedpostgresql
Redis 事物学习
2019年全球半导体市场收入4183亿美元,同比下滑11.9%
ZJNU 22-07-26 比赛心得
ES6 deleting attributes of objects_ ES6 delete an element "suggested collection" in the object
使用cpolar建立一个商业网站(5)
LG集团宣布将向湖北捐赠300万元现金、120万个口罩、1万套防护服
Why do we need third-party payment?
Wu Hequan: digital technology empowering "double carbon" practice according to local conditions
Cfssl of pki/tls tool -- the road to dream
Technology sharing | how to do Assertion Verification in interface automated testing?
Huawei's mobile phone shipments exceed Apple's, ranking second in the world, but it faces a large amount of inventory that needs to be cleaned up