原創(chuàng)文檔編寫不易,未經許可請勿轉載。文檔中有疑問的可以郵件聯(lián)系我。 郵箱:yinwanit@163.com
描述
Pod在k8s中歸屬apiVersion版本為v1。在編寫yaml文件中apiVersion應該設置為v1。kind才能設置成Pod。
編寫Pod的yaml文件時可以參考?kubectl explain --api-version=v1? pod. 一級一級查看具體的配置項。
apiVersion和kind對應關系參考:https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/workload-resources/。
在通過yaml文件創(chuàng)建Pod時最好通過kubectl run 命令導出一個yaml文件。
本文對于Pod常用yaml配置進行演示,主要有勾子進程、健康性檢查、初始化容器、Pod節(jié)點選擇、node污點及容忍性。
模板文件導出命令:?kubectl run test_name_pod --image=nginx? --image-pull-policy=IfNotPresent --restart=Always --namespace=kube-system --labels=aa=12? --dry-run=client -o yaml > temp_pod.yaml
一、Pod生命周期勾子
勾子程序用在在pod開啟關閉時通知應用級聯(lián)操作,用在容器開啟關閉前對容器進行自定義的命令或http端口拉取訪問。
勾子程序有兩個hook,一個為PostStart、PreStop,如果PreStop或者PostStart失敗的話, 容器會停止。
- PostStart:在容器運行前執(zhí)行操作,會阻塞Pod進入run的狀態(tài),如果PostStart hook未執(zhí)行成功,Pod將無法進行running狀態(tài)。
- PreStop:應用在容器停止前,會阻塞容器刪除,但是過了terminationGracePeriodSeconds時間后,容器會被強制刪除。
配置參考:kubectl explain? pod.spec.containers.lifecycle
1.1 exec實例文件


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test1-pod name: test1-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test1-pod resources: {} lifecycle: postStart: exec: command: ["/bin/bash", "-c", "touch /root/22"] preStop: exec: command: - cat - /root/22 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
1.2?httpGet實例文件


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test2-pod name: test2-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test2-pod resources: {} lifecycle: postStart: httpGet: host: 10.244.190.70 path: / port: 80 scheme: HTTP dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數詳解:
- host: 需要請求的主機地址
- path: web網頁的路徑
- port:對端web服務的端口
- scheme:連接主機的協(xié)議,默認HTTP
二、健康性檢查
每個容器都會執(zhí)行一個進程此進程由CMD和ENTRYPOINT指定,如果進程退出時返回碼非零,則表示該容器發(fā)生故障,k8s會根據yaml文件中定義的restrtPolicy規(guī)則決定是否重啟容器。
有時進程未退出則不會反會值,但是程序處于異常狀態(tài),此時需要額外的檢測手段對容器進行檢查,使用Liveness探測來確定容器中運行的程序能夠提供正常的服務。
在容器啟動完成過后但是容器中的應用不一定啟動完成,對于對外服務的容器,需要使用Readiness決定容器什么時候可以加入到service中。
LIveness配置參考:kubectl explain? pod.spec.containers.livenessProbe
Readiness配置參考:kubectl explain? pod.spec.containers.readinessProbe
2.1 Liveness探測
Liveness探測讓用戶可以自定義判斷容器是否健康,如果探測失敗則重啟容器。
2.1.1 exec實例文件
自定參數進行檢測。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test3-pod name: test3-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test3-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" livenessProbe: exec: command: - cat - /root/22 periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數詳解:
- periodSeconds: 每隔多少秒檢測一次,默認10秒,最小值1秒。
- initialDelaySeconds: 容器啟動過后多少秒開始檢測,默認0秒。
- failureThreshold: 檢測失敗多少次確定程序異常,默認3次,最小1次。
- successThreshold: 檢測成功多少次確定程序正常,必須為1,默認為1。
- timeoutSeconds: 檢測超時時間默認1秒。
2.1.2 httpGet實例文件
用作http網頁服務檢測


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test4-pod name: test4-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test4-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" livenessProbe: httpGet: host: 10.244.190.70 path: / port: 80 scheme: HTTP periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數解釋:
- host: 需要請求的主機地址
- path: web網頁的路徑
- port:對端web服務的端口
- scheme:連接主機的協(xié)議,默認HTTP
2.1.3??tcpSocket實例文件
用作tcp端口檢測。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test5-pod name: test5-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test5-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" livenessProbe: tcpSocket: host: 10.244.190.70 port: 80 periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數解釋:
- host: 需要請求的主機地址
- port:對端監(jiān)聽的TCP端口號
2.2 Readiness探測
Readiness可以檢測容器什么時候滿足對外提供服務的能力,即什么時候可以加入到service負載池中。Readiness探測語法和Liveness語法完全一致。?
2.2.1 exec實例文件
自定義參數檢查。


