k8s学习(二十一) 亲和性调度

亲和性调度可以分成软策略和硬策略两种方式:
软策略就是如果你没有满足调度要求的节点的话,pod 就会忽略这条规则,继续完成调度过程,说白了就是满足条件最好了,没有的话也无所谓了的策略
硬策略就比较强硬了,如果没有满足条件的节点的话,就不断重试直到满足条件为止,简单说就是你必须满足我的要求,不然我就不干的策略。

1、通过nodeSelector 和label选择节点调度

查看节点的label

[root@k8s-node1 test]# kubectl get nodes --show-labels
NAME        STATUS   ROLES    AGE   VERSION   LABELS                                                                                                      
k8s-node1   Ready    master   16d   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux,node-role.kubernetes.io/master=                                                                                            
k8s-node2   Ready       16d   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux                                                                                                                            
k8s-node3   Ready       16d   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node3,kubernetes.io/os=linux                                

为k8s-node3添加一个标签

[root@k8s-node1 test]# kubectl label nodes k8s-node3 com=zqw                                                                                              
node/k8s-node3 labeled     

构建一个pod使用此标签

---
 
apiVersion: v1
 
kind: Pod
 
metadata:
  labels:
    app: helloworld-nodeselector
  name: helloworld-nodeselector
 
spec:
  containers:
    - name: helloworld-nodeselector
      image: 172.16.10.190:8008/helloworld:0.1.1
      imagePullPolicy: Always
  imagePullSecrets:
    - name: myregistrykey6
  nodeSelector:
    com: zqw
kubectl apply -f helloworld-nodeSelector.yaml

查看pod

[root@k8s-node1 k8s]# kubectl get pods -owide
NAME                                         READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
dns-test                                     1/1     Running   0          14d     10.244.2.26   k8s-node3              
helloworld-nodeselector                      1/1     Running   0          2m10s   10.244.2.61   k8s-node3              
helloworld011-5cf986495c-6zslr               1/1     Running   0          14d     10.244.2.27   k8s-node3              
helloworld011-5cf986495c-9xh5d               1/1     Running   0          14d     10.244.1.20   k8s-node2              
innocent-boxer-hello-helm-867bd48ccc-k6jmw   1/1     Running   0          19h     10.244.0.33   k8s-node1              
kube-hpa-demo-d789d89b-9xgdx                 1/1     Running   0          16d     10.244.1.9    k8s-node2              
kube-hpa-demo-d789d89b-pw77j                 1/1     Running   0          16d     10.244.1.8    k8s-node2              
kube-node-6545cf5596-khw4x                   1/1     Running   0          16d     10.244.2.3    k8s-node3              
kube-node-6545cf5596-q7kd7                   1/1     Running   0          16d     10.244.1.5    k8s-node2              
liveness-tcpsocket                           1/1     Running   0          16d     10.244.1.6    k8s-node2              
myrabbitmq-0                                 1/1     Running   0          18h     10.244.2.60   k8s-node3              
mysqldb-6ffc996bdb-lxgsp                     0/1     Pending   0          19h                              
nfs-client-provisioner-84f967cff9-dtxxt      1/1     Running   0          9d      10.244.2.43   k8s-node3              
nfs-web-0                                    1/1     Running   0          9d      10.244.2.44   k8s-node3              
nfs-web-1                                    1/1     Running   0          9d      10.244.1.22   k8s-node2              
nginx-ds-7spc2                               1/1     Running   0          15d     10.244.1.12   k8s-node2              
nginx-ds-ffd9v                               1/1     Running   0          15d     10.244.2.13   k8s-node3              
nginx-ds-rmqvl                               1/1     Running   0          5d23h   10.244.0.7    k8s-node1              
static-nginx-k8s-node1                       1/1     Running   0          16d     10.244.0.2    k8s-node1              
use-pvc-deployment-85997f976b-llg9h          1/1     Running   0          9d      10.244.2.42   k8s-node3              
use-pvc-deployment-85997f976b-tm92t          1/1     Running   0          9d      10.244.1.21   k8s-node2              
web-0                                        1/1     Running   0          14d     10.244.2.25   k8s-node3              
web-1                                        1/1     Running   0          14d     10.244.1.19   k8s-node2              

发现helloworld-nodeselector在k8s-node3节点上

2、使用节点亲和性策略nodeAffinity

创建helloworld-nodeAffinity.yaml

---
 
apiVersion: extensions/v1beta1
 
kind: Deployment
 
metadata:
 
  name: helloworld-nodeaffinity
 
