Kubernetes

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

本文主要是介绍Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
## 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的相应节点
```

  

这篇关于Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!