Pod 亲和性

pod 自身的亲和性调度有两种表示形式

  1. podaffinity:pod 和 pod 更倾向腻在一起,把相近的 pod 结合到相近的位置,如同一区域,同一机架,这样的话 pod 和 pod 之间更好通信,比方说有两个机房,这两个机房部署的集群有 1000 台主机,那么我们希望把
    nginx 和 tomcat 都部署同一个地方的 node 节点上,可以提高通信效率;
  2. podunaffinity:pod 和 pod 更倾向不腻在一起,如果部署两套程序,那么这两套程序更倾向于反亲和性,这样相互之间不会有影响。

第一个 pod 随机选则一个节点,做为评判后续的 pod 能否到达这个 pod 所在的节点上的运行方式,这就称为 pod 亲和性;我们怎么判定哪些节点是相同位置的,哪些节点是不同位置的;我们在定义 pod 亲和性时需要有一个前提,哪些 pod 在同一个位置,哪些 pod 不在同一个位置,这个位置是怎么定义的,标准是什么?以节点名称为标准,这个节点名称相同的表示是同一个位置, 节点名称不相同的表示不是一个位置。

[root@master01 test]# kubectl explain pods.spec.affinity.podAffinity
KIND:     Pod
VERSION:  v1

RESOURCE: podAffinity <Object>

DESCRIPTION:
     Describes pod affinity scheduling rules (e.g. co-locate this pod in the
     same node, zone, etc. as some other pod(s)).

     Pod affinity is a group of inter pod affinity scheduling rules.

FIELDS:
   preferredDuringSchedulingIgnoredDuringExecution      <[]Object>    
     The scheduler will prefer to schedule pods to nodes that satisfy the
     affinity expressions specified by this field, but it may choose a node that
     violates one or more of the expressions. The node that is most preferred is
     the one with the greatest sum of weights, i.e. for each node that meets all
     of the scheduling requirements (resource request, requiredDuringScheduling
     affinity expressions, etc.), compute a sum by iterating through the
     elements of this field and adding "weight" to the sum if the node has pods
     which matches the corresponding podAffinityTerm; the node(s) with the
     highest sum are the most preferred.

   requiredDuringSchedulingIgnoredDuringExecution       <[]Object>
     If the affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     affinity requirements specified by this field cease to be met at some point
     during pod execution (e.g. due to a pod label update), the system may or
     may not try to eventually evict the pod from its node. When there are
     multiple elements, the lists of nodes corresponding to each podAffinityTerm
     are intersected, i.e. all terms must be satisfied.


preferredDuringSchedulingIgnoredDuringExecution   软亲和性
requiredDuringSchedulingIgnoredDuringExecution    硬亲和性

1. pod亲和性

定义两个 pod,第一个 pod 做为基准,第二个 pod 跟着它走

[root@master01 test]# cat pod_podaffinity.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app2: myapp2
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-second
  labels:
    app: backend
    tier: db
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sh","-c","sleep 3600"]
  affinity:
    podAffinity:     #这部分定义了Pod之间的亲和性策略。
       requiredDuringSchedulingIgnoredDuringExecution:   #硬亲和性
       - labelSelector:    #选择具有特定标签的Pod。
            matchExpressions:
            - {key: app2, operator: In, values: ["myapp2"]}
         topologyKey: kubernetes.io/hostname    #指定节点属性键,这个键是节点的名称(node01或者node02)
  
[root@master01 test]# kubectl apply -f pod_podaffinity.yaml
pod/pod-first created
pod/pod-second created
[root@master01 test]# kubectl get pod pod-first pod-second -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
pod-first    1/1     Running   0          62s   10.244.140.102   node02   <none>           <none>
pod-second   1/1     Running   0          62s   10.244.140.101   node02   <none>           <none>
可以看到两个pod调度到了同一个节点上。

2. pod反亲和性

定义两个 pod,第一个 pod 做为基准,第二个 pod 跟它调度节点相反

[root@master01 test]# cat pod_unaffinity.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app1: myapp1
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-second
  labels:
    app: backend
    tier: db
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sh","-c","sleep 3600"]
  affinity:
    podAntiAffinity:      #这部分定义了Pod之间的亲和性策略。
       requiredDuringSchedulingIgnoredDuringExecution:    #硬亲和性
       - labelSelector:   #选择具有特定标签的Pod。
           matchExpressions:
           - {key: app1, operator: In, values: ["myapp1"]}
         topologyKey: kubernetes.io/hostname    #指定节点属性键,这里是节点名称。

[root@master01 test]# kubectl apply -f pod_unaffinity.yaml
pod/pod-first created
[root@master01 test]# kubectl get pod  -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
pod-first    1/1     Running   0          7s    10.244.196.191   node01   <none>           <none>
pod-second   1/1     Running   0          7s    10.244.140.103   node02   <none>           <none>
可以看到两个pod是不在同一个节点的。

3. topologykey的作用

首先给两个node节点打上键和值都一样的标签

[root@master01 test]# kubectl label node node01 test=topologykey
node/node01 labeled
[root@master01 test]# kubectl label node node02 test=topologykey
node/node02 labeled

先创建第一个pod:

[root@master01 test]# cat pod-first-required-anti-affinity-demo-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app3: myapp3
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1

[root@master01 test]# kubectl apply -f pod-first-required-anti-affinity-demo-1.yaml
pod/pod-first configured
[root@master01 test]# kubectl get pod  pod-first
NAME        READY   STATUS    RESTARTS   AGE
pod-first   1/1     Running   0          4h48m

再创建第二个反亲和性的pod:

[root@master01 test]# cat pod-second-required-anti-affinity-demo-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-second
  labels:
    app: backend
    tier: db
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app3
            operator: In
            values:
            - myapp3
        topologyKey: test
[root@master01 test]# kubectl apply -f pod-second-required-anti-affinity-demo-1.yaml
pod/pod-second created

[root@master01 test]# kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
pod-first    1/1     Running   0          10s
pod-second   0/1     Pending   0          4s
第二个pod现在是 pending,因为两个节点是同一个位置,现在没有不是同一个位置的了,
而且我们要求反亲和性,所以就会处于 pending 状态,如果在反亲和性这个位置把 required 改成preferred,那么也会运行。 因为required是必须性的。

topologyKey作用:

  1. 节点标签:节点必须具有 test 这个标签键,并且具有具体的值(例如 test:dev 或 test:pro)。
  2. 反亲和性:此配置示例展示了反亲和性,即带有相同应用标签的 Pod 不会被调度到具有相同 test 标签的节点上。
  3. 亲和性:如果使用的是亲和性配置,则带有相同应用标签的 Pod 将被尽可能调度到具有相同 test 标签的节点上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峰峰--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值