亲和性分为NODE亲和性和POD亲和性
- requiredDuringSchedulingIgnoredDuringExecution 必须满足的条件
- preferredDuringSchedulingIgnoredDuringExecution 尽量满足的条件
Node亲和性
决定了一个POD能在哪些NODE节点上运行,取决于Node节点设置的标签
spec:affinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution:- preference: {}weight: 100requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: SCoperator: Invalues:- '1'
这里的含义就是这个POD必须运行在一个包含Key为SC并且Value为1的Node节点上.
反过来说就是如果一个Node节点的存在一个Key为SC并且Value为1的标签,那么这个node就能够跑POD.
Pod亲和性
相比较于Node的亲和性只能选择固定的节点,Pod的亲和性是针对运行在node节点上的pod来决定的.
spec:affinity:podAntiAffinity:preferredDuringSchedulingIgnoredDuringExecution:- podAffinityTerm:labelSelector:matchExpressions:- key: appoperator: Invalues:- nginxtopologyKey: kubernetes.io/hostnameweight: 100
这里(In)的含义是如果一个node上存在一个 app=nginx 标签的POD,那么后续的POD尽量不要放在这个节点.
NotIn反之:如果一个node上存在app=nginx 标签的POD,那么后续的POD尽量放在这个节点
topologyKey
拓扑域
附录
- nginx#YAML文件
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: nginxname: nginx-deployment-basicnamespace: defaultspec:progressDeadlineSeconds: 600replicas: 1revisionHistoryLimit: 10selector:matchLabels:app: nginxstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:labels:app: nginxspec:containers:- image: 'nginx:1.7.9'imagePullPolicy: IfNotPresentname: nginxports:- containerPort: 80protocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /port: 80scheme: HTTPinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 1resources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30
