簡介
nodeAffinity就是節點親和性,相對應的是Anti-Affinity,就是反親和性。
這種方法比nodeSelector靈活,可以有一些簡單的邏輯組合。
調度可以分成軟策略和硬策略兩種方式:
- 軟策略就是如果你沒有滿足調度要求的節點的話,POD 就會忽略這條規則,繼續完成調度過程。
- 沒有滿足條件的節點的話,就不斷重試直到滿足條件為止。
例子
apiVersion: Apps/v1
kind: Deployment
metadata:
labels:
app: hostname
name: hostname
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: hostname
strategy:
rollingUpdate:
maxSurge: 10%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: hostname
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kube.NETes.io/hostname
operator: In
values:
- k8s-node.example.com
- centos7.example.com
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: node-type
operator: In
values:
- testing
containers:
- image: tosomeone/hostname:v1.0.3
imagePullPolicy: IfNotPresent
resources:
requests:
memory: 20Mi
cpu: 100m
limits:
memory: 30Mi
cpu: 150m
livenessProbe:
failureThreshold: 5
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 1
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 2
name: hostname
ports:
- containerPort: 8080
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 2
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 5
lifecycle:
postStart:
exec:
command:
- echo
- "started"
preStop:
exec:
command:
- sleep
- "3"
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
DNSPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
preferredDuringSchedulingIgnoredDuringExecution:指定的就是軟策略
requiredDuringSchedulingIgnoredDuringExecution:指定的就是硬策略
例子中優先選擇node上有node-type=testing標簽的node,且這些node需要滿足主機名k8s-node.example.com或者centos7.example.com
操作符有下面的幾種:
- In:label 的值在某個列表中
- NotIn:label 的值不在某個列表中
- Gt:label 的值大于某個值
- Lt:label 的值小于某個值
- Exists:某個 label 存在
- DoesNotExist:某個 label 不存在