k8s排程器

z597011036發表於2019-11-17

節點硬親和度:Pod排程時必須滿足規劃,如不滿足Pod狀態將是Penging狀態。

節點軟親和度:Pod排程時先按規則進行排程,如不滿足規則也會找一個不匹配的節點執行。

Pod硬親和度,軟親和度與節點硬親和度,軟親和度相似。


1.節點硬親和度(節點不滿足規則)

[root@k8s01 yaml]# cat pod-affinity01.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-01
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - {key: zone,operator: In,values: ["one"]}   --節點zone(鍵)標籤必須是one(值)
  containers:
  - name: pod-01
    image: nginx:latest
    imagePullPolicy: Never

[root@k8s01 yaml]# kubectl apply -f pod-affinity01.yaml

pod/pod-01 created

[root@k8s01 yaml]# kubectl get pods --show-labels

NAME    READY    STATUS      RESTARTS      AGE      LABELS

pod-01       0/1        Pending                 0            103s         <none> --node節點不滿足pod標籤

[root@k8s01 yaml]# kubectl describe pods pod-01

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

Events:

Type Reason Age From Message

---- ------ ---- ---- -------

Warning FailedScheduling <unknown> default-scheduler 0/3 nodes are available: 3 node(s) didn't match node selector.

Warning FailedScheduling <unknown> default-scheduler 0/3 nodes are available: 3 node(s) didn't match node selector.

[root@k8s01 yaml]#

- matchExpressions:
  - {key: zone,operator: In,values: ["one"]} --zone是鍵,operator:是表示式,one是值
  - {key: zone,operator: In,values: ["one","two"]} --In 是包含
  - {key: ssd,operator: Exists,values: []} --Exists是存在


2.節點硬親和度(節點滿足規則)

[root@k8s01 yaml]# kubectl label node k8s02 zone=one     --在k8s02節點建立標籤

node/k8s02 labeled

[root@k8s01 yaml]# kubectl get pods --show-labels -o wide

NAME    READY    STATUS    RESTARTS    AGE    IP    NODE    NOMINATED    NODE    READINESS   GATES LABELS

pod-01      1/1            Running       0         6m13s          10.244.1.37         k8s02       <none>     <none>     <none>

[root@k8s01 yaml]#


3.節點軟親和度

[root@k8s01 yaml]# cat pod-affinity02.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-02
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 60    --60%的Pod會排程到zone標籤,one值這個節點上
            preference:
              matchExpressions:
              - {key: zone,operator: In,values: ["one"]}
      containers:
      - name: pod-02
        image: nginx:latest
        imagePullPolicy: Never

[root@k8s01 yaml]# kubectl apply -f pod-affinity02.yaml

deployment.apps/pod-02 created

[root@k8s01 yaml]# kubectl get pods --show-labels -o wide    --滿足標籤可建立,不滿足也可建立

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

pod-02-77d87986b-9bzg5 1/1 Running 0 16s 10.244.1.39 k8s02 <none> <none> app=myapp,pod-template-hash=77d87986b

pod-02-77d87986b-dckjq 1/1 Running 0 16s 10.244.2.42 k8s03 <none> <none> app=myapp,pod-template-hash=77d87986b

pod-02-77d87986b-z7v47 1/1 Running 0 16s 10.244.1.38 k8s02 <none> <none> app=myapp,pod-template-hash=77d87986b

[root@k8s01 yaml]#


4. Pod硬親和度(節點不滿足規則)

[root@k8s01 yaml]# kubectl  get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx                    1/1     Running   0          4s    app=web
[root@k8s01 yaml]# cat pod-affinity03.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: pod-01
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - {key: app,operator: In,values: ["web1"]}   --標籤值不滿足
        topologyKey: kubernetes.io/hostname
  containers:
  - name: pod-01
    image: nginx:latest
    imagePullPolicy: Never

[root@k8s01 yaml]# kubectl apply -f  pod-affinity03.yaml
pod/pod-01 created
[root@k8s01 yaml]# kubectl  get pods --show-labels -o wide
NAME     READY   STATUS    RESTARTS   AGE    IP      NODE     NOMINATED NODE   READINESS GATES   LABELS
nginx                    1/1     Running   0          8m9s   10.244.1.42   k8s02    <none>           <none>            app=web
pod-01                  0/1     Pending   0          28s    <none>        <none>   <none>           <none>            <none>
[root@k8s01 yaml]#


5.Pod硬親和度 (節點滿足規則)

