pod可以根据调度策略让pod调度到想要的节点上运行,或者不在某节点运行。
$ kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
ops-master-1 Ready control-plane,master 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-master-2 Ready control-plane,master 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-2,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-master-3 Ready control-plane,master 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-master-3,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
ops-worker-1 Ready <none> 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux
ops-worker-2 Ready <none> 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-2,kubernetes.io/os=linux
#给节点ops-worker-1打上标签env=uat
$ kubectl label node ops-worker-1 env=uat
#给节点ops-worker-2打上标签env=prod
$ kubectl label node ops-worker-2 env=prod
#查看生成的标签
$ kubectl get node --show-labels | grep env
ops-worker-1 Ready <none> 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=uat,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux
ops-worker-2 Ready <none> 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=prod,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-2,kubernetes.io/os=linux
$ cat nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-test
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeSelector: #添加此行
env: uat #指定标签
containers:
- name: nginx-test
image: nginx:latest
ports:
- containerPort: 80
$ kubectl apply -f nginx.yaml
deployment.apps/nginx-test created
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-6895cc9759-989h8 1/1 Running 0 7s 172.25.78.78 ops-worker-1 <none> <none>
nginx-test-6895cc9759-slzcm 1/1 Running 0 7s 172.25.78.79 ops-worker-1 <none> <none>
迁移过程相当于删除当前节点pod,再在新node上生成pod,生产环境操作时需要谨慎。
$ kubectl edit deploy nginx-test
修改
nodeSelector:
env: uat
为
nodeSelector:
env: prod
deployment.apps/nginx-test edited
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-6b7c99bbb-8sfh7 1/1 Running 0 49s 172.25.50.140 ops-worker-2 <none> <none>
nginx-test-6b7c99bbb-hp62s 1/1 Running 0 67s 172.25.50.139 ops-worker-2 <none> <none>
?pod已经切换至ops-worker-2节点上,且pod创建时间不足1分钟。
$ kubectl label node ops-worker-1 env-
node/ops-worker-1 labeled
?查看节点上pod状态。
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-node1-5f9555db6b-db2cq 1/1 Running 0 12m 172.25.78.76 ops-worker-1 <none> <none>
nginx-node1-5f9555db6b-rq2pn 1/1 Running 0 12m 172.25.78.77 ops-worker-1 <none> <none>
删除节点标签,正在运行的pod不受影响 。
?查看节点标签。
$ kubectl get nodes --show-labels | grep ops-worker-1
ops-worker-1 Ready <none> 26d v1.21.9 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ops-worker-1,kubernetes.io/os=linux
删除节点上的pod,验证pod重新调度是否正常。
$ kubectl delete pod nginx-node1-5f9555db6b-db2cq
pod "nginx-node1-5f9555db6b-db2cq" deleted
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-node1-5f9555db6b-ctbb9 0/1 Pending 0 59s <none> <none> <none> <none>
nginx-node1-5f9555db6b-rq2pn 1/1 Running 0 16m 172.25.78.77 ops-worker-1 <none> <none>
?pod无法调度运行,因为调度策略中的nodeSelector调度策略还存在,因此生产环境在清理了node标签后,一定要将运行在节点上的pod中配置的标签清理掉,否则pod重启后将无法调度。