Linux - K8S - 调度策略 - Pod策略之亲和和反亲和性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | ## 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的相应节点 ``` |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | # 软反亲和 [22:09:48 root@master1 scheduler] #cat 13-scheduler-pod-anaffinity-preferred.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: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - {key: env , operator: In, values: [ "dev" ]} topologyKey: kubernetes.io /hostname - weight: 50 podAffinityTerm: labelSelector: matchExpressions: - {key: env , operator: In, values: [ "test" ]} topologyKey: kubernetes.io /hostname [22:09:51 root@master1 scheduler] #kubectl apply -f 13-scheduler-pod-anaffinity-preferred.yaml pod /pod-antiaffinity created [22:10:03 root@master1 scheduler] #kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-antiaffinity 0 /1 ContainerCreating 0 5s <none> node2.noisedu.cn <none> <none> pod-dev 1 /1 Running 0 21s 10.244.3.3 node1.noisedu.cn <none> <none> pod- test 1 /1 Running 0 21s 10.244.4.3 node2.noisedu.cn <none> <none> [22:10:08 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 12s 10.244.4.4 node2.noisedu.cn <none> <none> pod-dev 1 /1 Running 0 28s 10.244.3.3 node1.noisedu.cn <none> <none> pod- test 1 /1 Running 0 28s 10.244.4.3 node2.noisedu.cn <none> <none> [22:10:15 root@master1 scheduler] #kubectl describe pod pod-antiaffinity Name: pod-antiaffinity Namespace: default Priority: 0 Node: node2.noisedu.cn /10 .0.0.54 Start Time: Sun, 16 Jan 2022 22:10:03 +0800 Labels: <none> Annotations: cni.projectcalico.org /containerID : 62ba17bfd69068d6c93cb2ca894749e93af745193ecb0f7bddc125847567894f cni.projectcalico.org /podIP : 10.244.4.4 /32 cni.projectcalico.org /podIPs : 10.244.4.4 /32 Status: Running IP: 10.244.4.4 IPs: IP: 10.244.4.4 Containers: pod- test : Container ID: docker: //3b8321e664753a0c54fbc74b903cd7c0614e03d44e33618b3998b3d119b9fbf1 Image: 10.0.0.55:80 /mykubernetes/pod_test :v0.1 Image ID: docker-pullable: //10 .0.0.55:80 /mykubernetes/pod_test @sha256:54402cda2ef15f45e4aafe98a5e56d4de076e3d4100c2a1bf1b780c787372fed Port: <none> Host Port: <none> State: Running Started: Sun, 16 Jan 2022 22:10:08 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes .io /serviceaccount from kube-api-access-vclbc (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: kube-api-access-vclbc: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: true QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io /not-ready :NoExecute op =Exists for 300s node.kubernetes.io /unreachable :NoExecute op =Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 35s default-scheduler Successfully assigned default /pod-antiaffinity to node2.noisedu.cn Normal Pulled 30s kubelet Container image "10.0.0.55:80/mykubernetes/pod_test:v0.1" already present on machine Normal Created 30s kubelet Created container pod- test Normal Started 30s kubelet Started container pod- test # 由于test权重低,所以调度到node2上面 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具