spec:
  revisionHistoryLimit: 10
  replicas: 3
 
  template:
 
    metadata:
 
      labels:
 
        app: web-nodeaffinity
 
    spec:
 
      containers:
 
        - name: helloworld-nodeaffinity
 
          image: 172.16.10.190:8008/helloworld:0.0.5
 
          ports:
 
            - containerPort: 8080
      imagePullSecrets:

        - name: myregistrykey6

      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:  # 硬策略
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: NotIn
                values:
                - k8s-node2
          preferredDuringSchedulingIgnoredDuringExecution:  # 软策略
          - weight: 1
            preference:
              matchExpressions:
              - key: com
                operator: In
                values:
                - zqw

  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
kubectl apply -f helloworld-nodeAffinity.yaml

查看pod信息

[root@k8s-node1 k8s]# kubectl get pods -owide
NAME                                         READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
dns-test                                     1/1     Running   0          15d     10.244.2.26   k8s-node3              
helloworld-nodeaffinity-5cfdccb89-9k4vz      1/1     Running   0          13s     10.244.2.63   k8s-node3              
helloworld-nodeaffinity-5cfdccb89-dqqlk      1/1     Running   0          13s     10.244.0.37   k8s-node1              
helloworld-nodeaffinity-5cfdccb89-k89zq      1/1     Running   0          13s     10.244.2.62   k8s-node3              
helloworld-nodeselector                      1/1     Running   0          16m     10.244.2.61   k8s-node3              
helloworld011-5cf986495c-6zslr               1/1     Running   0          14d     10.244.2.27   k8s-node3              
helloworld011-5cf986495c-9xh5d               1/1     Running   0          14d     10.244.1.20   k8s-node2              
innocent-boxer-hello-helm-867bd48ccc-k6jmw   1/1     Running   0          19h     10.244.0.33   k8s-node1              
kube-hpa-demo-d789d89b-9xgdx                 1/1     Running   0          16d     10.244.1.9    k8s-node2              
kube-hpa-demo-d789d89b-pw77j                 1/1     Running   0          16d     10.244.1.8    k8s-node2              
kube-node-6545cf5596-khw4x                   1/1     Running   0          16d     10.244.2.3    k8s-node3              
kube-node-6545cf5596-q7kd7                   1/1     Running   0          16d     10.244.1.5    k8s-node2              
liveness-tcpsocket                           1/1     Running   0          16d     10.244.1.6    k8s-node2              
myrabbitmq-0                                 1/1     Running   0          18h     10.244.2.60   k8s-node3              
mysqldb-6ffc996bdb-lxgsp                     0/1     Pending   0          19h                              
nfs-client-provisioner-84f967cff9-dtxxt      1/1     Running   0          9d      10.244.2.43   k8s-node3              
nfs-web-0                                    1/1     Running   0          9d      10.244.2.44   k8s-node3              
nfs-web-1                                    1/1     Running   0          9d      10.244.1.22   k8s-node2              
nginx-ds-7spc2                               1/1     Running   0          15d     10.244.1.12   k8s-node2              
nginx-ds-ffd9v                               1/1     Running   0          15d     10.244.2.13   k8s-node3              
nginx-ds-rmqvl                               1/1     Running   0          5d23h   10.244.0.7    k8s-node1              
static-nginx-k8s-node1                       1/1     Running   0          16d     10.244.0.2    k8s-node1              
use-pvc-deployment-85997f976b-llg9h          1/1     Running   0          9d      10.244.2.42   k8s-node3              
use-pvc-deployment-85997f976b-tm92t          1/1     Running   0          9d      10.244.1.21   k8s-node2              
web-0                                        1/1     Running   0          15d     10.244.2.25   k8s-node3              
web-1                                        1/1     Running   0          15d     10.244.1.19   k8s-node2              

发现不在k8s-node2上部署,在k8s-node3上部署

部署规则包括以下几种:

In:label 的值在某个列表中
NotIn:label 的值不在某个列表中
Gt:label 的值大于某个值
Lt:label 的值小于某个值
Exists:某个 label 存在
DoesNotExist:某个 label 不存在

3、pod亲和性和反亲和性
PodAffinity和podAntiAffinity参考
https://www.qikqiak.com/k8s-book/docs/51.Kubernetes%E4%BA%B2%E5%92%8C%E6%80%A7%E8%B0%83%E5%BA%A6.html

你可能感兴趣的:(k8s,亲和性调度,nodeAffinity,podAffinity,podAntiAffinity,kubernetes)