cat pod-readinessProve.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test8-pod name: test8-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test8-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" readinessProbe: exec: command: - cat - /root/22 periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數詳解:
- periodSeconds: 每隔多少秒檢測一次,默認10秒,最小值1秒。
- initialDelaySeconds: 容器啟動過后多少秒開始檢測,默認0秒。
- failureThreshold: 檢測失敗多少次確定程序異常,默認3次,最小1次。
- successThreshold: 檢測成功多少次確定程序正常,必須為1,默認為1。
- timeoutSeconds: 檢測超時時間默認1秒。
2.2.2 httpGet實例文件
用作http網頁服務檢測。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test6-pod name: test6-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test6-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" readinessProbe: httpGet: host: 10.244.190.70 path: / port: 80 scheme: HTTP periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數解釋:
- host: 需要請求的主機地址
- path: web網頁的路徑
- port:對端web服務的端口
- scheme:連接主機的協(xié)議,默認HTTP
2.2.3?tcpSocket實例文件
用作tcp端口檢測。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test7-pod name: test7-pod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: test7-pod resources: {} lifecycle: postStart: exec: command: - sleep - "20" readinessProbe: tcpSocket: host: 10.244.190.70 port: 80 periodSeconds: 11 initialDelaySeconds: 10 failureThreshold: 5 successThreshold: 1 timeoutSeconds: 3 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數解釋:
- host: 需要請求的主機地址
- port:對端監(jiān)聽的TCP端口號
二、初始化容器
在k8s中對于一些容器,在運行時必須滿足一定的條件,但是又不能直接在容器中執(zhí)行命令使環(huán)境到達滿足容器運行的要求,因為在容器中執(zhí)行命令可能會導致鏡像里面的CMD和ENTRYPOINT指定命令發(fā)生改變從而導致容器運行不起來。
為了當前環(huán)境能夠滿足正式容器運行的條件,創(chuàng)建一個初始化容器對環(huán)境進行一個設置,待初始化容器對環(huán)境設置完成過后再運行主程序容器。
如果為一個 Pod 指定了多個 Init 容器,Init容器會按順序一次運行一個。 每個 Init 容器必須運行成功才能夠運行一個Init容器,所有Init容器運行完成才能運行Pod中的主程序容器。
Pod中所有容器的名稱必須唯一包含Init容器和主程序容器,如果同一個Pod中有容器名稱不唯一則會報錯。
初始化容器一般用來對宿主機節(jié)點的配置進行修改。
配置參考:kubectl explain? pod.spec.initContainers
?2.1 使用Init預處理數據卷
創(chuàng)建兩個卷datadir、datadir2。datadir使用指定宿主機中/data_pod為路徑,datadir2隨機在宿主機上生成一個文件夾作為存儲數據路徑。在初始化容器中掛在datadir卷,并在卷中創(chuàng)建一個文件,最后在主程序容器中掛在datadir卷。
emptyDir對于容器來說是持久卷,對于Pod來說不是持久卷,該方式創(chuàng)建的卷會跟隨Pod刪除而刪除。
hostPath:對于Pod來說是持久卷,不會隨著Pod刪除而刪除。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test9-pod name: test9-pod spec: volumes: - name: datadir hostPath: path: /data_pod - name: datadir2 emptyDir: {} initContainers: - name: init-container-01 image: busybox command: - touch - /pod_data/lvan.sh volumeMounts: - name: datadir mountPath: "/pod_data" containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test9-pod volumeMounts: - mountPath: "/data01" name: datadir - mountPath: "/data/02" name: datadir2 dnsPolicy: ClusterFirst restartPolicy: Always status: {}
2.2 使用Init修改宿主機配置
使用初始化容器對宿主機進行配置更改時首先需要共享需要更改的宿主機的文件夾到初始化容器中,對于宿主機內核相關信息更改,還需在創(chuàng)建容器時使用特權模式創(chuàng)建。
設置了特權模式的容器,可以對主機進行很多操作,比如,要修改宿主機內核參數等。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test10-pod name: test10-pod spec: initContainers: - name: init-container-01 image: busybox command: - touch - /root/lvan.sh securityContext: privileged: true containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test10-pod dnsPolicy: ClusterFirst restartPolicy: Always status: {}
部分參數詳解:
- privileged: 設置容器為特權模式,true
三、pod調度
?創(chuàng)建Pod時可以指定Pod運行的worknode節(jié)點,直接指定運行的worknode將不受worknode上的污點影響。
3.1 nodeName
直接指定Pod運行在哪個宿主機上。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test11-pod name: test11-pod spec: nodeName: node01.lvan containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test11-pod dnsPolicy: ClusterFirst restartPolicy: Always status: {}
3.2?nodeSelector
nodeSelector通過標簽來確定Pod運行在哪個宿主機中。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test12-pod name: test12-pod spec: nodeSelector: kubernetes.io/hostname: node02.lvan containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test12-pod dnsPolicy: ClusterFirst restartPolicy: Always status: {}
四、node污點設置
沒有污點的worknode可以支持可容忍污點的Pod。如果worknode上污點,那么表示不允許pod調度到該節(jié)點,除非pod也被標記為可以容忍污點節(jié)點。
可以使用污點來區(qū)分不同節(jié)點的類型,如環(huán)境中worknode節(jié)點物理配置不一樣,有的磁盤比較好、有的網絡比較好。可以添加污點卻分出每種不同類型的nodework。
使用kubectl taint命令可完成Node節(jié)點污點設置。
設置了污點的Node,會根據設置的策略對Pod進行調度,可以讓新的Pod不調度到該節(jié)點,也可以把現有節(jié)點的上Pod驅逐出該Node。
污點由標簽和作用兩部分組成,格式為: key=value:effect,其中key=value即為污點標簽,部分,effect為該污點的具體作用。污點有三個effect動作。
effect策略解釋:
- PreferNoSchedule:NoSchedule軟策略,盡量不要調度到該節(jié)點上,如果環(huán)境中除了該節(jié)點外其他節(jié)點均不滿足Pod運行,則會調度到該策略的workNode上。
- NoSchedule:設置Pod不調度到該workNode上,Pod設置了可以容忍該污點除外。
- NoExecute:設置workNode不能調度Pod,同時驅逐掉該workNode上已有的Pod。
4.1 節(jié)點污點查看
?使用命令查看node下有哪些污點。
# kubectl describe node node_name | grep -iA1 Taints
如果結果為<none>表示沒有設置污點。
4.2 節(jié)點污點設置
格式:key=value:effect
4.2.1 設置污點
# kubectl taint nodes node_name 標簽名=標簽值:NoSchedule # #設置節(jié)點node01.lvan污點標簽為storage=low-storage,盡量不調度Pod到該節(jié)點。 # kubectl taint nodes node01.lvan storage=low-storage:PreferNoSchedule
4.2.2 去除污點
# kubectl taint node node_name 標簽名=標簽值:PreferNoSchedule-
# kubectl taint node node_name 標簽名=標簽值-
# kubectl taint node node_name 標簽名- # #去除node01.lvan節(jié)點上storage:PreferNoSchedule污點 # kubectl taint node node01.lvan storage:PreferNoSchedule-
五、Pod容忍性
當節(jié)點設置了污點,同時污點的策略還是NoSchedule,Pod則不會調度到該節(jié)點上。如果需要在該workNode上運行Pod,則需要設置Pod能夠對該節(jié)點的上的污點進行容忍。
yaml文件中污點設置參考:kubectl explain --api-version=v1 pod.spec.tolerations.?
operator參數解釋:
- Exists:節(jié)點上存在該標簽的污點就行,不用在乎具體的值和動作。都可以容忍。
- Equal:節(jié)點上存在的污點標簽值要和yaml文件中定義一致,動作可以省略才可以容忍。
5.1 Exists
節(jié)點上只要有yaml文件中對應標簽名的污點即表示該Pod可容忍對應的污點。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test12-pod name: test12-pod spec: tolerations: - key: storage operator: Exists containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test12-pod dnsPolicy: ClusterFirst restartPolicy: Always status: {}
5.2 Equal
需要yaml中定義的容忍污點標簽、值、動作和節(jié)點上的污點一致才可容忍節(jié)點上的污點。


apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: test12-pod name: test12-pod spec: tolerations: - key: storage operator: Equal value: low-storage effect: NoSchedule containers: - image: nginx imagePullPolicy: IfNotPresent env: - name: aa value: "456" - name: bb value: "test_env" name: test12-pod dnsPolicy: ClusterFirst restartPolicy: Always status: {}
六、親和性
k8s中親和性分為節(jié)點親和性和Pod親和性兩種。
- 節(jié)點親和性:設置Pod和node的關系。
- Pod親和性:設置Pod和Pod之間的關系。
節(jié)點親和性和Pod親和性有軟親和性和硬親和性兩種。
- 軟親和性:preferred打頭的為軟親和性
- 硬親和性:required打頭的為硬親和性。
?配置參考kubectl explain? pod.spec.affinity.
6.1 鍵值運算關系
持續(xù)更新2023年8月7日。文章來源:http://www.zghlxwxcb.cn/news/detail-630767.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-630767.html
到了這里,關于k8s學習筆記-3(Pod yaml文件編寫)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!