[root@k8s01 yaml]# kubectl  get pods --show-labels      --檢視pod標籤
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx                    1/1     Running   0          4s    app=web
[root@k8s01 yaml]# cat pod-affinity04.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: pod-01
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - {key: app,operator: In,values: ["web"]}
        topologyKey: kubernetes.io/hostname
  containers:
  - name: pod-01
    image: nginx:latest
    imagePullPolicy: Never

[root@k8s01 yaml]# kubectl apply -f  pod-affinity04.yaml
pod/pod-01 created
[root@k8s01 yaml]# kubectl  get pods --show-labels -o wide       --建立的新Pod會找app=web的標籤
NAME     READY   STATUS    RESTARTS   AGE     IP     NODE    NOMINATED NODE   READINESS GATES   LABELS
nginx                    1/1     Running   0          4m14s   10.244.1.42   k8s02   <none>           <none>            app=web
pod-01                  1/1     Running   0          17s     10.244.1.43   k8s02   <none>           <none>            <none>
[root@k8s01 yaml]#


6.Pod軟親和度

[root@k8s01 yaml]# kubectl  get pods --show-labels -o wide
NAME      READY   STATUS    RESTARTS   AGE     IP      NODE     NOMINATED NODE   READINESS GATES   LABELS
nginx                    1/1     Running   0          14m     10.244.1.42   k8s02    <none>           <none>            app=web

[root@k8s01 yaml]# cat pod-affinity04.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-02
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 60
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - {key: app,operator: In,values: ["web12"]}
              topologyKey: zone
      containers:
      - name: pod-02
        image: nginx:latest
        imagePullPolicy: Never

[root@k8s01 yaml]# kubectl  get pods --show-labels -o wide     --不滿足Pod軟親和度也可建立
NAME      READY   STATUS    RESTARTS   AGE     IP    NODE     NOMINATED NODE   READINESS GATES   LABELS
nginx                    1/1     Running   0          16m     10.244.1.42   k8s02    <none>      <none>       app=web
pod-02-6f96fbfdf-52gbf   1/1     Running   0   11s     10.244.2.43   k8s03    <none>    <none>     app=myapp,pod-template-hash=6f96fbfdf
pod-02-6f96fbfdf-dl5z5   1/1     Running   0    11s     10.244.1.44   k8s02    <none>    <none>    app=myapp,pod-template-hash=6f96fbfdf
pod-02-6f96fbfdf-f8bzn   1/1     Running   0    11s     10.244.0.55   k8s01    <none>    <none>    app=myapp,pod-template-hash=6f96fbfdf
[root@k8s01 yaml]#


7.定義汙點和容忍度

NoSchedule:不能容忍此汙點的新Pod對像,不能排程到當前節點,強制性。

PreferNoSchedule:新Pod儘量不排程到此節點,若無其它節點可以排程,也允許使用當前節點排程,柔和性。

NoExecute:新Pod不能排程到當前節點,如果當前節點存在Pod也會被驅逐,強制性。

[root@k8s01 yaml]# kubectl  describe node k8s02 | grep -i taints     --檢視汙點
Taints:             <none>
[root@k8s01 yaml]# kubectl taint node k8s02 node-type=production:NoSchedule    --為k8s02節點打汙點

node/k8s02 tainted
[root@k8s01 yaml]# kubectl  describe node k8s02 | grep -i taints
Taints:             node-type=production:NoSchedule
[root@k8s01 yaml]# cat pod-affinity05.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-02
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
      - name: pod-02
        image: nginx:latest
        imagePullPolicy: Never

[root@k8s01 yaml]# kubectl apply -f  pod-affinity05.yaml
deployment.apps/pod-02 created
[root@k8s01 yaml]# kubectl  get pods -o wide --show-labels     --k8s02節點已打汙點,所以不能排程Pod對像
NAME     READY   STATUS    RESTARTS   AGE   IP       NODE    NOMINATED NODE   READINESS GATES   LABELS
pod-02-5c54dc6489-j7tsh   1/1     Running   0          29s   10.244.0.56   k8s01   <none>           <none>            app=myapp,pod-template-hash=5c54dc6489
pod-02-5c54dc6489-sk5dm   1/1     Running   0          29s   10.244.2.44   k8s03   <none>           <none>            app=myapp,pod-template-hash=5c54dc6489
pod-02-5c54dc6489-sn5wd   1/1     Running   0          29s   10.244.2.45   k8s03   <none>           <none>            app=myapp,pod-template-hash=5c54dc6489

