首页 > 其他分享 >Pod亲和与反亲和、污点与容忍、驱逐

Pod亲和与反亲和、污点与容忍、驱逐

时间:2022-10-27 12:13:15浏览次数:44  
标签:tomcat kubernetes deployment 172.16 magedu io 反亲 污点 Pod

一、Pod调度

1.1、Pod调度流程

1.2、nodeSelector简介

nodeSelector 基于node标签选择器, 将pod调度的指定的目的节点上。
https://kubernetes.io/zh/docs/concepts/scheduling-eviction/assign-pod-node/

可用于基于服务类型干预Pod调度结果, 如对磁盘I/O要求高的pod调度到SSD节点, 对内存要求比较高的pod调度的内存较高的节点。

也可以用于区分不同项目的pod, 如将node添加不同项目的标签, 然后区分调度。

[root@easzlab-deploy Affinit-case]# kubectl describe node 172.16.88.157
Name:               172.16.88.157
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=172.16.88.157
                    kubernetes.io/os=linux
                    kubernetes.io/role=master
Annotations:        node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Tue, 18 Oct 2022 17:08:10 +0800
Taints:             node.kubernetes.io/unschedulable:NoSchedule
Unschedulable:      true
Lease:
  HolderIdentity:  172.16.88.157
  AcquireTime:     <unset>
  RenewTime:       Thu, 27 Oct 2022 10:47:06 +0800
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Wed, 19 Oct 2022 22:11:11 +0800   Wed, 19 Oct 2022 22:11:11 +0800   CalicoIsUp                   Calico is running on this node
  MemoryPressure       False   Thu, 27 Oct 2022 10:47:08 +0800   Tue, 18 Oct 2022 17:08:10 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Thu, 27 Oct 2022 10:47:08 +0800   Tue, 18 Oct 2022 17:08:10 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Thu, 27 Oct 2022 10:47:08 +0800   Tue, 18 Oct 2022 17:08:10 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Thu, 27 Oct 2022 10:47:08 +0800   Wed, 19 Oct 2022 22:09:58 +0800   KubeletReady                 kubelet is posting ready status. AppArmor enabled
Addresses:
  InternalIP:  172.16.88.157
  Hostname:    172.16.88.157
Capacity:
  cpu:                2
  ephemeral-storage:  49869532Ki
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             4026076Ki
  pods:               500
Allocatable:
  cpu:                2
  ephemeral-storage:  45959760616
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             3718876Ki
  pods:               500
System Info:
  Machine ID:                 1a19d04d6cc141e58368dc5dcc39f1a9
  System UUID:                8f09cdfe-374c-4faf-b9f2-78f0d9d3bd9a
  Boot ID:                    d7c79d7f-c664-44a6-9d03-0bf4ad6db51b
  Kernel Version:             5.4.0-128-generic
  OS Image:                   Ubuntu 20.04.4 LTS
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  containerd://1.6.4
  Kubelet Version:            v1.24.2
  Kube-Proxy Version:         v1.24.2
PodCIDR:                      10.200.1.0/24
PodCIDRs:                     10.200.1.0/24
Non-terminated Pods:          (1 in total)
  Namespace                   Name                 CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                 ------------  ----------  ---------------  -------------  ---
  kube-system                 calico-node-xh79g    250m (12%)    0 (0%)      0 (0%)           0 (0%)         8d
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                250m (12%)  0 (0%)
  memory             0 (0%)      0 (0%)
  ephemeral-storage  0 (0%)      0 (0%)
  hugepages-1Gi      0 (0%)      0 (0%)
  hugepages-2Mi      0 (0%)      0 (0%)
Events:              <none>
[root@easzlab-deploy Affinit-case]# 

1.3、nodeSelector调度

