pod 的亲和性与反亲和性 实验
一、环境准备
node01、 node02 都有标签 test=a,有个pod1 运行在node01上, 标签为app=myapp01
#设置node01和node02节点,拥有标签 test=a
[root@master demo]# kubectl label nodes node{01,02} test=a --overwrite
node/node01 labeled
node/node02 labeled
#查看拥有标签test=a的节点
[root@master demo]# kubectl get nodes -l test=a
NAME STATUS ROLES AGE VERSION
node01 Ready <none> 7d6h v1.15.1
node02 Ready <none> 7d6h v1.15.1
[root@master demo]# vim test.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp01
labels:
app: myapp01
spec:
containers:
- name: with-node-affinity
image: soscscs/myapp:v1
#声明式创建pod
[root@master demo]# kubectl apply -f test.yaml
pod/myapp01 created
#查看pod myapp01的详细信息的标签
[root@master demo]# kubectl get pods myapp01 -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp01 1/1 Running 0 26s 10.244.1.112 node01 <none> <none> app=myapp01
二、亲和性+ In 测试
[root@master demo]# vim a.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp10
labels:
app: myapp03
spec:
containers:
- name: myapp03
image: soscscs/myapp:v1
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- myapp01
topologyKey: test
pod拥有app=myapp01 标签(设这个pod为x),则把新pod调度到和 pod x 拥有同一个拓扑域test=a 的 节点上。
[root@master demo]# for i in myapp{11..15}; do kubectl apply -f a.yaml; sed -ri "4s#myapp1[0-9]#$i#" a.yaml; done
pod/myapp10 created
pod/myapp11 created
pod/myapp12 created
pod/myapp13 created
pod/myapp14 created
[root@master demo]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp01 1/1 Running 0 88m 10.244.1.112 node01 <none> <none> app=myapp01
myapp10 1/1 Running 0 6s 10.244.2.104 node02 <none> <none> app=myapp03
myapp11 1/1 Running 0 6s 10.244.1.138 node01 <none> <none> app=myapp03
myapp12 1/1 Running 0 5s 10.244.2.105 node02 <none> <none> app=myapp03
myapp13 1/1 Running 0 5s 10.244.1.139 node01 <none> <none> app=myapp03
myapp14 1/1 Running 0 5s 10.244.2.106 node02 <none> <none> app=myapp03
因为拓扑域test=a内,已经有了pod存在于node01节点上,所以
三、亲和性+NotIn 测试
[root@master demo]# vim a.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp10
labels:
app: myapp03
spec:
containers:
- name: myapp03
image: soscscs/myapp:v1
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: NotIn
values:
- myapp01
topologyKey: test
[root@master demo]# kubectl delete pod myapp{10..14};for i in myapp{11..15}; do kubectl apply -f a.yaml; sed -ri "4s#myapp1[0-9]#$i#" a.yaml; done
pod "myapp10" deleted
pod "myapp11" deleted
pod "myapp12" deleted
pod "myapp13" deleted
pod "myapp14" deleted
pod/myapp10 created
pod/myapp11 created
pod/myapp12 created
pod/myapp13 created
pod/myapp14 created
[root@master demo]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp01 1/1 Running 0 91m 10.244.1.112 node01 <none> <none> app=myapp01
myapp10 1/1 Running 0 5s 10.244.1.140 node01 <none> <none> app=myapp03
myapp11 1/1 Running 0 5s 10.244.2.107 node02 <none> <none> app=myapp03
myapp12 1/1 Running 0 4s 10.244.1.141 node01 <none> <none> app=myapp03
myapp13 1/1 Running 0 4s 10.244.2.108 node02 <none> <none> app=myapp03
myapp14 1/1 Running 0 4s 10.244.1.142 node01 <none> <none> app=myapp03
四、非亲和性+In 测试
[root@master demo]# vim a.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp10
labels:
app: myapp03
spec:
containers:
- name: myapp03
image: soscscs/myapp:v1
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- myapp01
topologyKey: test
[root@master demo]# kubectl delete pod myapp{10..14};for i in myapp{11..15}; do kubectl apply -f a.yaml; sed -ri "4s#myapp1[0-9]#$i#" a.yaml; done
pod "myapp10" deleted
pod "myapp11" deleted
pod "myapp12" deleted
pod "myapp13" deleted
pod "myapp14" deleted
pod/myapp10 created
pod/myapp11 created
pod/myapp12 created
pod/myapp13 created
pod/myapp14 created
[root@master demo]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp01 1/1 Running 0 96m 10.244.1.112 node01 <none> <none> app=myapp01
myapp10 0/1 Pending 0 4s <none> <none> <none> <none> app=myapp03
myapp11 0/1 Pending 0 3s <none> <none> <none> <none> app=myapp03
myapp12 0/1 Pending 0 3s <none> <none> <none> <none> app=myapp03
myapp13 0/1 Pending 0 3s <none> <none> <none> <none> app=myapp03
myapp14 0/1 Pending 0 3s <none> <none> <none> <none> app=myapp03
[root@master demo]# kubectl describe pod myapp10
五、非亲和性 + NotIn 测试
[root@master demo]# vim a.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp10
labels:
app: myapp03
spec:
containers:
- name: myapp03
image: soscscs/myapp:v1
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: NotIn
values:
- myapp01
topologyKey: test
[root@master demo]# kubectl delete pod myapp{10..14};for i in myapp{11..15}; do kubectl apply -f a.yaml; sed -ri "4s#myapp1[0-9]#$i#" a.yaml; done
pod "myapp10" deleted
pod "myapp11" deleted
pod "myapp12" deleted
pod "myapp13" deleted
pod "myapp14" deleted
pod/myapp10 created
pod/myapp11 created
pod/myapp12 created
pod/myapp13 created
pod/myapp14 created
[root@master demo]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp01 1/1 Running 0 102m 10.244.1.112 node01 <none> <none> app=myapp01
myapp10 1/1 Running 0 8s 10.244.2.109 node02 <none> <none> app=myapp03
myapp11 0/1 Pending 0 8s <none> <none> <none> <none> app=myapp03
myapp12 0/1 Pending 0 7s <none> <none> <none> <none> app=myapp03
myapp13 0/1 Pending 0 7s <none> <none> <none> <none> app=myapp03
myapp14 0/1 Pending 0 7s <none> <none> <none> <none> app=myapp03
[root@master demo]# kubectl describe pod myapp11