Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性

互联网 2022/1/17 7:08:48

## Pod调度 - 亲和podAffinity### 硬亲和requiredDuringSchedulingIgnoredDuringExecution```sh [15:25:06 root@master1 scheduler]#cat 07-scheduler-pod-affinity-base.yaml --- apiVersion: v1 kind: Pod metadata:name: pod-devlabels:env: dev spec:containers:- n…
## Pod调度 - 亲和podAffinity

### 硬亲和requiredDuringSchedulingIgnoredDuringExecution

```sh
[15:25:06 root@master1 scheduler]#cat 07-scheduler-pod-affinity-base.yaml 
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-dev
  labels:
    env: dev
spec:
  containers:
  - name: pod-test
    image: 10.0.0.55:80/mykubernetes/pod_test:v0.1
    imagePullPolicy: IfNotPresent
  nodeSelector:
    env: dev
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-test
  labels:
    env: test
spec:
  containers:
  - name: pod-test
    image: 10.0.0.55:80/mykubernetes/pod_test:v0.1
    imagePullPolicy: IfNotPresent
  nodeSelector:
    env: test
[15:25:18 root@master1 scheduler]#kubectl apply -f 07-scheduler-pod-affinity-base.yaml 
pod/pod-dev created
pod/pod-test created
[15:25:27 root@master1 scheduler]#kubectl get pod -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES
pod-dev    1/1     Running   0          5s    10.244.3.10   node1.noisedu.cn   <none>           <none>
pod-test   1/1     Running   0          5s    10.244.4.9    node2.noisedu.cn   <none>           <none>

# 此时建立了两个pod分别有各自的标签,下面根据pod的标签创建基于硬亲和的pod
[15:26:06 root@master1 scheduler]#cat 08-scheduler-pod-affinity-required.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-affinity
spec:
  containers:
  - name: pod-test
    image: 10.0.0.55:80/mykubernetes/pod_test:v0.1
    imagePullPolicy: IfNotPresent
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: env
            operator: In
            values:
            - test
        namespaces: ["default"]
        topologyKey: kubernetes.io/hostname

[15:25:32 root@master1 scheduler]#kubectl apply -f 08-scheduler-pod-affinity-required.yaml 
pod/pod-affinity created
[15:25:40 root@master1 scheduler]#kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES
pod-affinity   1/1     Running   0          4s    10.244.4.10   node2.noisedu.cn   <none>           <none>
pod-dev        1/1     Running   0          17s   10.244.3.10   node1.noisedu.cn   <none>           <none>
pod-test       1/1     Running   0          17s   10.244.4.9    node2.noisedu.cn   <none>           <none>
[15:25:44 root@master1 scheduler]#kubectl get pod -o wide --show-labels 
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES   LABELS
pod-affinity   1/1     Running   0          26s   10.244.4.10   node2.noisedu.cn   <none>           <none>            <none>
pod-dev        1/1     Running   0          39s   10.244.3.10   node1.noisedu.cn   <none>           <none>            env=dev
pod-test       1/1     Running   0          39s   10.244.4.9    node2.noisedu.cn   <none>           <none>            env=test





```

### 软亲和preferredDuringSchedulingIgnoredDuringExecution

```sh
# 多满足条件,选择权重高的node
[15:30:53 root@master1 scheduler]#cat 09-scheduler-pod-affinity-preferred.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-affinity
spec:
  containers:
  - name: pod-test
    image: 10.0.0.55:80/mykubernetes/pod_test:v0.1
    imagePullPolicy: IfNotPresent
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 60
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - {key: env, operator: In, values: ["dev"]}
          topologyKey: kubernetes.io/hostname
      - weight: 30
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - {key: env, operator: In, values: ["test"]}
          topologyKey: kubernetes.io/hostname
[15:31:26 root@master1 scheduler]#kubectl apply -f 09-scheduler-pod-affinity-preferred.yaml 
pod/pod-affinity created
[15:32:32 root@master1 scheduler]#kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE     IP            NODE               NOMINATED NODE   READINESS GATES
pod-affinity   1/1     Running   0          9s      10.244.3.11   node1.noisedu.cn   <none>           <none>
pod-dev        1/1     Running   0          7m14s   10.244.3.10   node1.noisedu.cn   <none>           <none>
pod-test       1/1     Running   0          7m14s   10.244.4.9    node2.noisedu.cn   <none>           <none>

# 这种方式,一般会适用于一些服务和后台存储必须放在一起
```

## Pod调度 - 反亲和podAntiAffinity

```sh
[15:45:56 root@master1 scheduler]#cat 12-scheduler-pod-anaffinity-required.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-antiaffinity
spec:
  containers:
  - name: pod-test
    image: 10.0.0.55:80/mykubernetes/pod_test:v0.1
    imagePullPolicy: IfNotPresent
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - {key: env, operator: In, values: ["dev"]}
        topologyKey: kubernetes.io/hostname
[15:46:08 root@master1 scheduler]#kubectl apply -f 12-scheduler-pod-anaffinity-required.yaml 
pod/pod-antiaffinity created
[15:46:31 root@master1 scheduler]#kubectl get pod -o wide
NAME               READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES
pod-antiaffinity   1/1     Running   0          5s    10.244.4.11   node2.noisedu.cn   <none>           <none>
pod-dev            1/1     Running   0          21m   10.244.3.10   node1.noisedu.cn   <none>           <none>
pod-test           1/1     Running   0          21m   10.244.4.9    node2.noisedu.cn   <none>           <none>
[15:46:36 root@master1 scheduler]#kubectl get pod -o wide --show-labels 
NAME               READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES   LABELS
pod-antiaffinity   1/1     Running   0          24s   10.244.4.11   node2.noisedu.cn   <none>           <none>            <none>
pod-dev            1/1     Running   0          21m   10.244.3.10   node1.noisedu.cn   <none>           <none>            env=dev
pod-test           1/1     Running   0          21m   10.244.4.9    node2.noisedu.cn   <none>           <none>            env=test

# 因为是反亲和性,所以会调度到pod-test的相应节点
```

  

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性]http://www.zyiz.net/tech/detail-286372.html

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>