#为node节点打标签:
[root@easzlab-deploy Affinit-case]# kubectl label node 172.16.88.160 project="magedu"
node/172.16.88.160 labeled
[root@easzlab-deploy Affinit-case]# 
[root@easzlab-deploy Affinit-case]# kubectl label node 172.16.88.160 disktype="ssd"
node/172.16.88.161 labeled
[root@easzlab-deploy Affinit-case]# 
[root@easzlab-deploy Affinit-case]# kubectl get node --show-labels=true
NAME            STATUS                     ROLES    AGE   VERSION   LABELS
172.16.88.157   Ready,SchedulingDisabled   master   8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.157,kubernetes.io/os=linux,kubernetes.io/role=master
172.16.88.158   Ready,SchedulingDisabled   master   8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.158,kubernetes.io/os=linux,kubernetes.io/role=master
172.16.88.159   Ready,SchedulingDisabled   master   8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.159,kubernetes.io/os=linux,kubernetes.io/role=master
172.16.88.160   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.160,kubernetes.io/os=linux,kubernetes.io/role=node,project=magedu
172.16.88.161   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.161,kubernetes.io/os=linux,kubernetes.io/role=node
172.16.88.162   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.162,kubernetes.io/os=linux,kubernetes.io/role=node
172.16.88.163   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.163,kubernetes.io/os=linux,kubernetes.io/role=node
172.16.88.164   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.164,kubernetes.io/os=linux,kubernetes.io/role=node
172.16.88.165   Ready                      node     8d    v1.24.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.165,kubernetes.io/os=linux,kubernetes.io/role=node
[root@easzlab-deploy Affinit-case]# 

#将pod调度到目的node, yaml文件中指定的key与value必须精确匹配:
[root@easzlab-deploy Affinit-case]# vi case1-nodeSelector.yaml 
[root@easzlab-deploy Affinit-case]# cat case1-nodeSelector.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app2-deployment-label
  name: magedu-tomcat-app2-deployment
  namespace: magedu
spec:
  replicas: 4
  selector:
    matchLabels:
      app: magedu-tomcat-app2-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app2-selector
    spec:
      containers:
      - name: magedu-tomcat-app2-container
        image: tomcat:7.0.94-alpine 
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1
            memory: "512Mi"
          requests:
            cpu: 500m
            memory: "512Mi"
      nodeSelector:
        project: magedu
        disktype: ssd
[root@easzlab-deploy Affinit-case]# kubectl apply -f case1-nodeSelector.yaml 
deployment.apps/magedu-tomcat-app2-deployment created
[root@easzlab-deploy Affinit-case]# 
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                             READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999       1/1     Running   0          3d20h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z       1/1     Running   0          44h     10.200.105.177   172.16.88.164   <none>           <none>
magedu-tomcat-app2-deployment-75d7bb596c-n2c2s   1/1     Running   0          11m     10.200.40.212    172.16.88.160   <none>           <none>
magedu-tomcat-app2-deployment-75d7bb596c-pk2c2   1/1     Running   0          11m     10.200.40.214    172.16.88.160   <none>           <none>
magedu-tomcat-app2-deployment-75d7bb596c-pvf8c   1/1     Running   0          11m     10.200.40.215    172.16.88.160   <none>           <none>
magedu-tomcat-app2-deployment-75d7bb596c-z7klc   1/1     Running   0          11m     10.200.40.213    172.16.88.160   <none>           <none>
[root@easzlab-deploy Affinit-case]# 

指定节点创建

[root@easzlab-deploy Affinit-case]# vi case2-nodename.yaml 
[root@easzlab-deploy Affinit-case]# cat case2-nodename.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app2-deployment-label
  name: magedu-tomcat-app2-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-tomcat-app2-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app2-selector
    spec:
      nodeName: 172.16.88.161
      containers:
      - name: magedu-tomcat-app2-container
        image: tomcat:7.0.94-alpine 
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1
            memory: "512Mi"
          requests:
            cpu: 500m
            memory: "512Mi"
[root@easzlab-deploy Affinit-case]# kubectl apply -f case2-nodename.yaml 
deployment.apps/magedu-tomcat-app2-deployment configured
[root@easzlab-deploy Affinit-case]#
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                             READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999       1/1     Running   0          3d20h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z       1/1     Running   0          44h     10.200.105.177   172.16.88.164   <none>           <none>
magedu-tomcat-app2-deployment-756bb8b58b-xcd5f   1/1     Running   0          49s     10.200.233.116   172.16.88.161   <none>           <none>
[root@easzlab-deploy Affinit-case]#

二、node节点亲和性

affinity是Kubernetes 1.2版本后引入的新特性, 类似于nodeSelector, 允许使用者指定一些Pod在Node间调度的约束, 目前支持两种形式:

  • requiredDuringSchedulingIgnoredDuringExecution #必须满足pod调度匹配条件, 如果不满足则不进行调度
  • preferredDuringSchedulingIgnoredDuringExecution #倾向满足pod调度匹配条件, 不满足的情况下会调度的不符合条件的Node上
  • IgnoreDuringExecution表示如果在Pod运行期间Node的标签发生变化, 导致亲和性策略不能满足, 也会继续运行当前的Pod。
  • Affinity与anti-affinity的目的也是控制pod的调度结果, 但是相对于nodeSelector, Affinity(亲和)与anti-affinity(反亲和)的功能更加强大

