1,亲和性分为软亲和性以及硬亲和性

preferredDuringSchedulingIgnoredDuringExecution:软策略 可以不再最好在
requiredDuringSchedulingIgnoredDuringExecution:硬策略 必须在
键值运算关系
In:label 的值在某个列表中
NotIn:label 的值不在某个列表中
Gt:label 的值大于某个值
Lt:label 的值小于某个值
Exists:某个 label 存在
DoesNotExist:某个 label 不存在

2,node亲和性

apiVersion: v1
kind: Pod
metadata:
name: affinity
labels:
app: node-affinity-pod
spec:
containers:

  • name: with-node-affinity
    image: hub.atguigu.com/library/myapp:v1
    affinity: #亲和性
    nodeAffinity: #node的亲和性
    requiredDuringSchedulingIgnoredDuringExecution: #硬亲和性
    nodeSelectorTerms: #node选择方案
    - matchExpressions: #
    - key: kubernetes.io/hostname #key是node简名节点标签.hostname
    operator: NotIn #不是,排除
    values:
    - k8s-node02 #主机名不是k8s-node02就可以

apiVersion: v1
kind: Pod
metadata:
name: affinity
labels:
app: node-affinity-pod
spec:
containers:

  • name: with-node-affinity
    image: hub.atguigu.com/library/myapp:v1
    affinity:
    nodeAffinity:
    preferredDuringSchedulingIgnoredDuringExecution: #软亲和性
    • weight: 1 #权重.如果有多个亲和性权重高的优先
      preference: #偏好详细信息
      matchExpressions: #匹配表达式
      • key: kubernetes.io/hostname # key是node简名节点标签
        operator: In #要在
        values:
        • k8s-node03 #主机名是k8s-node03最好

3,Pod 亲和性

pod.spec.affinity.podAffinity/podAntiAffinity
preferredDuringSchedulingIgnoredDuringExecution:软策略
requiredDuringSchedulingIgnoredDuringExecution:硬策略

apiVersion: v1
kind: Pod
metadata:
name: pod-3
labels:
app: pod-3 #pod标签
spec:
containers:

  • name: pod-3
    image: hub.atguigu.com/library/myapp:v1
    affinity:
    podAffinity: #pod亲和性
    requiredDuringSchedulingIgnoredDuringExecution: #硬策略
    • labelSelector: #便签选择
      matchExpressions:
      • key: app #当app有没有一个APP标签是pod-1的就是pod的标签
        operator: In #在
        values:
        • pod-1 #pod-1的
          topologyKey: kubernetes.io/hostname #同一个node节点 #app在哪里就去那个机器创建
          podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
    • weight: 1
      podAffinityTerm:
      labelSelector:
      matchExpressions:
      - key: app
      operator: In
      values:
      - pod-2
      topologyKey: kubernetes.io/hostname

调度策略 匹配标签 操作符 拓 扑域支持 调度目标
nodeAffinity 主机 In, NotIn, Exists,DoesNotExist, Gt, Lt 否 指定主机
podAffinity POD In , NotIn, Exists,DoesNotExist 是 POD与指定POD同一拓扑域
podAnitAffinity POD In, NotIn, Exists,DoesNotExist 是 POD与指定POD不在同一拓扑域

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