亲和性分为NODE亲和性和POD亲和性
- requiredDuringSchedulingIgnoredDuringExecution 必须满足的条件
- preferredDuringSchedulingIgnoredDuringExecution 尽量满足的条件
Node亲和性
决定了一个POD能在哪些NODE节点上运行,取决于Node节点设置的标签
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference: {}
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: SC
operator: In
values:
- '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: app
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
weight: 100
这里(In)的含义是如果一个node上存在一个 app=nginx
标签的POD,那么后续的POD尽量不要放在这个节点.
NotIn反之:如果一个node上存在app=nginx
标签的POD,那么后续的POD尽量放在这个节点
topologyKey
拓扑域
附录
- nginx#YAML文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx-deployment-basic
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: 'nginx:1.7.9'
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30