關(guān)于作者 劉海平(HappyLau )云計算高級顧問 目前在騰訊云從事公有云相關(guān)工作,曾就職于酷狗,EasyStack,擁有多年公有云+私有云計算架構(gòu)設(shè)計,運(yùn)維,交付相關(guān)經(jīng)驗,參與了酷狗,南方電網(wǎng),國泰君安等大型私有云平臺建設(shè),精通Linux,Kubernetes,OpenStack,Ceph等開源技術(shù),在云計算領(lǐng)域具有豐富實(shí)戰(zhàn)經(jīng)驗,擁有RHCA/OpenStack/Linux授課經(jīng)驗。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、烏拉特中網(wǎng)絡(luò)推廣、成都小程序開發(fā)、烏拉特中網(wǎng)絡(luò)營銷、烏拉特中企業(yè)策劃、烏拉特中品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供烏拉特中建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
上一篇文章中kubernetes系列教程(六)kubernetes資源管理和服務(wù)質(zhì)量初步介紹了kubernetes中的resource資源調(diào)度和服務(wù)質(zhì)量Qos,介紹了kubernetes中如何定義pod的資源和資源調(diào)度,以及設(shè)置resource之后的優(yōu)先級別Qos,接下來介紹kubernetes系列教程pod的調(diào)度機(jī)制。
kubernets是容器編排引擎,其中最主要的一個功能是容器的調(diào)度,通過kube-scheduler實(shí)現(xiàn)容器的完全自動化調(diào)度,調(diào)度周期分為:調(diào)度周期Scheduling Cycle和綁定周期Binding Cycle,其中調(diào)度周期細(xì)分為過濾filter和weight稱重,按照指定的調(diào)度策略將滿足運(yùn)行pod節(jié)點(diǎn)的node賽選出來,然后進(jìn)行排序;綁定周期是經(jīng)過kube-scheduler調(diào)度優(yōu)選的pod后,由特定的node節(jié)點(diǎn)watch然后通過kubelet運(yùn)行。
?
過濾階段包含預(yù)選Predicate和scoring排序,預(yù)選是篩選滿足條件的node,排序是最滿足條件的node打分并排序,預(yù)選的算法包含有:
過濾條件需要檢查node上滿足的條件,可以通過kubectl describe node node-id方式查看,如下圖:
優(yōu)選調(diào)度算法有:
nodeName是PodSpec中的一個字段,可以通過pod.spec.nodeName指定將pod調(diào)度到某個具體的node節(jié)點(diǎn)上,該字段比較特殊一般都為空,如果有設(shè)置nodeName字段,kube-scheduler會直接跳過調(diào)度,在特定節(jié)點(diǎn)上通過kubelet啟動pod。通過nodeName調(diào)度并非是集群的智能調(diào)度,通過指定調(diào)度的方式可能會存在資源不均勻的情況,建議設(shè)置Guaranteed的Qos,防止資源不均時候Pod被驅(qū)逐evince。如下以創(chuàng)建一個pod運(yùn)行在node-3上為例:
[root@node-1 demo]# cat nginx-nodeName.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-run-on-nodename
annotations:
kubernetes.io/description: "Running the Pod on specific nodeName"
spec:
containers:
- name: nginx-run-on-nodename
image: nginx:latest
ports:
- name: http-80-port
protocol: TCP
containerPort: 80
nodeName: node-3 #通過nodeName指定將nginx-run-on-nodename運(yùn)行在特定節(jié)點(diǎn)node-3上
[root@node-1 demo]# kubectl apply -f nginx-nodeName.yaml
pod/nginx-run-on-nodename created
[root@node-1 demo]# kubectl get pods nginx-run-on-nodename -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-run-on-nodename 1/1 Running 0 6m52s 10.244.2.15 node-3
nodeSelector是PodSpec中的一個字段,nodeSelector是最簡單實(shí)現(xiàn)將pod運(yùn)行在特定node節(jié)點(diǎn)的實(shí)現(xiàn)方式,其通過指定key和value鍵值對的方式實(shí)現(xiàn),需要node設(shè)置上匹配的Labels,節(jié)點(diǎn)調(diào)度的時候指定上特定的labels即可。如下以node-2添加一個app:web的labels,調(diào)度pod的時候通過nodeSelector選擇該labels:
[root@node-1 demo]# kubectl label node node-2 app=web
node/node-2 labeled
[root@node-1 demo]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node-1 Ready master 15d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node-2 Ready 15d v1.15.3 app=web,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-2,kubernetes.io/os=linux
node-3 Ready 15d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-3,kubernetes.io/os=linux
[root@node-1 demo]# cat nginx-nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-run-on-nodeselector
annotations:
kubernetes.io/description: "Running the Pod on specific node by nodeSelector"
spec:
containers:
- name: nginx-run-on-nodeselector
image: nginx:latest
ports:
- name: http-80-port
protocol: TCP
containerPort: 80
nodeSelector: #通過nodeSelector將pod調(diào)度到特定的labels
app: web
[root@node-1 demo]# kubectl apply -f nginx-nodeselector.yaml
pod/nginx-run-on-nodeselector created
[root@node-1 demo]# kubectl get pods nginx-run-on-nodeselector -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-run-on-nodeselector 1/1 Running 0 51s 10.244.1.24 node-2
系統(tǒng)默認(rèn)預(yù)先定義有多種內(nèi)置的labels,這些labels可以標(biāo)識node的屬性,如arch架構(gòu),操作系統(tǒng)類型,主機(jī)名等
affinity/anti-affinity和nodeSelector功能相類似,相比于nodeSelector,affinity的功能更加豐富,未來會取代nodeSelector,affinity增加了如下的一些功能增強(qiáng):
下面通過一個例子來演示node affinity的使用,requiredDuringSchedulingIgnoredDuringExecution指定需要滿足的條件,preferredDuringSchedulingIgnoredDuringExecution指定優(yōu)選的條件,兩者之間取與關(guān)系。
[root@node-1 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node-1 Ready master 15d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node-2 Ready 15d v1.15.3 app=web,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-2,kubernetes.io/os=linux
node-3 Ready 15d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-3,kubernetes.io/os=linux
[root@node-1 demo]# cat nginx-node-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-run-node-affinity
annotations:
kubernetes.io/description: "Running the Pod on specific node by node affinity"
spec:
containers:
- name: nginx-run-node-affinity
image: nginx:latest
ports:
- name: http-80-port
protocol: TCP
containerPort: 80
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node-1
- node-2
- node-3
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: app
operator: In
values: ["web"]
[root@node-1 demo]# kubectl apply -f nginx-node-affinity.yaml
pod/nginx-run-node-affinity created
[root@node-1 demo]# kubectl get pods --show-labels nginx-run-node-affinity -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-run-node-affinity 1/1 Running 0 106s 10.244.1.25 node-2
本文介紹了kubernetes中的調(diào)度機(jī)制,默認(rèn)創(chuàng)建pod是全自動調(diào)度機(jī)制,調(diào)度由kube-scheduler實(shí)現(xiàn),調(diào)度過程分為兩個階段調(diào)度階段(過濾和沉重排序)和綁定階段(在node上運(yùn)行pod)。通過干預(yù)有四種方式:
調(diào)度框架介紹:https://kubernetes.io/docs/concepts/configuration/scheduling-framework/
Pod調(diào)度方法:https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
?
當(dāng)你的才華撐不起你的野心時,你就應(yīng)該靜下心來學(xué)習(xí)
返回kubernetes系列教程目錄
?