[root@k8s01 yaml]


8.刪除汙點

[root@k8s01 yaml]# kubectl  taint node k8s02 node-type:NoSchedule-      --刪除 NoSchedule汙點

node/k8s02 untainted

[root@k8s01 yaml]# kubectl taint node k8s02 node-type=production:PreferNoSchedule      --打上汙點
node/k8s02 tainted
[root@k8s01 yaml]# kubectl  describe node k8s02 | grep -i node-type
Taints:             node-type=production:PreferNoSchedule
[root@k8s01 yaml]# kubectl taint node k8s02 node-type:PreferNoSchedule-    --刪除 PreferNoSchedule汙點

node/k8s02 untainted
[root@k8s01 yaml]# kubectl  describe node k8s02 | grep -i node-type

[root@k8s01 yaml]# kubectl taint node k8s02 node-type-                  --刪除所有汙點
node/k8s02 untainted
[root@k8s01 yaml]# kubectl  describe node k8s02 | grep -i node-type
[root@k8s01 yaml]#


9. 驅逐節點上的Pod

[root@k8s01 yaml]# kubectl cordon k8s03    --將新建立的pod不能排程到k8s03節點,以前的Pod不受影響。
node/k8s03 cordoned
[root@k8s01 yaml]# kubectl  get nodes
NAME    STATUS                     ROLES    AGE   VERSION
k8s01   Ready                      master   71d   v1.16.0
k8s02   Ready                      <none>   70d   v1.16.0
k8s03   Ready, SchedulingDisabled   <none>   30d   v1.16.0

[root@k8s01 yaml]# kubectl  get pods -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP      NODE    NOMINATED NODE   READINESS GATES
pod-02-5c54dc6489-442kx   1/1     Running   0          12m   10.244.2.48   k8s03   <none>           <none>
pod-02-5c54dc6489-92l8m   1/1     Running   0          12m   10.244.2.49   k8s03   <none>           <none>
pod-02-5c54dc6489-k4bc7   1/1     Running   0          12m   10.244.0.58   k8s01   <none>           <none>
[root@k8s01 yaml]# kubectl uncordon k8s03     --釋放排程規則
node/k8s03 uncordoned
[root@k8s01 yaml]# kubectl  get nodes
NAME    STATUS   ROLES    AGE   VERSION
k8s01   Ready    master   71d   v1.16.0
k8s02   Ready    <none>   70d   v1.16.0
k8s03   Ready    <none>   30d   v1.16.0
[root@k8s01 yaml]# kubectl drain k8s03 --ignore-daemonsets     --去除k8s03節點上的Pod
node/k8s03 already cordoned
WARNING: ignoring DaemonSet-managed Pods: kube-system/kube-flannel-ds-amd64-cg795, kube-system/kube-proxy-h4dkf
evicting pod "pod-02-5c54dc6489-92l8m"
evicting pod "pod-02-5c54dc6489-442kx"
pod/pod-02-5c54dc6489-92l8m evicted
pod/pod-02-5c54dc6489-442kx evicted
node/k8s03 evicted
[root@k8s01 yaml]# kubectl  get nodes
NAME    STATUS               ROLES    AGE   VERSION
k8s01   Ready                      master   71d   v1.16.0
k8s02   Ready                      <none>   70d   v1.16.0
k8s03   Ready, SchedulingDisabled   <none>   30d   v1.16.0
[root@k8s01 yaml]# kubectl  get pods -o wide         --以前k8s03節點的Pod全部轉移到其它節點上
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
pod-02-5c54dc6489-k4bc7   1/1     Running   0          14m   10.244.0.58   k8s01   <none>           <none>
pod-02-5c54dc6489-mxk46   1/1     Running   0          25s   10.244.1.46   k8s02   <none>           <none>
pod-02-5c54dc6489-vmb8l   1/1     Running   0          25s   10.244.1.45   k8s02   <none>           <none>
[root@k8s01 yaml]# kubectl uncordon k8s03     --恢復排程
node/k8s03 uncordoned
[root@k8s01 yaml]# kubectl  get nodes
NAME    STATUS   ROLES    AGE   VERSION
k8s01   Ready    master   71d   v1.16.0
k8s02   Ready    <none>   70d   v1.16.0
k8s03   Ready    <none>   30d   v1.16.0
[root@k8s01 yaml]#


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2664507/,如需轉載,請註明出處,否則將追究法律責任。

相關文章