针对对象为Pod,目的是实现,新建Pod和目标Pod不要调度到一起,不在同一个Node上
示例:
root@k8s-master:/home/vagrant# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:name: testpod01labels:app: myapp01env: test1
spec:containers:- name: testpod01image: nginx:1.23.2imagePullPolicy: IfNotPresentroot@k8s-master:/home/vagrant# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:name: testpod02labels:app: myapp02env: test2
spec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution: ##必须满足下面匹配规则- labelSelector:matchExpressions:- key: appoperator: In values:- myapp01 ## app=myapp01, 上面的Pod是符合要求的topologyKey: "kubernetes.io/hostname"containers:- name: testpod02image: redis:6.2root@k8s-master:/home/vagrant# kubectl create -f pod.yaml
pod/testpod01 created
root@k8s-master:/home/vagrant# kubectl create -f pod2.yaml
pod/testpod02 createdroot@k8s-master:/home/vagrant# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
node-affinity 1/1 Running 0 30m 172.16.126.2 k8s-worker2 <none> <none> <none>
testpod01 1/1 Running 0 11s 172.16.194.67 k8s-worker1 <none> <none> app=myapp01,env=test1
testpod02 1/1 Running 0 9s 172.16.126.3 k8s-worker2 <none> <none> app=myapp02,env=test2