affinity与nodeSelector对比:
1、亲和与反亲和对目的标签的选择匹配不仅仅支持and, 还支持In、 NotIn、 Exists、 DoesNotExist、 Gt、 Lt。

  • In: 标签的值存在匹配列表中(匹配成功就调度到目的node, 实现node亲和)
  • NotIn: 标签的值不存在指定的匹配列表中(不会调度到目的node, 实现反亲和)
  • Gt: 标签的值大于某个值(字符串)
  • Lt: 标签的值小于某个值(字符串)
  • Exists: 指定的标签存在

2、可以设置软匹配和硬匹配, 在软匹配下, 如果调度器无法匹配节点, 仍然将pod调度到其它不符合条件的节点。
3、还可以对pod定义亲和策略, 比如允许哪些pod可以或者不可以被调度至同一台node。
注:

  • 如果定义一个nodeSelectorTerms(条件)中通过一个matchExpressions基于列表指定了多个operator条件, 则只要满足其中一个条件, 就会被调度到相应的节点上, 即or的关系, 即如果nodeSelectorTerms下面有多个条件的话, 只要满足任何一个条件就可以了。
  • 如果定义一个nodeSelectorTerms中都通过一个matchExpressions(匹配表达式)指定key匹配多个条件, 则所有的目的条件都必须满足才会调度到对应的节点, 即and的关系, 即果matchExpressions有多个选项的话, 则必须同时满足所有这些条件才能正常调度。

2.1、硬亲和-nodeAffinity-requiredDuring-matchExpressions-1

affinity:
 nodeAffinity:
  requiredDuringSchedulingIgnoredDuringExecution:
  nodeSelectorTerms:
  - matchExpressions: #匹配条件1,有一个key但是有多个values、 则只要匹配成功一个value就可以调度
   - key: disktype
     operator: In
     values:
     - ssd #只有一个value是匹配成功也可以调度
     - xxx
  - matchExpressions: #匹配条件2,多个matchExpressions加上以及每个matchExpressions 有一个key及多个values只有其中一个value匹配成功就可以调度
   - key: project
     operator: In
     values:
     - mmm #即使这俩条件都匹配不上也可以调度, 即多个matchExpressions只要有任意一个能匹配任何一个value就可以调用。
     - nnn

验证测试

[root@easzlab-deploy Affinit-case]# vi case3-1.1-nodeAffinity-requiredDuring-matchExpressions.yaml 
[root@easzlab-deploy Affinit-case]# cat case3-1.1-nodeAffinity-requiredDuring-matchExpressions.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app2-deployment-label
  name: magedu-tomcat-app2-deployment
  namespace: magedu
spec:
  replicas: 5
  selector:
    matchLabels:
      app: magedu-tomcat-app2-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app2-selector
    spec:
      containers:
      - name: magedu-tomcat-app2-container
        image: tomcat:7.0.94-alpine
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions: #匹配条件1,有一个key但是有多个values、则只要匹配成功一个value就可以调度 
              - key: disktype
                operator: In
                values:
                - ssd # 只有一个value是匹配成功也可以调度
                - xxx
            - matchExpressions: #匹配条件1,有一个key但是有多个values、则只要匹配成功一个value就可以调度 
              - key: project
                operator: In
                values:
                - mmm  #即使这俩条件都匹配不上也可以调度,即多个matchExpressions只要有任意一个能匹配任何一个value就可以调用。
                - nnn

[root@easzlab-deploy Affinit-case]# kubectl apply -f case3-1.1-nodeAffinity-requiredDuring-matchExpressions.yaml 
deployment.apps/magedu-tomcat-app2-deployment configured
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                             READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999       1/1     Running   0          3d20h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z       1/1     Running   0          45h     10.200.105.177   172.16.88.164   <none>           <none>
magedu-tomcat-app2-deployment-555df55d49-4qfkc   1/1     Running   0          25s     10.200.233.122   172.16.88.161   <none>           <none>
magedu-tomcat-app2-deployment-555df55d49-89psp   1/1     Running   0          28s     10.200.40.216    172.16.88.160   <none>           <none>
magedu-tomcat-app2-deployment-555df55d49-8trjm   1/1     Running   0          25s     10.200.40.218    172.16.88.160   <none>           <none>
magedu-tomcat-app2-deployment-555df55d49-h6rrn   1/1     Running   0          28s     10.200.233.120   172.16.88.161   <none>           <none>
magedu-tomcat-app2-deployment-555df55d49-slkm8   1/1     Running   0          28s     10.200.40.217    172.16.88.160   <none>           <none>
[root@easzlab-deploy Affinit-case]# 

