k8s(八)—调度因素(nodeName、nodeSelector、亲和与反亲和、Taints 污点)、影响pod调度的指令
[root@server2 ~]# kubectl get pod -n kube-system2 影响kubernetes调度的因素2.1 、nodeName[root@server2 ~]# vim pod.yamlapiVersion: v1kind: Podmetadata:name: nginxspec:containers:- name: nginximage: nginxnodeNa
·
1 调度简介
[root@server2 ~]# kubectl get pod -n kube-system
2 影响kubernetes调度的因素
2.1 nodeName(针对节点)
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeName: server3 指定节点调度到server3
[root@server2 ~]# kubectl apply -f pod.yaml 运行,pod直接被调度到server3上
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeName: server13 指定错误的节点
[root@server2 ~]# kubectl get pod 运行不成功
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 12s
缺点:直接无视存在的节点,直奔指定的节点
2.2 nodeSelector(针对节点)
[root@server2 ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeSelector:
disktype: ssd 标签
[root@server2 ~]# kubectl apply -f pod.yaml 创建
pod/nginx created
[root@server2 ~]# kubectl get pod 查看pod,没有运行成功,因为没有匹配的标签
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 103s
[root@server2 ~]# kubectl label nodes server4 disktype=ssd 添加server4节点标签为disktype=ssd
node/server4 labeled
[root@server2 ~]# kubectl get pod 查看pod,运行成功
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5m19s
[root@server2 ~]# kubectl get node --show-labels 查看节点标签,标签已经添加
[root@server2 ~]# kubectl label nodes server4 disktype- 如果删除标签,-表示去掉
node/server4 unlabeled
[root@server2 ~]# kubectl get pod 查看pod还是运行成功,因为以及调度过去了
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10m
2.3 亲和与反亲和(针对节点)
[root@server2 ~]# kubectl delete -f pod.yaml 删除之前pod
pod "nginx" deleted
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
affinity:
nodeAffinity: 节点亲和性
requiredDuringSchedulingIgnoredDuringExecution: 必须满足
nodeSelectorTerms: 匹配标签
- matchExpressions:
- key: disktype 匹配 的key
operator: In 运算符为in
values: 取值
- ssd 匹配的取值为ssd
[root@server2 ~]# kubectl apply -f pod.yaml 创建
pod/nginx created
[root@server2 ~]# kubectl get pod 查看pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 19s
[root@server2 ~]# kubectl label nodes server3 disktype=ssd 给server3节点加标签
node/server3 labeled
[root@server2 ~]# kubectl get pod 查看pod,运行成功
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m57s
[root@server2 ~]# kubectl label nodes server4 disktype=ssd 给server4节点加标签
node/server4 labeled
添加倾向满足
[root@server2 ~]# kubectl delete -f pod.yaml
pod "nginx" deleted
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
preferredDuringSchedulingIgnoredDuringExecution: 添加倾向性
- weight: 1
preference:
matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- server4 倾向server4
[root@server2 ~]# kubectl apply -f pod.yaml
pod/nginx created
[root@server2 ~]# kubectl describe pod nginx 查看镜像详细信息,虽然sever3和server4上都有 disktype=ssd 标签,但是设置了倾向server4,所以会调度到server4
[root@server2 ~]# vim pod.yaml
[root@server2 ~]# kubectl delete -f pod.yaml
pod "nginx" deleted
[root@server2 ~]# kubectl apply -f pod.yaml 创建
pod/nginx created
2.4 针对pod亲和性和反亲和性
[root@server2 ~]# kubectl run nginx --image=nginx 运行一个pod
pod/nginx created
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: nginx
image: nginx
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: run
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
[root@server2 ~]# kubectl get pod --show-labels 查看pod标签为 run=nginx
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 13m run=nginx
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: nginx
image: nginx
affinity:
podAffinity: 表示pod亲和力
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: run 当运行demo时必须和带有run=nginx标签的pod在一起
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname 指定区域,通过主机名区分,也可以通过不同集群区分
[root@server2 ~]# kubectl apply -f pod.yaml 创建
pod/demo created
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,demo和nginx都被调度到server4上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
demo 1/1 Running 0 68s 10.244.2.17 server4 <none> <none>
nginx 1/1 Running 0 20m 10.244.2.16 server4 <none> <none>
pod反亲和性
[root@server2 ~]# kubectl delete -f pod.yaml 删除之前创建的
pod "demo" deleted
[root@server2 ~]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: nginx
image: nginx
affinity:
podAntiAffinity: 反亲和性
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: run
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
[root@server2 ~]# kubectl apply -f pod.yaml
pod/demo created
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,demo和nginx在不同的节点上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
demo 1/1 Running 0 13s 10.244.1.26 server3 <none> <none>
nginx 1/1 Running 1 (45m ago) 11h 10.244.2.18 server4 <none> <none>
2.5 Taints 污点
实验前先环境清理:
[root@server2 ~]# kubectl delete -f pod.yaml
pod "demo" deleted
[root@server2 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx 1/1 Running 1 (4h30m ago) 15h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d23h
service/mysql ClusterIP None <none> 3306/TCP 4d15h
service/mysql-read ClusterIP 10.101.37.222 <none> 3306/TCP 4d15h
NAME READY AGE
statefulset.apps/mysql 0/0 39h
[root@server2 ~]# kubectl delete statefulsets.apps mysql 删除statefulsets控制器
[root@server2 ~]# kubectl delete svc mysql 删除服务
[root@server2 ~]# kubectl delete svc mysql-read 删除服务
[root@server2 ~]# kubectl delete pvc --all 删除所有pvc
测试:
[root@server2 ~]# kubectl get pod 查看pod,现在就运行一个pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 1 (4h36m ago) 15h
[root@server2 ~]# kubectl get pod -o wide 查看调度节点,为server4
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 1 (4h36m ago) 15h 10.244.2.18 server4 <none> <none>
[root@server2 ~]# kubectl taint node server4 key1=v1:NoExecute 在server4上添加污点,server4上没有对应的key1=v1设置,直接躯离
node/server4 tainted
[root@server2 ~]# kubectl get pod 查看pod,直接被回收了
No resources found in default namespace.
[root@server2 ~]# kubectl describe nodes | grep Taints 查询所有节点污点
Taints: node-role.kubernetes.io/master:NoSchedule
Taints: <none>
Taints: key1=v1:NoExecute
[root@server2 ~]# kubectl describe nodes server4 | grep Taints 查询server4节点上的污点
Taints: key1=v1:NoExecute
[root@server2 ~]# kubectl taint node server4 key1:NoExecute- 删除server4节点上的污点
node/server4 untainted
[root@server2 ~]# vim pod.yaml
[root@server2 ~]# kubectl apply -f pod.yaml
[root@server2 ~]# kubectl get pod -o wide 两个pod一个调度到server3一个调度到server4
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-85b98978db-565cs 1/1 Running 0 8s 10.244.1.27 server3 <none> <none>
web-server-85b98978db-hrwbm 1/1 Running 0 8s 10.244.2.20 server4 <none> <none>
[root@server2 ~]# kubectl taint node server4 key1=v1:NoSchedule 在server4上加污点,不匹配key和value时,不参加调度,但是不会躯离
node/server4 tainted
[root@server2 ~]# vim pod.yaml
[root@server2 ~]# kubectl apply -f pod.yaml 创建
deployment.apps/web-server configured
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,都调度到server3上了
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-85b98978db-2zw8q 1/1 Running 0 39s 10.244.1.28 server3 <none> <none>
web-server-85b98978db-565cs 1/1 Running 0 38m 10.244.1.27 server3 <none> <none>
web-server-85b98978db-btcst 1/1 Running 0 39s 10.244.1.29 server3 <none> <none>
web-server-85b98978db-hrwbm 1/1 Running 0 38m 10.244.2.20 server4 <none> <none>
添加容忍度
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
selector:
matchLabels:
app: nginx
replicas: 4
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
tolerations: 添加容忍
- operator: "Exists" 不指定key,就表示匹配所有 key 和 vlaue
effect: "NoSchedule" 具备NoSchedule这样的容忍度
[root@server2 ~]# kubectl apply -f pod.yaml 创建
deployment.apps/web-server configured
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,可以发现容忍了server2和srevr4上存在的污点,在server2和server4都调度了
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-7fdd6fbffc-7l4r5 1/1 Running 0 7m5s 10.244.2.22 server4 <none> <none>
web-server-7fdd6fbffc-92m56 1/1 Running 0 7m14s 10.244.2.21 server4 <none> <none>
web-server-7fdd6fbffc-nhbdk 1/1 Running 0 7m6s 10.244.0.22 server2 <none> <none>
web-server-7fdd6fbffc-xzs7t 1/1 Running 0 7m13s 10.244.1.30 server3 <none> <none>
[root@server2 ~]# kubectl taint node server3 key1=v1:NoExecute 将server3躯离掉
node/server3 tainted
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,server3被躯离到server2和server4上,因为容忍设置只能容忍NoSchedule
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-7fdd6fbffc-7l4r5 1/1 Running 1 (52m ago) 16h 10.244.2.24 server4 <none> <none>
web-server-7fdd6fbffc-92m56 1/1 Running 1 (52m ago) 16h 10.244.2.23 server4 <none> <none>
web-server-7fdd6fbffc-nhbdk 1/1 Running 1 (52m ago) 16h 10.244.0.24 server2 <none> <none>
web-server-7fdd6fbffc-tcl79 1/1 Running 0 62s 10.244.2.25 server4 <none> <none>
[root@server2 ~]# vim pod.yaml
[root@server2 ~]# kubectl apply -f pod.yaml 创建
deployment.apps/web-server configured
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,可以发现server2、server3、server4都可以调度
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-5d7f8b7699-28zjb 1/1 Running 0 36s 10.244.2.27 server4 <none> <none>
web-server-5d7f8b7699-5wscd 1/1 Running 0 46s 10.244.2.26 server4 <none> <none>
web-server-5d7f8b7699-9qsgj 1/1 Running 0 46s 10.244.1.32 server3 <none> <none>
web-server-5d7f8b7699-kcc6g 1/1 Running 0 46s 10.244.1.33 server3 <none> <none>
web-server-5d7f8b7699-mjs2q 1/1 Running 0 30s 10.244.0.29 server2 <none> <none>
web-server-5d7f8b7699-ngxrx 1/1 Running 0 34s 10.244.0.28 server2 <none> <none>
[root@server2 ~]# kubectl delete -f pod.yaml 回收
deployment.apps "web-server" deleted
[root@server2 ~]# kubectl taint node server3 key1:NoExecute- 删除server3上的污点
node/server3 untainted
[root@server2 ~]# kubectl taint node server4 key1:NoSchedule- 删除server4上的污点
node/server4 untainted
3 影响pod调度的指令
3.1 cordon—停止调度
[root@server2 ~]# kubectl cordon server3 停止server3调度
node/server3 cordoned
[root@server2 ~]# kubectl get nodes 查看节点
NAME STATUS ROLES AGE VERSION
server2 Ready control-plane,master 5d18h v1.23.5
server3 Ready,SchedulingDisabled <none> 5d18h v1.23.5 server3被停止调度了
server4 Ready <none> 5d18h v1.23.5
[root@server2 ~]# vim pod.yaml
[root@server2 ~]# kubectl apply -f pod.yaml 创建
deployment.apps/web-server configured
[root@server2 ~]# kubectl get pod -o wide 查看调度情况,server3不参与调度
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-85b98978db-cjww9 1/1 Running 0 27s 10.244.2.33 server4 <none> <none>
web-server-85b98978db-gsn2s 1/1 Running 0 36s 10.244.2.31 server4 <none> <none>
web-server-85b98978db-nbrn9 1/1 Running 0 28s 10.244.2.32 server4 <none> <none>
web-server-85b98978db-zm44t 1/1 Running 0 36s 10.244.2.30 server4 <none> <none>
3.2 drain—驱离并停止调度
[root@server2 ~]# kubectl uncordon server3 先恢复server3的调度
node/server3 uncordoned
[root@server2 ~]# kubectl drain server4 驱离
cannot delete DaemonSet-managed Pods (use --ignore-daemonsets to ignore): kube-system/kube-flannel-ds-24kvl, kube-system/kube-proxy-jvgb2 有DaemonSet控制器不让驱离
[root@server2 ~]# kubectl drain server4 --ignore-daemonsets 添加忽略daemonsets,驱离
[root@server2 ~]# kubectl get pod -o wide 查看调度节点情况,server4全部被驱离,全部调度在server3上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-server-85b98978db-2pdtp 1/1 Running 0 2m58s 10.244.1.38 server3 <none> <none>
web-server-85b98978db-csrfp 1/1 Running 0 2m58s 10.244.1.35 server3 <none> <none>
web-server-85b98978db-dht2r 1/1 Running 0 2m58s 10.244.1.36 server3 <none> <none>
web-server-85b98978db-gwhsw 1/1 Running 0 2m58s 10.244.1.37 server3 <none> <none>
[root@server2 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
server2 Ready control-plane,master 5d18h v1.23.5
server3 Ready <none> 5d18h v1.23.5
server4 Ready,SchedulingDisabled <none> 5d18h v1.23.5 驱离节点上的pod并禁用调度
3.3 delete—删除节点
注意删除节点时,先驱离该节点上pod,再删除
[root@server2 ~]# kubectl delete nodes server4 删除节点
node "server4" deleted
[root@server2 ~]# kubectl get nodes 查看节点,server4被删除
NAME STATUS ROLES AGE VERSION
server2 Ready control-plane,master 5d19h v1.23.5
server3 Ready <none> 5d19h v1.23.5
如果从新加入server4
[root@server4 ~]# systemctl restart kubelet 在server4上重启kubelet
[root@server2 ~]# kubectl get nodes server4会被重新添加上
NAME STATUS ROLES AGE VERSION
server2 Ready control-plane,master 5d19h v1.23.5
server3 Ready <none> 5d19h v1.23.5
server4 Ready <none> 12s v1.23.5
更多推荐
已为社区贡献9条内容
所有评论(0)