当前位置:网站首页>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
边栏推荐
- Assignment 1 - Hello World ! - Simple thread Creation
- C语言pow函数(c语言中指数函数怎么打)
- 使用cpolar建立一个商业网站(5)
- Set -- data deconstruction
- Common methods of object learning [clone and equals]
- MySQL learning record (III) multi table query, sub query, paging query, case statement, single line function
- Redis hash structure command
- It is said that Intel will stop the nervana chip manufactured by TSMC at 16nm
- 图解LeetCode——592. 分数加减运算(难度:中等)
- 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
猜你喜欢

Add joint control to gltf model

Redis queue, RDB learning

I'm also drunk. Eureka delayed registration and this pit

Codeworks 5 questions per day (average 1500) - day 24

Two years after its release, the price increased by $100, and the reverse growth of meta Quest 2

图解LeetCode——剑指 Offer II 115. 重建序列(难度:中等)

发布2年后涨价100美元,Meta Quest 2的逆生长

站在巨人肩膀上学习,京东爆款架构师成长手册首发

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

Use cpolar to build a business website (5)
随机推荐
Preprocessing and macro definition
2019年中国智能机市场:华为拿下近4成份额,稳坐国内第一
多点双向重发布及路由策略的简单应用
Assignment 1 - Hello World ! - Simple thread Creation
C语言pow函数(c语言中指数函数怎么打)
OA项目之我的审批(查询&会议签字)
【Map 集合】
Pyqt5 rapid development and practice 4.7 qspinbox (counter) and 4.8 QSlider (slider)
办公自动化解决方案——DocuWare Cloud 将应用程序和流程迁移到云端的完整的解决方案
How to quickly improve the three minute response rate of Tiktok store? What will affect the reply rate of Tiktok store?
Source code analysis of Chang'an chain data storage
继华为、联发科之后,这家手机芯片厂商宣布向武汉捐款700万
MediaTek releases Helio g80, a mid-range game phone chip
发布2年后涨价100美元,Meta Quest 2的逆生长
JS array method foreach and map comparison
LG集团宣布将向湖北捐赠300万元现金、120万个口罩、1万套防护服
华为手机出货超苹果成全球第二,但面临大量库存需要清理
set--数据解构
My approval of OA project (Query & meeting signature)
Clickhouse 实现 MaterializedPostgreSQL