目錄
Pod 中 init 容器
1 init 容器特點(diǎn)
2 使用 init 容器
Pod 中 init 容器
Init 容器是一種特殊容器,在Pod 內(nèi)的應(yīng)用容器啟動(dòng)之前運(yùn)行。Init 容器可以包括一些應(yīng)用鏡像中不存在的實(shí)用工具和安裝腳本。
1 init 容器特點(diǎn)
init 容器與普通的容器非常像,除了如下幾點(diǎn):
-
它們總是運(yùn)行到完成。如果 Pod 的 Init 容器失敗,kubelet 會(huì)不斷地重啟該 Init 容器直到該容器成功為止。 然而,如果 Pod 對(duì)應(yīng)的
restartPolicy
值為 "Never",并且 Pod 的 Init 容器失敗, 則 Kubernetes 會(huì)將整個(gè) Pod 狀態(tài)設(shè)置為失敗。 -
每個(gè)都必須在下一個(gè)啟動(dòng)之前成功完成。
-
同時(shí) Init 容器不支持
lifecycle
、livenessProbe
、readinessProbe
和startupProbe
, 因?yàn)樗鼈儽仨氃?Pod 就緒之前運(yùn)行完成。 -
如果為一個(gè) Pod 指定了多個(gè) Init 容器,這些容器會(huì)按順序逐個(gè)運(yùn)行。 每個(gè) Init 容器必須運(yùn)行成功,下一個(gè)才能夠運(yùn)行。當(dāng)所有的 Init 容器運(yùn)行完成時(shí), Kubernetes 才會(huì)為 Pod 初始化應(yīng)用容器并像平常一樣運(yùn)行。
-
Init 容器支持應(yīng)用容器的全部字段和特性,包括資源限制、數(shù)據(jù)卷和安全設(shè)置。 然而,Init 容器對(duì)資源請(qǐng)求和限制的處理稍有不同。
2 使用 init 容器
官網(wǎng)地址: Init 容器 | Kubernetes
在 Pod 的規(guī)約中與用來(lái)描述應(yīng)用容器的 containers
數(shù)組平行的位置指定 Init 容器。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-632855.html
apiVersion: v1
kind: Pod
metadata:
name: init-demo
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', 'echo init-myservice is running! && sleep 5']
- name: init-mydb
? image: busybox:1.28
? command: ['sh', '-c', 'echo init-mydb is running! && sleep 10']
-
查看啟動(dòng)詳細(xì)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-632855.html
$ kubectl describe pod init-demo
?
# 部分結(jié)果
Events:
Type ? Reason ? ? Age ? From ? ? ? ? ? ? ? Message
?---- ? ?------ ? ? ---- ? ---- ? ? ? ? ? ? ? -------
Normal Scheduled 2m16s default-scheduler Successfully assigned default/init-demo to k8s-node2
Normal Pulling ? 2m16s kubelet ? ? ? ? ? Pulling image "busybox:1.28"
Normal Pulled ? ? 118s ? kubelet ? ? ? ? ? Successfully pulled image "busybox:1.28" in 17.370617268s (17.370620685s including waiting)
Normal Created ? 118s ? kubelet ? ? ? ? ? Created container init-myservice
Normal Started ? 118s ? kubelet ? ? ? ? ? Started container init-myservice
Normal Pulled ? ? 112s ? kubelet ? ? ? ? ? Container image "busybox:1.28" already present on machine
Normal Created ? 112s ? kubelet ? ? ? ? ? Created container init-mydb
Normal Started ? 112s ? kubelet ? ? ? ? ? Started container init-mydb
Normal Pulled ? ? 101s ? kubelet ? ? ? ? ? Container image "busybox:1.28" already present on machine
Normal Created ? 101s ? kubelet ? ? ? ? ? Created container myapp-container
Normal Started ? 101s ? kubelet ? ? ? ? ? Started container myapp-container
到了這里,關(guān)于【云原生】kubernetes在Pod中init容器的作用和使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!