k8s亲和性调度-nodeAffinity

简介nodeAffinity就是节点亲和性,相对应的是Anti-Affinity,就是反亲和性 。

k8s亲和性调度-nodeAffinity

文章插图
【k8s亲和性调度-nodeAffinity】 
这种方法比nodeSelector灵活,可以有一些简单的逻辑组合 。
调度可以分成软策略和硬策略两种方式:
  • 软策略就是如果你没有满足调度要求的节点的话,POD 就会忽略这条规则,继续完成调度过程 。
  • 没有满足条件的节点的话,就不断重试直到满足条件为止 。

k8s亲和性调度-nodeAffinity

文章插图
 
例子apiVersion: Apps/v1kind: Deploymentmetadata:labels:app: hostnamename: hostnamenamespace: defaultspec:progressDeadlineSeconds: 600replicas: 2revisionHistoryLimit: 10selector:matchLabels:app: hostnamestrategy:rollingUpdate:maxSurge: 10%maxUnavailable: 1type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:app: hostnamespec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kube.NETes.io/hostnameoperator: Invalues:- k8s-node.example.com- centos7.example.compreferredDuringSchedulingIgnoredDuringExecution:- weight: 1preference:matchExpressions:- key: node-typeoperator: Invalues:- testingcontainers:- image: tosomeone/hostname:v1.0.3imagePullPolicy: IfNotPresentresources:requests:memory: 20Micpu: 100mlimits:memory: 30Micpu: 150mlivenessProbe:failureThreshold: 5httpGet:path: /port: 8080scheme: HTTPinitialDelaySeconds: 1periodSeconds: 2successThreshold: 1timeoutSeconds: 2name: hostnameports:- containerPort: 8080protocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /port: 8080scheme: HTTPinitialDelaySeconds: 2periodSeconds: 1successThreshold: 1timeoutSeconds: 5lifecycle:postStart:exec:command:- echo- "started"preStop:exec:command:- sleep- "3"terminationMessagePath: /dev/termination-logterminationMessagePolicy: FileDNSPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30
preferredDuringSchedulingIgnoredDuringExecution:指定的就是软策略
requiredDuringSchedulingIgnoredDuringExecution:指定的就是硬策略
例子中优先选择node上有node-type=testing标签的node,且这些node需要满足主机名k8s-node.example.com或者centos7.example.com
操作符有下面的几种:
  • In:label 的值在某个列表中
  • NotIn:label 的值不在某个列表中
  • Gt:label 的值大于某个值
  • Lt:label 的值小于某个值
  • Exists:某个 label 存在
  • DoesNotExist:某个 label 不存在




    推荐阅读