2.2、硬亲和-nodeAffinity-requiredDuring-matchExpressions-2

affinity:
 nodeAffinity:
  requiredDuringSchedulingIgnoredDuringExecution:
   nodeSelectorTerms:
   - matchExpressions: #匹配条件1, 同一个key的多个value只有有一个匹配成功就认为当前key匹配成功
    - key: disktype
      operator: In
      values:
      - ssd
      - hdd
    - key: project #匹配条件2, 当前key也要匹配成功一个value, 即条件1和条件2必须同时每个key匹配成功一个value,否则不调度
      operator: In
      values:
      - magedu

验证测试

[root@easzlab-deploy Affinit-case]# vi case3-1.2-nodeAffinity-requiredDuring-matchExpressions.yaml 
[root@easzlab-deploy Affinit-case]# cat case3-1.2-nodeAffinity-requiredDuring-matchExpressions.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app2-deployment-label
  name: magedu-tomcat-app2-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-tomcat-app2-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app2-selector
    spec:
      containers:
      - name: magedu-tomcat-app2-container
        image: tomcat:7.0.94-alpine
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions: #匹配条件1,同一个key的多个value只有有一个匹配成功就认为当前key匹配成功 
              - key: disktype
                operator: In
                values:
                - ssd
                - hddx
              - key: project #匹配条件2,当前key也要匹配成功一个value,即条件1和条件2必须同时每个key匹配成功一个value,否则不调度
                operator: In
                values:
                - magedu
[root@easzlab-deploy Affinit-case]#
[root@easzlab-deploy Affinit-case]# kubectl delete -f case3-1.1-nodeAffinity-requiredDuring-matchExpressions.yaml #删除旧的pod
deployment.apps "magedu-tomcat-app2-deployment" deleted
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu  #确认是否删除
NAME                                         READY   STATUS    RESTARTS   AGE
magedu-jenkins-deployment-79bbd88cb7-84999   1/1     Running   0          3d21h
magedu-jenkins-deployment-79bbd88cb7-pmd9z   1/1     Running   0          45h
[root@easzlab-deploy Affinit-case]# kubectl apply -f case3-1.2-nodeAffinity-requiredDuring-matchExpressions.yaml 
deployment.apps/magedu-tomcat-app2-deployment created
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                             READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999       1/1     Running   0          3d21h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z       1/1     Running   0          45h     10.200.105.177   172.16.88.164   <none>           <none>
magedu-tomcat-app2-deployment-585f4864c4-brw6j   1/1     Running   0          15s     10.200.40.219    172.16.88.160   <none>           <none>
[root@easzlab-deploy Affinit-case]# 

2.3、软亲和-nodeAffinity-preferredDuring

affinity:
 nodeAffinity:
  preferredDuringSchedulingIgnoredDuringExecution:
  - weight: 80 #软亲和条件1, weight值越大优先级越高, 越优先匹配调度
    preference:
     matchExpressions:
     - key: project
       operator: In
       values:
        - magedu
  - weight: 60 #软亲和条件2, 在条件1不满足时匹配条件2
    preference:
     matchExpressions:
     - key: disktype
       operator: In
       values:
        - ssd

验证测试

[root@easzlab-deploy Affinit-case]# kubectl delete -f case3-1.2-nodeAffinity-requiredDuring-matchExpressions.yaml #删除旧的pod
deployment.apps "magedu-tomcat-app2-deployment" deleted
[root@easzlab-deploy Affinit-case]# 
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu #确实是否彻底删除
NAME                                         READY   STATUS    RESTARTS   AGE
magedu-jenkins-deployment-79bbd88cb7-84999   1/1     Running   0          3d21h
magedu-jenkins-deployment-79bbd88cb7-pmd9z   1/1     Running   0          45h
[root@easzlab-deploy Affinit-case]# 

