书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》
一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客
前面创建的pod只是调度到了两台node上,虽然master的状态也是Ready,但是并没有pod调度上去,这就是因为出现taint(污点)的问题了。
如果我们给某节点设置了taint的话,只有那些设置了tolerations(容忍污点)的pod才能运行在此节点上。
想象一下,某个公司有污点(假设克扣工资),一般面试的人是不会选择这家公司的。但是如果某人说我能容忍这个污点,则就可以过来上班了。同理,如果一个pod能容忍节点上污点,则此pod就可以在这个节点上运行。
首先查看vms11是否设置了taint。
##########实操验证##########
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: <none>
[root@vms10 pod]#
可以看到此时vms11上并没有任何taint的设置。
给节点设置及删除taint
本节来演示一下如何给节点添加及删除污点,在pod里增加容忍污点toleration。
为节点设置taint的语法如下。
kubectl taint nodes 节点名 key值=value值:effect effect的值一般是NoSchedule
如果要对所有节点设置,语法如下。
kubectl taint nodes --all key值=value值:NoSchedule
注意:这里value的值是可以不写的,如果不写,语法就是这样的。
kubectl taint nodes 节点名 key值=:NoSchedule
删除taint。
kubectl taint nodes 节点名 key-
步骤1:为vms11设置taint。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx=valuexx:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: keyxx=valuexx:NoSchedule
[root@vms10 pod]#
步骤2:现在查看现有pod。
##########实操验证##########
[root@vms10 pod]# kubectl get pods -o wide --no-headers
nginx-7cf7d6dbc8-28ccn 1/1 Running 0 9m48s 10.244.81.80 vms11.rhce.cc <none> <none>
nginx-7cf7d6dbc8-bwckp 1/1 Running 0 5m25s 10.244.81.81 vms11.rhce.cc <none> <none>
nginx-7cf7d6dbc8-hbdcb 1/1 Running 0 9m48s 10.244.81.79 vms11.rhce.cc <none> <none>
nginx-7cf7d6dbc8-w4sdm 1/1 Running 0 5m25s 10.244.81.82 vms11.rhce.cc <none> <none>
[root@vms10 pod]#
可以发现,pod依然是在vms11上运行,说明如果对某节点设置taint的话,是不影响当前正在运行的pod的。
把deployment nginx的副本数设置为0,然后再将其设置为4,目的是让其重新生成新的pod,然后查看pod的分布情况。
步骤3:把nginx的副本数设置为0。
##########实操验证##########
[root@vms10 pod]# kubectl scale deploy nginx --replicas=0
deployment.apps/nginx scaled
[root@vms10 pod]#
步骤4:查看现有的pod。
##########实操验证##########
[root@vms10 pod]# kubectl get pods
No resources found in default namespace.
[root@vms10 pod]#
可以看到现在已经没有pod了。
步骤5:把nginx的副本数设置为4。
##########实操验证##########
[root@vms10 pod]# kubectl scale deploy nginx --replicas=4
deployment.apps/nginx scaled
[root@vms10 pod]#
步骤6:查看pod的分布情况。
##########实操验证##########
[root@vms10 pod]# kubectl get pods -o wide --no-headers
nginx-7cf7d6dbc8-5s9s5 1/1 Running 0 20s 10.244.14.19 vms12.rhce.cc <none> <none>
nginx-7cf7d6dbc8-sqg9l 1/1 Running 0 20s 10.244.14.18 vms12.rhce.cc <none> <none>
nginx-7cf7d6dbc8-sqp64 1/1 Running 0 20s 10.244.14.17 vms12.rhce.cc <none> <none>
nginx-7cf7d6dbc8-wncfh 1/1 Running 0 20s 10.244.14.20 vms12.rhce.cc <none> <none>
[root@vms10 pod]#
可以看到,现在所有的pod都是运行在vms12上了,因为vms11存在taint污点。
步骤7:删除名字为nginx的deployment。
##########实操验证##########
[root@vms10 pod]# kubectl delete deploy nginx
deployment.apps "nginx" deleted
[root@vms10 pod]#
如果需要pod在含有taint的节点上运行,则定义pod的时候需要指定toleration属性。
在pod里定义toleration的格式如下。
tolerations:
- key: "key值"
operator: "Equal"
value: "value值"
effect: "值"
operator的值有以下两个。
Equal: value需要和taint的value值一样(默认)。
Exists: 可以不指定value的值。
设置operator的值为Equal
在pod里定义tolerations的时候,如果operator的值为Equal的话,则value和effect的值要与节点的taint的值匹配才可以。
步骤1:删除vms12上的diskxx=ssdxx标签。
##########实操验证##########
[root@vms10 pod]# kubectl label nodes vms12.rhce.cc diskxx-
node/vms12.rhce.cc labeled
[root@vms10 pod]#
步骤2:在vms11上设置标签diskxx=ssdxx。
##########实操验证##########
[root@vms10 pod]# kubectl label nodes vms11.rhce.cc diskxx=ssdxx
node/vms11.rhce.cc labeled
[root@vms10 pod]#
做这个的目的是创建pod时,方便我们控制pod在vms11上运行,然后测试在有污点的情况下如何让pod在其上运行。
步骤3:创建pod用的yaml文件podtaint.yaml,内容如下。
##########实操验证##########
[root@vms10 pod]# cat podtaint.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
role: myrole
spec:
nodeSelector:
diskxx: ssdxx
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
[root@vms10 pod]#
这里指定了nodeSelector,只能在vms11上运行,因为vms11上才有diskxx=ssdxx这个标签,但是此pod里现在是没有设置容忍污点的,所以应该无法正常运行。
步骤4:创建pod。
##########实操验证##########
[root@vms10 pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1 0/1 Pending 0 6s <none> <none> <none> <none>
[root@vms10 pod]#
因为要求web1要在vms11上运行(因为vms11上有diskxx=ssdxx标签),但vms11上有污点,所以此时pod处于Pending状态。
步骤5:删除此pod。
##########实操验证##########
[root@vms10 pod]# kubectl delete -f podtaint.yaml
pod "web1" deleted
[root@vms10 pod]#
步骤6:修改podtaint.yaml的内容。
##########实操验证##########
[root@vms10 pod]# cat podtaint.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
role: myrole
spec:
nodeSelector:
diskxx: ssdxx
tolerations:
- key: "keyxx"
operator: "Equal"
value: "valuexx"
effect: "NoSchedule"
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
[root@vms10 pod]#
这里添加了容忍污点的选项tolerations,容忍键值对为keyxx=valuexx,且effect的值被设置为NoSchedule的污点,vms11上设置的就是这样的污点。
步骤7:再次创建pod。
##########实操验证##########
[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]#
[root@vms10 pod]#
[root@vms10 pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1 1/1 Running 0 9s 10.244.81.83 vms11.rhce.cc <none> <none>
[root@vms10 pod]#
可以看到此时pod在vms11上正常运行了。
注意:并不是节点设置了taint,pod设置了toleration,这个pod就一定会在此节点上运行,所以这里用label来指定pod在vms11上运行。
步骤8:删除此pod。
##########实操验证##########
[root@vms10 pod]# kubectl delete -f podtaint.yaml
pod "web1" deleted
[root@vms10 pod]#
步骤9:删除vms11的taint设置。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx-
node/vms11.rhce.cc untainted
[root@vms10 pod]#
步骤10:给vms11重新设置多个taint。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc key123=value123:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx=valuexx:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E -A1 '(Roles|Taints)'
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
--
Taints: key123=value123:NoSchedule
keyxx=valuexx:NoSchedule
[root@vms10 pod]#
上面可以看到,vms11上有了多个污点。
步骤11:在podtaint.yaml不修改的情况下再次运行。
##########实操验证##########
[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]#
[root@vms10 pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1 0/1 Pending 0 6s <none> <none> <none> <none>
[root@vms10 pod]#
pod的状态为Pending,说明pod的toleration没有和节点的所有污点匹配,那么就不允许创建pod。
步骤12:删除此pod。
##########实操验证##########
[root@vms10 pod]# kubectl delete -f podtaint.yaml
pod "web1" deleted
[root@vms10 pod]#
步骤13:修改podtaint.yaml文件。
##########实操验证##########
[root@vms10 pod]# cat podtaint.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
role: myrole
spec:
nodeSelector:
diskxx: ssdxx
tolerations:
- key: "keyxx"
operator: "Equal"
value: "valuexx"
effect: "NoSchedule"
- key: "key123"
operator: "Equal"
value: "value123"
effect: "NoSchedule"
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
[root@vms10 pod]#
这里设置了容忍vms11上的所有污点。
步骤14:运行并查看pod。
##########实操验证##########
[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]#
[root@vms10 pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1 1/1 Running 0 8s 10.244.81.84 vms11.rhce.cc <none> <none>
[root@vms10 pod]#
说明:如果节点有多个污点的话,则需要在pod里设置容忍所有的污点,pod才能在此节点上运行。
自行删除此pod。
operator的值等于Exists的情况
先说结论,在设置节点taint的时候,如果value的值为非空,在pod里的tolerations字段只能写Equal,不能写Exists。
步骤1:取消vms11的key123=value123这个taint值。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc key123-
node/vms11.rhce.cc untainted
[root@vms10 pod]#
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: keyxx=valuexx:NoSchedule
[root@vms10 pod]#
这里vms11还有一个污点keyxx=valuexx:NoSchedule,此处keyxx的值是valuexx,是非空的。
步骤2:修改podtaint.yaml,配置operator的值为Exist。
##########实操验证##########
[root@vms10 pod]# cat podtaint.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
role: myrole
spec:
nodeSelector:
diskxx: ssdxx
tolerations:
- key: "keyxx"
operator: "Exists"
value: "valuexx"
effect: "NoSchedule"
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
[root@vms10 pod]#
步骤3:创建pod
##########实操验证##########
[root@vms10 pod]# kubectl apply -f podtaint.yaml
The Pod "web1" is invalid:
* spec.tolerations[0].operator: Invalid value: core.Toleration{Key:"keyxx", Operator:"Exists", Value:"valuexx", Effect:"NoSchedule", TolerationSeconds:(*int64)(nil)}: value must be empty when `operator` is 'Exists'
* spec.tolerations: Forbidden: existing toleration can not be modified except its tolerationSeconds
[root@vms10 pod]#
说明:在设置toleration时,如果operator选择的是Exists的话,是不能写value的。
步骤4:修改vms11的taint值。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx=:NoSchedule --overwrite
node/vms11.rhce.cc modified
[root@vms10 pod]#
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: keyxx:NoSchedule
[root@vms10 pod]#
“keyxx=”后面直接是冒号,说明此时value的值为空。
步骤5:修改podtaint.yaml的值。
##########实操验证##########
[root@vms10 pod]# cat podtaint.yaml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
role: myrole
spec:
nodeSelector:
diskxx: ssdxx
tolerations:
- key: "keyxx"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
[root@vms10 pod]#
此时并没有设置value的值。
步骤6:创建pod。
##########实操验证##########
[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]#
[root@vms10 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web1 1/1 Running 0 6s
[root@vms10 pod]#
正常运行,说明在没有设置value的时候可以用Exists,不过如果要使用Equal的话,应该按照如下格式写。
- key:"keyxx"
operator: "Exists"
value: ""
effect: "NoSchedule"
步骤7:删除web1这个pod。
##########实操验证##########
[root@vms10 pod]# kubectl delete -f podtaint.yaml
pod "web1" deleted
[root@vms10 pod]#
步骤8:清除vms11上所有的taint设置。
##########实操验证##########
[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx-
node/vms11.rhce.cc untainted
[root@vms10 pod]#
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: <none>
[root@vms10 pod]#