Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性
2022/1/17 7:08:48
本文主要是介绍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策略之亲和和反亲和性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20/kubernetes 1.32版本更新解读:新特性和变化一目了然
- 2024-12-19拒绝 Helm? 如何在 K8s 上部署 KRaft 模式 Kafka 集群?
- 2024-12-16云原生周刊:Kubernetes v1.32 正式发布
- 2024-12-13Kubernetes上运行Minecraft:打造开发者平台的例子
- 2024-12-12深入 Kubernetes 的健康奥秘:探针(Probe)究竟有多强?
- 2024-12-10运维实战:K8s 上的 Doris 高可用集群最佳实践
- 2024-12-022024年最好用的十大Kubernetes工具
- 2024-12-02OPA守门人:Kubernetes集群策略编写指南
- 2024-11-26云原生周刊:K8s 严重漏洞
- 2024-11-15在Kubernetes (k8s) 中搭建三台 Nginx 服务器怎么实现?-icode9专业技术文章分享