当前位置:网站首页>Pod环境变量和initContainer
Pod环境变量和initContainer
2022-08-01 07:50:00 【GottdesKrieges】
Pod环境变量和initContainer
Pod环境变量
Pod中的环境变量主要有以下几种应用场景:
- 容器内应用程序获取Pod信息;
- 容器内应用程序通过用户定义的变量改变默认行为。
Pod环境变量可以按照以下方式定义:
- 自定义变量值;
- 变量值从Pod属性获取;
- 变量值从Secret、ConfigMap获取。
测试
生成测试用的yaml文件:
[[email protected] ~]# kubectl run pod1 --image=busybox --dry-run=client -o yaml -- sleep 24
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1
name: pod1
spec:
containers:
- args:
- sleep
- "24"
image: busybox
name: pod1
resources: {
}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {
}
通过env
属性手动定义变量、从Pod属性获取变量值:
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod1
name: pod1
spec:
containers:
- args:
- sleep
- "24h"
image: busybox
name: pod1
env: #自定义变量
- name: testvar1
value: 42
- name: testvar2
value: KillerQueen
- name: THIS_NODE_NAME #从Pod属性获取变量值
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: THIS_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: THIS_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: THIS_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: THIS_POD_SRV_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
部署Pod:
[[email protected] ~]# kubectl apply -f pod1.yaml
Error from server (BadRequest): error when creating "pod1.yaml": Pod in version "v1" cannot be handled as a Pod: json: cannot unmarshal number into Go struct field EnvVar.spec.containers.env.value of type string
将变量值为数字的加上引号,变为字符串:
env: #自定义变量
- name: testvar1
value: "42"
重新部署后,检查环境变量:
[[email protected] ~]# kubectl exec -it pod1 -- sh
/ # echo $testvar2
KillerQueen
/ # echo $THIS_NODE_NAME
k8s-node2
/ # echo $THIS_POD_NAME
pod1
/ # echo $THIS_POD_NAMESPACE
default
/ # echo $THIS_POD_SRV_ACCOUNT
default
initContainer
初始化容器(initContainer)仅用于同一个Pod中的业务容器运行前的初始化工作,执行完就结束。
- 优先于应用容器执行;
- 支持大部分应用容器配置,但不支持健康检查(lifecycle/livenessProbe/readinessProbe/startupProbe)。
主要应用场景包括:
- 环境检查:比如确保应用容器依赖的服务启动后再启动应用容器;
- 初始化配置:比如给应用准备配置文件。
测试
定义一个Pod,其中有两个初始化容器,一个等待myservice
服务,另一个等待mydb
服务。
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
运行Pod:
kubectl apply -f initConainer.yaml
查看Pod创建状态:
[[email protected] ~]# kubectl get -f initContainer.yaml
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 28s
或者
[[email protected] ~]# kubectl describe -f initContainer.yaml
Name: myapp-pod
Namespace: default
Status: Pending
...
Init Containers:
init-myservice:
Container ID: docker://667aa13dc8a9e8a1ecad54d88d0400c3d101e6946c1a41301ba6d9e66858b008
Image: busybox:1.28
...
State: Running
Started: Sun, 31 Jul 2022 04:00:04 -0400
Ready: False
...
init-mydb:
Container ID:
Image: busybox:1.28
Image ID:
...
State: Waiting
Reason: PodInitializing
Ready: False
...
Containers:
myapp-container:
Container ID:
Image: busybox:1.28
Image ID:
...
State: Waiting
Reason: PodInitializing
Ready: False
...
Conditions:
Type Status
Initialized False
Ready False
ContainersReady False
PodScheduled True
Volumes:
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m40s default-scheduler Successfully assigned default/myapp-pod to k8s-node2
Normal Pulling 3m40s kubelet Pulling image "busybox:1.28"
Normal Pulled 3m9s kubelet Successfully pulled image "busybox:1.28" in 30.987078455s
Normal Created 3m9s kubelet Created container init-myservice
Normal Started 3m9s kubelet Started container init-myservice
查看容器日志:
[[email protected] ~]# kubectl logs myapp-pod -c init-myservice
nslookup: can't resolve 'myservice.default.svc.cluster.local'
waiting for myservice
[[email protected] ~]# kubectl logs myapp-pod -c init-mydb
Error from server (BadRequest): container "init-mydb" in pod "myapp-pod" is waiting to start: PodInitializing
由于myservice
和mydb
都未创建,init-myservice
一直没有Ready,所以init-mydb
一直在等待init-myservice
就绪,而应用容器一直在等两个初始化容器就绪。
接下来我们创建对应的服务:
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
暴露服务:
kubectl apply -f init-services.yaml
再次查看应用状态:
[[email protected] ~]# kubectl get -f initContainer.yaml
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 15m
[[email protected] ~]# kubectl describe -f initContainer.yaml
Name: myapp-pod
Namespace: default
...
Status: Running
...
Init Containers:
init-myservice:
Container ID: docker://667aa13dc8a9e8a1ecad54d88d0400c3d101e6946c1a41301ba6d9e66858b008
...
State: Terminated
Reason: Completed
Exit Code: 0
Ready: True
...
init-mydb:
Container ID: docker://be037f56bdf5b2c940de7e0516b9167bf70674005fb64986aaed135c7e94d011
...
State: Terminated
Reason: Completed
Exit Code: 0
Ready: True
...
Containers:
myapp-container:
Container ID: docker://0083db021fda9835ac7113c61f5a48403a91257b35c6866a9dfa84b8de57a46f
...
State: Running
Started: Sun, 31 Jul 2022 04:14:40 -0400
Ready: True
Restart Count: 0
...
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 15m default-scheduler Successfully assigned default/myapp-pod to k8s-node2
Normal Pulling 15m kubelet Pulling image "busybox:1.28"
Normal Pulled 15m kubelet Successfully pulled image "busybox:1.28" in 30.987078455s
Normal Created 15m kubelet Created container init-myservice
Normal Started 15m kubelet Started container init-myservice
Normal Pulled 30s kubelet Container image "busybox:1.28" already present on machine
Normal Created 30s kubelet Created container init-mydb
Normal Started 29s kubelet Started container init-mydb
Normal Pulled 29s kubelet Container image "busybox:1.28" already present on machine
Normal Created 29s kubelet Created container myapp-container
Normal Started 28s kubelet Started container myapp-container
总结
Pod中会有这几类容器:
- 基础容器(Infrastructure Container):维护整个Pod网络空间;
- 初始化容器(initContainer):先于业务容器开始串行执行;
- 业务容器(Container) :并行启动(Sidecar容器是从业务容器中分离出来的辅助容器)。
边栏推荐
- 2022杭电中超杯(1)个人题解
- JSON 与 JS 对象的区别
- [Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
- VSCode 快捷键及通用插件推荐
- 图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
- pytest接口自动化测试框架 | parametrize中ids的用法
- 【MySQL】操作表DML相关语句
- navicat mysql 内存占用过高,被强制关闭
- Shell executes SQL to send emails
- 好的plm软件有哪些?plm软件排行榜
猜你喜欢
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
VSCode插件推荐(Rust环境)
【杭电多校第四场 B题】最短路图+缩点dp
How to generate and configure public key certificate in Alipay
MATLAB program design and application of MATLAB 2.5
Fist game copyright-free music download, League of Legends copyright-free music, can be used for video creation, live broadcast
rhcsa 第三次
云原生FAQ
华为深度学习课程第九章——卷积神经网络以及案例实践
随机推荐
正则表达式符号
巧妙利用unbuffer实时写入
Golang: go open web service
LeetCode240+312+394
05-SDRAM: Arbitration
Data Analysis 5
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
Shell执行SQL发邮件
JVM内存模型之深究模型特征
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
GO error handling
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
USB Protocol (2) Terminology
电磁兼容简明教程(6)测试项目
Delphi MDI appliction 文档最大化显示、去掉最大化最小化等按钮
The use of Golang: go template engine
类似 MS Project 的项目管理工具有哪些
SaaS安全认证综合指南
navicat mysql 内存占用过高,被强制关闭