文章目录
- 一、概念
- 二、实操节点亲和力1
- 三、实操pod亲和力2
一、概念
-
节点亲和力概念(反亲和力相反)
ps:官方文档http://kubernetes.p2hp.com/docs/concepts/scheduling-eviction/assign-pod-node.html
节点亲和力(Node Affinity)是在 Pod 上定义的一组约束,用于确定哪些节点适合进行调度,即使用亲和性规则为 Pod 的节点分配定义硬性要求和软性要求。类似于nodeSelector,你可以根据节点上的标签来约束Pod可以调度到哪些节点上。节点亲和性有两种:
1)requiredDuringSchedulingIgnoredDuringExecution(硬亲和力):调度器只有在规则被满足的时候才能执行调度。 类似于nodeselector ,但其语法表达能力更强。
2)preferredDuringSchedulingIgnoredDuringExecution(软亲和力):调度器会尝试寻找满足对应规则的节点。如果找不到匹配的节点,调度器仍然会调度该Pod。优选尽量满足匹配条件的节点。 -
亲和力匹配条件
ln:满足一个就行 NotIn:一个都不能满足,反亲和性 Exists:只要存在,就满足 DoesNotExist:只有不存在。才满足 Gt:必须要大于节点上的数值才满足 Lt:必须小于节点上的数值才满足
二、实操节点亲和力1
-
给node2打标签:kubectl label no worker2 nginx=permit
-
创建nginx的deploy,设置节点亲和: vi nginx-deployment
apiVersion: apps/v1 kind: Deployment metadata:name: nginx-deployment spec:replicas: 3selector:matchLabels:app: nginx-podtemplate:metadata:labels:app: nginx-podspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: nginxoperator: Invalues:- permitcontainers:- name: nginx-containerimage: nginx:latestports:- name: nginx-portcontainerPort: 80protocol: TCP
-
查看pod:kubectl get pod -o wide
三、实操pod亲和力2
ps:根据节点上是否存在指定标签的pod,来判断pod是否调度到该节点
-
创建nginx的deploy,设置节点亲和: vi /opt/nginx_podaffinity.yaml
ps:将pod节点部署到拥有app=nginx-pod的pod的节点上
apiVersion: apps/v1 kind: Deployment metadata:name: nginx-podaffinity spec:replicas: 3selector:matchLabels:app: nginx-podaffinitytemplate:metadata:labels:app: nginx-podaffinityspec:affinity:podAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- nginx-podtopologyKey: kubernetes.io/hostnamecontainers:- name: nginx-containerimage: nginx:latestports:- name: nginx-portcontainerPort: 80protocol: TCP
-
创建pod:kubectl apply -f /opt/nginx_podaffinity.yaml
-
查看:kubectl get pod -o wide
ps:因为只有worker2满足,所以都部署到第二个去了