Kubernetes-Affinity(node亲和、pod亲和)

发布时间:2024年01月24日

目录

一、节点亲和性

(1)节点硬亲和

(2)节点软亲和

(3)软硬亲和共同使用

二、Pod亲和

1.Pod亲和性

2.Pod反亲和性

三、亲和性/反亲和性调度策略比较


一、节点亲和性

pod.spec.nodeAffinity

????????preferredDuringSchedulingIgnoredDuringExecution:软策略

????????requiredDuringSchedulingIgnoredDuringExecution:硬策略

键值运算关系:

In:label 的值在某个列表中

NotIn:label 的值不在某个列表中

Gt:label 的值大于某个值

Lt:label 的值小于某个值

Exists:某个 label 存在

DoesNotExist:某个 label 不存在

<!--如果`nodeSelectorTerms`下面有多个选项的话,满足任何一个条件就可以了;
如果`matchExpressions`有多个选项的话,则必须同时满足这些条件才能正常调度 POD-->

(1)节点硬亲和

requiredDuringSchedulingIgnoredDuringExecution(硬亲和)

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: node-affinity
    image: centos:7.9.2009
  affinity:			#亲和性
    nodeAffinity:	#节点亲和性
      requiredDuringSchedulingIgnoredDuringExecution:		#硬性限制
        nodeSelectorTerms:	#节点的选择器限制
        - matchExpressions:	#匹配运算符
          - key: kubernetes.io/hostname
            operator: NotIn	#不在列表
            values:
            - k8s-node02	#不匹配node02节点(不将此pod调度到k8s-node02节点)

节点硬亲和实验:

vim node_required_affinity.yaml

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: node-affinity
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s-node02

kubectl create -f node_required_affinity.yaml
    #创建pod

kubectl get pod -o wide
    #查看新建的pod调度到哪个节上

vim node_required_affinity.yaml
    #修改operator:NotIn改为In
	#values改为k8s-node03
apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: node-affinity
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: In
            values:
            - k8s-node03

kubectl delete -f node_required_affinity.yaml
    #删除刚刚创建的pod

kubectl apply -f node_required_affinity.yaml
    #根据更改后的yaml文件,再次创建pod

kubectl get pod -o wide
    #没有node03节点,硬亲和匹配不到,所以pod一直pending。

(2)节点软亲和

preferredDuringSchedulingIgnoredDuringExecution(软亲和)

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: node-affinity
    image: nginx:latest
  affinity:			#亲和性
    nodeAffinity:	#节点亲和性
      preferredDuringSchedulingIgnoredDuringExecution:		#软性亲和
      - weight: 1		#权重
        preference:
          matchExpressions:
          - key: kubernetes.io/hostname
            operator: In		#因为没有三节点,所以软亲和会在从节点1或2上选
            values:
            - k8s-node02

节点软亲和实验:

vim node_preferred_affinity.yaml

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: kubernetes.io/hostname
            operator: In    #匹配规则IN
            values:
            - k8s-node02    #优先匹配k8s-node02节点

kubectl create -f node_preferred_affinity.yaml

kubectl get pod -o wide
    #查看pod调度的节点

kubectl delete -f node_preferred_affinity.yaml

vim node_preferred_affinity.yaml
    #修改k8s-node02为k8s-node03
apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: kubernetes.io/hostname
            operator: In    #匹配规则IN
            values:
            - k8s-node03    #优先匹配k8s-node03节点,如果没有node03节点,会在node01和02之间随机选择。

kubectl create -f node_preferred_affinity.yaml

kubectl get pod -o wide
    #查看pod调度的节点

(3)软硬亲和共同使用

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: node-affinity
    image: nginx:latest
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:		#硬亲和
        nodeSelectorTerms:		#节点选择器
        - matchExpressions:		#匹配运算符
          - key: kubernetes.io/hostname
            operator: NotIn		#除了node02节点,其他都可以运行
            values:
            - k8s-node02
      preferredDuringSchedulingIgnoredDuringExecution:		#软亲和
      - weight: 1
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - aaa    #除了node02节点不匹配,优先匹配带有aaa标签的节点。

二、Pod亲和

1.Pod亲和性

pod.spec.affinity.podAffinity/podAntiAffinity

????????preferredDuringSchedulingIgnoredDuringExecution:软策略

????????requiredDuringSchedulingIgnoredDuringExecution:硬策略

apiVersion: v1
kind: Pod
metadata:
  name: pod-3
  labels:
    app: pod-3
spec:
  containers:
  - name: pod-3
    image: nginx:latest
  affinity:
    podAffinity:		#pod亲和性
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In		#在列表
            values:
            - pod-1		#pod-1在哪个节点,就匹配到哪个节点。
        topologyKey: kubernetes.io/hostname	
#是否在一起,根据这个topologyKey决定,他的values只要相同,那么就认为是在一起的
    podAntiAffinity:		#反亲和性
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - pod-2
          topologyKey: kubernetes.io/hostname

pod亲和性实验:

vim pod_affinity.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-affinity
  labels:
    app: pod-affinity
spec:
  containers:
  - name: pod-affinity
    image: nginx:latest
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - pod-1
        topologyKey: kubernetes.io/hostname

kubectl create -f pod_affinity.yaml

kubectl get pod -o wide
    #必须得有一个具备此标签pod才可以,现在没有,所以是Pending状态

vim pod-1.yaml
    #创建一个带有pod-1标签的pod
apiVersion: v1
kind: Pod
metadata:
  name: pod
  labels:
    app: pod-1
spec:
  containers:
  - name: pod-1
    image: nginx:latest
    imagePullPolicy: IfNotPresent

kubectl create -f pod-1.yaml
    #创建一个带有pod-1标签的pod

kubectl get pod -o wide
    #pod-affinity找到了匹配的标签,达到running状态。

kubectl get pod --show-labels
    #查看pod的标签

2.Pod反亲和性

apiVersion: v1
kind: Pod
metadata:
  name: pod-antiaffinity
  labels:
    app: pod-antiaffinity
spec:
  containers:
  - name: pod-antiaffinity
    image: nginx:latest
  affinity:
    podAntiAffinity:		#反亲和性
      preferredDuringSchedulingIgnoredDuringExecution:    #软限制策略
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In    #在列表
              values:
              - pod-2         #匹配pod-2
          topologyKey: kubernetes.io/hostname
#由于是反亲和,根据上述配置,软限制,优先选择pod-2不在的节点运行。

pod反亲和实验:

vim pod_antiaffinity.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-antiaffinity
  labels:
    app: pod-antiaffinity
spec:
  containers:
  - name: pod-antiaffinity
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In    #在列表
              values:
              - pod-1         
          topologyKey: kubernetes.io/hostname
#最终pod优先不与pod-1标签的pod在一个节点

kubectl create -f pod_antiaffinity.yaml

kubectl get pod -o wide
    #查看pod的运行节点

三、亲和性/反亲和性调度策略比较

调度策略

匹配标签

操作符

拓扑域支持

调度目标

nodeAffinity

主机

In, NotIn, Exists, DoesNotExist, Gt, Lt

指定主机

podAffinity

POD

In, NotIn, Exists, DoesNotExist

POD与指定POD同一拓扑域

podAnitAffinity

POD

In, NotIn, Exists, DoesNotExist

POD与指定POD不在同一拓扑域

文章来源:https://blog.csdn.net/m0_73562288/article/details/135825412
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。