当前位置:网站首页>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
边栏推荐
- DP (dynamic programming)
- PyQt5快速开发与实战 4.7 QSpinBox(计数器) and 4.8 QSlider(滑动条)
- 内置函数时间日期函数
- Redis queue, RDB learning
- [RCTF2015]EasySQL-1|SQL注入
- 京东:按关键字搜索商品 API
- set--数据解构
- Pytorch multiplication and broadcasting mechanism
- Following Huawei and MediaTek, the mobile phone chip manufacturer announced a donation of 7million yuan to Wuhan
- Oracle Xe installation and user operation
猜你喜欢

C语言--数组

Use cpolar to build a business website (5)

'vite' is not an internal or external command, nor is it a runnable program or batch file

PyQt5快速开发与实战 4.3 QLabel and 4.4 文本框类控件

JS实现视频录制-以Cesium为例

《安富莱嵌入式周报》第275期:2022.07.18--2022.07.24

Redis 事物学习

Check the internship salary of Internet companies: with it, you can also enter the factory

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

站在巨人肩膀上学习,京东爆款架构师成长手册首发
随机推荐
What is a multi-layer perceptron (what is a multi-layer perceptron)
西数移动硬盘无法读取(高枕无忧的成语)
为什么需要第三方支付?
JS array method foreach and map comparison
Redis thing learning
京东:按关键字搜索商品 API
Redis 事物学习
Office automation solution - docuware cloud is a complete solution to migrate applications and processes to the cloud
Get wechat product details API
Can software testing be learned in 2022? Don't learn, software testing positions are saturated
传英特尔将停掉台积电16nm代工的Nervana芯片
Why do we need third-party payment?
JD: get the raw data API of commodity details
Slf4j introduction
康佳首批10万颗存储主控芯片售罄,2020年预计销量1亿颗
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
Clickhouse 实现 MaterializedPostgreSQL
ES6--解构赋值
To share the denoising methods and skills of redshift renderer, you must have a look
Under the epidemic, I left my job for a year, and my income increased 10 times