赞
踩
nodeSelector 提供了一种最简单的方法来将 Pod 约束到具有特定标签的节点上。 亲和性和反亲和性扩展了你可以定义的约束类型。使用亲和性与反亲和性的一些好处有:
亲和性功能由两种类型的亲和性组成:
节点亲和性概念上类似于 nodeSelector, 它使你可以根据节点上的标签来约束 Pod 可以调度到哪些节点上。 节点亲和性有两种:
spec: template: spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: # 硬匹配 nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os # 匹配的key operator: In # 匹配的操作,In代表在 values: - linux # 匹配的值 preferredDuringSchedulingIgnoredDuringExecution: # 软匹配 - weight: 1 # 匹配的权重,数值越大,权重越高 preference: matchExpressions: - key: label-1 # 匹配的key operator: In values: - key-1 # 匹配的值 - weight: 50 preference: matchExpressions: - key: label-2 operator: In values: - key-2
[root@k8s-master ~]# kubectl label nodes k8s-node-01 label-1=key-1
node/k8s-node-01 labeled
[root@k8s-master ~]# kubectl label nodes k8s-node-02 label-2=key-2
node/k8s-node-02 labeled
由于requiredDuringSchedulingIgnoredDuringExecution 硬匹配都可以匹配到,但是 在 preferredDuringSchedulingIgnoredDuringExecution 软匹配的时候,node2的亲和力比node1的高,所以pod都跑到了node2上。
Pod 间亲和性与反亲和性使你可以基于已经在节点上运行的 Pod 的标签来约束 Pod 可以调度到的节点,而不是基于节点上的标签。
[root@k8s-master ~]# kubectl label nodes k8s-node-01 k8s-node-02 topology.kubernetes.io/zone=V
node/k8s-node-01 labeled
node/k8s-node-02 labeled
[root@k8s-master ~]# kubectl label nodes k8s-master topology.kubernetes.io/zone=R
node/k8s-master labeled
[root@k8s-master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 9d v1.25.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=,topology.kubernetes.io/zone=R
k8s-node-01 Ready <none> 9d v1.25.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-01,kubernetes.io/os=linux,label-1=key-1,topology.kubernetes.io/zone=V,type=microsvc
k8s-node-02 Ready <none> 7d1h v1.25.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-02,kubernetes.io/os=linux,label-2=key-2,topology.kubernetes.io/zone=V,type=microsvc
apiVersion: apps/v1 # deployment api 版本 kind: Deployment # 资源类型为 deployment metadata: # 元信息 labels: # 标签 app: nginx-deploy # 具体的 key: value 配置形式 name: nginx-deploy-s1 # deployment 的名字 namespace: default # 所在的命名空间 spec: replicas: 2 # 期望副本数 revisionHistoryLimit: 10 # 进行滚动更新后,保留的历史版本数 selector: # 选择器,用于找到匹配的 RS matchLabels: # 按照标签匹配 app: nginx-deploy # 匹配的标签key/value strategy: # 更新策略 rollingUpdate: # 滚动更新配置 maxSurge: 25% # 进行滚动更新时,更新的个数最多可以超过期望副本数的个数/比例 maxUnavailable: 25% # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功 type: RollingUpdate # 更新类型,采用滚动更新 template: # pod 模板 metadata: # pod 的元信息 labels: # pod 的标签 app: nginx-deploy # topology.kubernetes.io/zone: V security: S1 spec: # pod 期望信息 containers: # pod 的容器 - image: nginx:1.20 # 镜像 imagePullPolicy: IfNotPresent # 拉取策略 name: nginx # 容器名称 resources: limits: cpu: 200m memory: 128Mi requests: cpu: 100m memory: 128Mi restartPolicy: Always # 重启策略 terminationGracePeriodSeconds: 30 # 删除操作最多宽限多长时间
[root@k8s-master affinity]# kubectl create -f s1-nginx-deploy-affinity.yaml
deployment.apps/nginx-deploy-s1 created
[root@k8s-master affinity]# kubectl get po -owide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
dns-test 1/1 Running 2 (2d12h ago) 4d21h 10.2.1.58 k8s-node-02 <none> <none> run=dns-test
fluentd-59k8k 1/1 Running 1 (2d12h ago) 4d3h 10.2.2.34 k8s-node-01 <none> <none> app=logging,controller-revision-hash=7555d95dc,id=fluentd,pod-template-generation=2
fluentd-hhtls 1/1 Running 1 (2d12h ago) 4d3h 10.2.1.59 k8s-node-02 <none> <none> app=logging,controller-revision-hash=7555d95dc,id=fluentd,pod-template-generation=2
nginx-deploy-579987f57f-ccgd8 1/1 Running 0 51m 10.2.2.113 k8s-node-01 <none> <none> app=nginx-deploy,pod-template-hash=579987f57f
nginx-deploy-579987f57f-hwpjk 1/1 Running 0 51m 10.2.2.112 k8s-node-01 <none> <none> app=nginx-deploy,pod-template-hash=579987f57f
nginx-deploy-s1-5cc987b754-82xt5 1/1 Running 0 26s 10.2.1.92 k8s-node-02 <none> <none> app=nginx-deploy,pod-template-hash=5cc987b754,security=S1
nginx-deploy-s1-5cc987b754-kwsk5 1/1 Running 0 26s 10.2.0.7 k8s-master <none> <none> app=nginx-deploy,pod-template-hash=5cc987b754,security=S1
apiVersion: apps/v1 # deployment api 版本 kind: Deployment # 资源类型为 deployment metadata: # 元信息 labels: # 标签 app: nginx-deploy # 具体的 key: value 配置形式 name: nginx-deploy-s2 # deployment 的名字 namespace: default # 所在的命名空间 spec: replicas: 1 # 期望副本数 revisionHistoryLimit: 10 # 进行滚动更新后,保留的历史版本数 selector: # 选择器,用于找到匹配的 RS matchLabels: # 按照标签匹配 app: nginx-deploy # 匹配的标签key/value strategy: # 更新策略 rollingUpdate: # 滚动更新配置 maxSurge: 25% # 进行滚动更新时,更新的个数最多可以超过期望副本数的个数/比例 maxUnavailable: 25% # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功 type: RollingUpdate # 更新类型,采用滚动更新 template: # pod 模板 metadata: # pod 的元信息 labels: # pod 的标签 app: nginx-deploy # topology.kubernetes.io/zone: V security: S2 spec: # pod 期望信息 nodeSelector: kubernetes.io/hostname: k8s-master containers: # pod 的容器 - image: nginx:1.20 # 镜像 imagePullPolicy: IfNotPresent # 拉取策略 name: nginx # 容器名称 resources: limits: cpu: 200m memory: 128Mi requests: cpu: 100m memory: 128Mi restartPolicy: Always # 重启策略 terminationGracePeriodSeconds: 30 # 删除操作最多宽限多长时间
[root@k8s-master affinity]# kubectl create -f s2-nginx-deploy-affinity.yaml
deployment.apps/nginx-deploy-s2 created
[root@k8s-master affinity]# kubectl get po -owide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
dns-test 1/1 Running 2 (2d12h ago) 4d21h 10.2.1.58 k8s-node-02 <none> <none> run=dns-test
fluentd-59k8k 1/1 Running 1 (2d12h ago) 4d3h 10.2.2.34 k8s-node-01 <none> <none> app=logging,controller-revision-hash=7555d95dc,id=fluentd,pod-template-generation=2
fluentd-hhtls 1/1 Running 1 (2d12h ago) 4d3h 10.2.1.59 k8s-node-02 <none> <none> app=logging,controller-revision-hash=7555d95dc,id=fluentd,pod-template-generation=2
nginx-deploy-579987f57f-ccgd8 1/1 Running 0 55m 10.2.2.113 k8s-node-01 <none> <none> app=nginx-deploy,pod-template-hash=579987f57f
nginx-deploy-579987f57f-hwpjk 1/1 Running 0 56m 10.2.2.112 k8s-node-01 <none> <none> app=nginx-deploy,pod-template-hash=579987f57f
nginx-deploy-s1-5cc987b754-82xt5 1/1 Running 0 5m19s 10.2.1.92 k8s-node-02 <none> <none> app=nginx-deploy,pod-template-hash=5cc987b754,security=S1
nginx-deploy-s1-5cc987b754-kwsk5 1/1 Running 0 5m19s 10.2.0.7 k8s-master <none> <none> app=nginx-deploy,pod-template-hash=5cc987b754,security=S1
nginx-deploy-s2-656bfc8d9c-qpzl4 1/1 Running 0 3s 10.2.0.8 k8s-master <none> <none> app=nginx-deploy,pod-template-hash=656bfc8d9c,security=S2
apiVersion: apps/v1 # deployment api 版本 kind: Deployment # 资源类型为 deployment metadata: # 元信息 labels: # 标签 app: nginx-deploy # 具体的 key: value 配置形式 name: nginx-deploy-test # deployment 的名字 namespace: default # 所在的命名空间 spec: replicas: 2 # 期望副本数 revisionHistoryLimit: 10 # 进行滚动更新后,保留的历史版本数 selector: # 选择器,用于找到匹配的 RS matchLabels: # 按照标签匹配 app: nginx-deploy # 匹配的标签key/value strategy: # 更新策略 rollingUpdate: # 滚动更新配置 maxSurge: 25% # 进行滚动更新时,更新的个数最多可以超过期望副本数的个数/比例 maxUnavailable: 25% # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功 type: RollingUpdate # 更新类型,采用滚动更新 template: # pod 模板 metadata: # pod 的元信息 labels: # pod 的标签 app: nginx-deploy spec: # pod 期望信息 affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: security operator: In values: - S1 topologyKey: topology.kubernetes.io/zone podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: security operator: In values: - S2 topologyKey: topology.kubernetes.io/zone containers: # pod 的容器 - image: nginx:1.20 # 镜像 imagePullPolicy: IfNotPresent # 拉取策略 name: nginx # 容器名称 resources: limits: cpu: 200m memory: 128Mi requests: cpu: 100m memory: 128Mi restartPolicy: Always # 重启策略 terminationGracePeriodSeconds: 30 # 删除操作最多宽限多长时间
[root@k8s-master affinity]# kubectl create -f nginx-affinity-deploy.yaml
deployment.apps/nginx-deploy-test created
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。