#修改node label, 测试pod能否调度软策略优先级教高的节点 [root@easzlab-deploy Affinit-case]# kubectl label node 172.16.88.160 project- #取消160节点 project标签 node/172.16.88.160 unlabeled [root@easzlab-deploy Affinit-case]# kubectl label node 172.16.88.161 project=magedu #指定161节点 project标签 node/172.16.88.161 labeled [root@easzlab-deploy Affinit-case]# kubectl get node --show-labels=true NAME STATUS ROLES AGE VERSION LABELS 172.16.88.157 Ready,SchedulingDisabled master 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.157,kubernetes.io/os=linux,kubernetes.io/role=master 172.16.88.158 Ready,SchedulingDisabled master 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.158,kubernetes.io/os=linux,kubernetes.io/role=master 172.16.88.159 Ready,SchedulingDisabled master 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.159,kubernetes.io/os=linux,kubernetes.io/role=master 172.16.88.160 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.160,kubernetes.io/os=linux,kubernetes.io/role=node 172.16.88.161 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.161,kubernetes.io/os=linux,kubernetes.io/role=node,project=magedu 172.16.88.162 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.162,kubernetes.io/os=linux,kubernetes.io/role=node 172.16.88.163 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.163,kubernetes.io/os=linux,kubernetes.io/role=node 172.16.88.164 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.164,kubernetes.io/os=linux,kubernetes.io/role=node 172.16.88.165 Ready node 8d v1.24.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.88.165,kubernetes.io/os=linux,kubernetes.io/role=node [root@easzlab-deploy Affinit-case]# [root@easzlab-deploy Affinit-case]# vi case3-2.1-nodeAffinity-preferredDuring.yaml [root@easzlab-deploy Affinit-case]# cat case3-2.1-nodeAffinity-preferredDuring.yaml kind: Deployment #apiVersion: extensions/v1beta1 apiVersion: apps/v1 metadata: labels: app: magedu-tomcat-app2-deployment-label name: magedu-tomcat-app2-deployment namespace: magedu spec: replicas: 1 selector: matchLabels: app: magedu-tomcat-app2-selector template: metadata: labels: app: magedu-tomcat-app2-selector spec: containers: - name: magedu-tomcat-app2-container image: tomcat:7.0.94-alpine imagePullPolicy: IfNotPresent #imagePullPolicy: Always ports: - containerPort: 8080 protocol: TCP name: http affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 80 #软亲和条件1,weight值越大优先级越高,越优先匹配调度 preference: matchExpressions: - key: project operator: In values: - magedu - weight: 60 #软亲和条件2,在条件1不满足时匹配条件2 preference: matchExpressions: - key: disktype operator: In values: - ssd [root@easzlab-deploy Affinit-case]# kubectl apply -f case3-2.1-nodeAffinity-preferredDuring.yaml deployment.apps/magedu-tomcat-app2-deployment created [root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES magedu-jenkins-deployment-79bbd88cb7-84999 1/1 Running 0 3d21h 10.200.104.250 172.16.88.163 <none> <none> magedu-jenkins-deployment-79bbd88cb7-pmd9z 1/1 Running 0 45h 10.200.105.177 172.16.88.164 <none> <none> magedu-tomcat-app2-deployment-56c9bdffdd-sl62q 1/1 Running 0 18s 10.200.233.123 172.16.88.161 <none> <none> [root@easzlab-deploy Affinit-case]#

硬亲和与软亲和结合使用-nodeAffinity-requiredDuring-preferredDuring

基于硬亲和和软亲和实现pod调度: 1.绝对不会调度在哪些node 2.倾向于调度到哪些node
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution: #硬策略
      nodeSelectorTerms:
      - matchExpressions: #硬匹配条件1
        - key: "kubernetes.io/role" 
          operator: NotIn
          values:
          - "master" #硬性匹配key 的值kubernetes.io/role不包含master的节点,即绝对不会调度到master节点(node反亲和)
    preferredDuringSchedulingIgnoredDuringExecution: #软策略
    - weight: 80 
      preference: 
        matchExpressions: 
        - key: project 
          operator: In 
          values: 
            - magedu
    - weight: 60 
      preference: 
        matchExpressions: 
        - key: disktype
          operator: In 
          values: 
            - ssd

验证测试

[root@easzlab-deploy Affinit-case]# vi case3-2.2-nodeAffinity-requiredDuring-preferredDuring.yaml 
[root@easzlab-deploy Affinit-case]# cat case3-2.2-nodeAffinity-requiredDuring-preferredDuring.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app2-deployment-label
  name: magedu-tomcat-app2-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-tomcat-app2-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app2-selector
    spec:
      containers:
      - name: magedu-tomcat-app2-container
        image: tomcat:7.0.94-alpine
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution: #硬亲和
            nodeSelectorTerms:
            - matchExpressions: #硬匹配条件1
              - key: "kubernetes.io/role" 
                operator: NotIn
                values:
                - "master" #硬性匹配key 的值kubernetes.io/role不包含master的节点,即绝对不会调度到master节点(node反亲和)
          preferredDuringSchedulingIgnoredDuringExecution: #软亲和
          - weight: 80 
            preference: 
              matchExpressions: 
              - key: project 
                operator: In 
                values: 
                  - magedu
          - weight: 60 
            preference: 
              matchExpressions: 
              - key: disktype
                operator: In 
                values: 
                  - hdd
