1、应用场景
pod和节点间的关系:
- 某些Pod优先选择有ssd=true标签的节点,如果没有在考虑部署到其它节点;
- 某些Pod需要部署在ssd=true和type=physical的节点上,但是优先部署在ssd=true的节点上;
pod和pod间的关系:
- 同一个应用的Pod不同的副本或者同一个项目的应用尽量或必须不部署在同一个节点或者符合某个标签的一类节点上或者不同的区域; #反亲和
- 相互依赖的两个Pod尽量或必须部署在同一个节点上或者同一个域内。 #亲和
2、分类
Affinity亲和力:
- NodeAffinity:节点亲和力/反亲和力
- PodAffinity:Pod 亲和力
- PodAntiAffinity:Pod反亲和力
3、可用率保障形态
通过亲和性和反亲和性可以部署到不同的宿主机、机房或者机柜,保证应用与服务的可用性。
4、亲和力配置
4.1 节点亲和力
#核心参数解释1、requiredDuringSchedulingIgnoredDuringExecution:硬亲和力配置nodeSelectorTerms:节点选择器配置,可以配置多个matchExpressions(满足其一),每个matchExpressions下可以配置多个key、value类型的选择器(都需要满足),其中values可以配置多个(满足其一)2、preferredDuringSchedulingIgnoredDuringExecution:软亲和力配置weight:软亲和力的权重,权重越高优先级越大,范围1-100preference:软亲和力配置项,和weight同级,可以配置多个,matchExpressions和硬亲和力一致 operator:标签匹配的方式In:相当于key = value的形式NotIn:相当于key != value的形式Exists:节点存在label的key为指定的值即可,不能配置values字段DoesNotExist:节点不存在label的key为指定的值即可,不能配置values字段 Gt:大于value指定的值Lt:小于value指定的值
#示例apiVersion: v1 kind: Pod metadata: name: with-node-affinity spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/e2e-az-name operator: In values: - e2e-az1 - az-2 preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: another-node-label-key operator: In values: - another-node-label-value containers: - name: with-node-affinity
4.2 pod亲和力
#核心参数,注意区分是节点还是Pod。labelSelector:Pod选择器配置,可以配置多个matchExpressions:和节点亲和力配置一致operator:配置和节点亲和力一致,但是没有Gt和LttopologyKey:匹配的拓扑域的key,也就是节点上label的key,key和value相同的为同一个域,可以用于标注不同的机房和地区Namespaces: 和哪个命名空间的Pod进行匹配,为空为当前命名空间
apiVersion: v1 kind: Pod metadata: name: with-pod-affinity spec: affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: security operator: In values: - S1 topologyKey: failure-domain.beta.kubernetes.io/zone podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: #只能写一个,重复需要重新划分权重。 matchExpressions: - key: security operator: In values: - S2 namespaces: - default topologyKey: failure-domain.beta.kubernetes.io/zone containers: - name: with-pod-affinity image: nginx
5、示例
5.1 同一个应用分布到不同的节点
#反亲和力示例apiVersion: apps/v1 kind: Deployment metadata: labels: app: must-be-diff-nodes name: must-be-diff-nodes namespace: kube-public spec: replicas: 1 #只有一台机器,所以使用一个副本数 selector: matchLabels: app: must-be-diff-nodes template: metadata: labels: app: must-be-diff-nodes spec: affinity: podAntiAffinity: #反亲和星 requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app #pod的label选择器为must-be-diff-nodes operator: In values: - must-be-diff-nodes topologyKey: kubernetes.io/hostname #拓扑域使用的主机名,只要主机名相同,就是一个相同的域 containers: - image: nginx imagePullPolicy: IfNotPresent name: must-be-diff-nodeskubectl create -f antiaffinity.yaml #应用yaml文件kubectl get po -nkube-public -owide #查看Pod详细信息,发现NODE节点是所在的主机名NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmust-be-diff-nodes-7bdb5cfc9f-lqbjv 1/1 Running 0 53s 10.244.96.137 zhy > >kubectl scale deployment must-be-diff-nodes -n kube-public --replicas=2 #扩副本数后,如果没有合适的机器,会发现po处于penning状态kubectl get po -nkube-public -owide #查看扩容后的状态NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmust-be-diff-nodes-7bdb5cfc9f-4dn96 0/1 Pending 0 3s > > > >must-be-diff-nodes-7bdb5cfc9f-lqbjv 1/1 Running 0 94s 10.244.96.137 zhy > >##describe的详细信息如下
6、拓扑域TopologyKey
6.1 简介
topologyKey:拓扑域,主要针对宿主机,相当于对宿主机进行区域的划分。用label进行判断,不同的key和不同的value是属于不同的拓扑域.
kubectl get nodes zhy --show-labels #可以查看不同节点的label信息
6.2 示例
#我这里只有一台机器,所以只打一个region标签[root@zhy ~/k8s/affinity]# kubectl label nodes zhy region=zhynode/zhy labeled[root@zhy ~/k8s/affinity]# kubectl get nodes zhy --show-labels | grep region region=zhy#配置deploy的yaml文件apiVersion: apps/v1 kind: Deployment metadata: labels: app: must-be-diff-zone name: must-be-diff-zone namespace: kube-public spec: replicas: 2 #这里副本数为2 selector: matchLabels: app: must-be-diff-zone template: metadata: labels: app: must-be-diff-zone spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - must-be-diff-zone topologyKey: region #添加region的域,创建pod会判断两个Pod是不是属于同一个域,如果属于,则不会创建到一起,如果没有合适的节点,pod会处于pending状态 containers: - image: nginx imagePullPolicy: IfNotPresent name: must-be-diff-zone#创建podkubectl create -f topolog.yamlkubectl get pod -nkube-public #查看发现处于pending状态NAME READY STATUS RESTARTS AGEmust-be-diff-zone-99744d496-lf87d 1/1 Running 0 10smust-be-diff-zone-99744d496-wppk2 0/1 Pending 0 10s#查看pod错误信息kubectl describe pod -nkube-public must-be-diff-zone-99744d496-wppk2
来源地址:https://blog.csdn.net/qwerty1372431588/article/details/134013220