当前位置:网站首页>Pod environment variables and initContainer
Pod environment variables and initContainer
2022-08-01 07:59:00 【GottdesKrieges】
Pod环境变量和initContainer
Pod环境变量
PodThe environment variable basically has the following several kinds of application scenarios:
- Container application forPod信息;
- Container application by user-defined variables changing the default behavior.
PodEnvironment variables can be defined in the following way:
- 自定义变量值;
- 变量值从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
Attribute manually define variables、从PodAttributes for a variable's value:
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 #从PodAttributes for a variable's value
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
The variable values for the digital quotes,变为字符串:
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)Only used for the samePodRun the business container before initialization,执行完就结束.
- Prior to the application container to perform;
- 支持大部分应用容器配置,但不支持健康检查(lifecycle/livenessProbe/readinessProbe/startupProbe).
主要应用场景包括:
- 环境检查:Such as to ensure that applications rely on the container service start before you start the application container;
- 初始化配置:For example for the application configuration file.
测试
定义一个Pod,There are two initialization container,一个等待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
Did not create,init-myservice
一直没有Ready,所以init-mydb
一直在等待init-myservice
就绪,The application container initialization has been waiting for two containers ready.
Next, we create the corresponding service:
---
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
To view your application status again:
[[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
总结
PodWill this a few kinds are there in the container:
- 基础容器(Infrastructure Container):维护整个Pod网络空间;
- 初始化容器(initContainer):Before business container serial execution;
- 业务容器(Container) :并行启动(SidecarContainers are separated from business container auxiliary container).
边栏推荐
- nodetype中值1、2、3分别代表什么意思
- pytest接口自动化测试框架 | parametrize中ids的用法
- 研发过程中的文档管理与工具
- Mysql数据库的部署以及初始化步骤
- 网络基础学习
- 【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?
- pytest interface automation testing framework | pass in parameter values in the form of function return values
- 最小生成树
- app 自动化 通过工具查看app 元素 (三)
- 并发编程13-JUC之CountDownLatch
猜你喜欢
华为深度学习课程第六、七章
nodetype中值1、2、3分别代表什么意思
HoloView--live data
Electromagnetic compatibility introductory tutorial (6) test project
Vim三种模式
聊一聊ICMP协议以及ping的过程
Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
leetcode-6133:分组的最大数量
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
Golang:go获取url和表单属性值
随机推荐
zip package all files in the directory (including hidden files/folders)
三维坐标系距离
SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
LeetCode240+312+394
mysql查看cpu使用情况
app 自动化 打开app (二)
POJ2421道路建设题解
nodetype中值1、2、3分别代表什么意思
华为深度学习课程第九章——卷积神经网络以及案例实践
Generate pictures based on the content of the specified area and share them with a summary
【MySQL】操作表DML相关语句
Golang:go连接和使用mysql
codeforces每日5题(均1600)-第二十七天
关于App不同方式更新的测试点归纳
Electromagnetic compatibility introductory tutorial (6) test project
数据分析5
扁平数组转树结构实现方式
leetcode-6133:分组的最大数量
POJ2031空间站题解
JVM内存模型之深究模型特征