[root@easzlab-deploy Affinit-case]# kubectl delete -f case3-2.1-nodeAffinity-preferredDuring.yaml 
deployment.apps "magedu-tomcat-app2-deployment" deleted
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                         READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999   1/1     Running   0          3d21h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z   1/1     Running   0          45h     10.200.105.177   172.16.88.164   <none>           <none>
[root@easzlab-deploy Affinit-case]# kubectl apply -f case3-2.2-nodeAffinity-requiredDuring-preferredDuring.yaml 
deployment.apps/magedu-tomcat-app2-deployment created
[root@easzlab-deploy Affinit-case]# kubectl get pod -n magedu -owide
NAME                                             READY   STATUS    RESTARTS   AGE     IP               NODE            NOMINATED NODE   READINESS GATES
magedu-jenkins-deployment-79bbd88cb7-84999       1/1     Running   0          3d21h   10.200.104.250   172.16.88.163   <none>           <none>
magedu-jenkins-deployment-79bbd88cb7-pmd9z       1/1     Running   0          45h     10.200.105.177   172.16.88.164   <none>           <none>
magedu-tomcat-app2-deployment-7564998d64-6565f   1/1     Running   0          4s      10.200.233.125   172.16.88.161   <none>           <none>
[root@easzlab-deploy Affinit-case]# 

 

 

 


 

 

标签:tomcat,kubernetes,deployment,172.16,magedu,io,反亲,污点,Pod
From: https://www.cnblogs.com/cyh00001/p/16617650.html

相关文章

  • k8s基础篇 pod(九)Pod生命周期
    9.1Init容器Pod里面可以有一个或者多个容器,部署应用的容器可以称为主容器,在创建Pod时候,Pod中可以有一个或多个先于主容器启动的Init容器,这个init容器就可以成为初始化容......
  • 运行一个pod资源10
    运行一个pod资源10接下来了解k8s的资源单位k8s里面最小的资源单位就是pod,如果说咱们教室这里有一面墙,这面墙的最小资源单位是什么?是一块砖,在k8s里面有很多的资源,其中最小......
  • K8S进阶篇-高级调度计划任务、污点和容忍、Affinity
    一、Job1Job可以干什么Job可以干什么?在容器启动或退出时做一些任务。Job可以并行执行。1、需要等待后执行的任务2、导入SQL文件3、创建用户、清理表........等等 ......
  • Pod资源清单
    apiVersion:v1#必选,版本号,例如v1kind:Pod#必选,资源类型,例如Podmetadata:#必选,元数据name:string#必选,Pod名称namespace:strin......
  • k8s如何调度pod
    选择节点步骤k8s默认的调度器是kube-scheduler,它会为新创建的pod且未被调度的pod选择最合适的节点。这个过程如下过滤:节点是否有足够的资源满足请求资源条件,满足条件的节点......
  • HELM chart 部署mongodb 到k8s 集群 pod 无法解析dns 问题
    1,正常拉取bitbami的包部署mongodb到k8s集群,运行前一切正常2,部署到mongodb-1的时候,卡主,查看日志,arbiter报无法连接mongodb-0或者mongodb-headless 3,搜到早些年的issu......
  • ks8:pod使用nfs存储的3种方式:直接在pod中通过原生nfs配置volumes
     apiVersion:apps/v1kind:Deploymentmetadata:creationTimestamp:nulllabels:app:testnfsname:testnfsspec:replicas:2selector:mat......
  • k8s-pod控制器Deployment
    在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故......
  • volume-pod.yaml
    apiVersion:v1kind:Podmetadata:name:volume-podspec:containers:-name:nginx-containerimage:nginxports:-containerPort:80vol......
  • Kubernetes--Pod对象的生命周期
    Pod对象自从其创建开始至其终止退出的时间范围称为其生命周期。在这段时间中,Pod会处于多种不同的状态,并执行一些操作;其中,创建主容器(maincontainer)为必需的操作,其他可选......