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-11-15在Kubernetes (k8s) 中搭建三台 Nginx 服务器怎么实现?-icode9专业技术文章分享
- 2024-11-05基于Kubernetes的自定义AWS云平台搭建指南
- 2024-11-05基于Kubernetes Gateway API的现代流量管理方案
- 2024-11-05在Kubernetes上部署你的第一个应用:Nginx服务器
- 2024-11-05利用拓扑感知路由控制Kubernetes中的流量
- 2024-11-05Kubernetes中的层次命名空间:更灵活的资源管理方案
- 2024-11-055分钟上手 Kubernetes:精简实用的 Kubectl 命令速查宝典!
- 2024-10-30K8s 容器的定向调度与亲和性
- 2024-10-28云原生周刊:K8s未来三大发展方向 丨2024.10.28
- 2024-10-25亚马逊弹性Kubernetes服务(EKS)实战:轻松搭建Kubernetes平台