书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》
一边学习一边整理读书笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Kubernetes认证考试自学系列 | 汇总_COCOgsta的博客-CSDN博客
前面创建的pod只是调度到了两台node上,虽然master的状态也是Ready,但是并没有pod调度上去,这就是因为出现taint(污点)的问题了。
如果我们给某节点设置了taint的话,只有那些设置了tolerations(容忍污点)的pod才能运行在此节点上。
首先查看vms11是否设置了taint。
[root@vms10 pod]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: <none>
[root@vms10 pod]#
可以看到此时vms11上并没有任何taint的设置。
5.8.1 给节点设置及删除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-5957f949fc-58h7x 1/1 Running 0 105s 10.244.81.105 vms11.rhce.cc
nginx-5957f949fc-p6r8x 1/1 Running 0 34m 10.244.81.100 vms11.rhce.cc
nginx-5957f949fc-rbxzm 1/1 Running 0 34m 10.244.81.101 vms11.rhce.cc
nginx-5957f949fc-wfhdl 1/1 Running 0 34m 10.244.81.102 vms11.rhce.cc
[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-dzc7w 1/1 Running 0 39s 10.244.14.51 vms12.rhce.cc
nginx-7cf7d6dbc8-kr22f 1/1 Running 0 39s 10.244.14.45 vms12.rhce.cc
nginx-7cf7d6dbc8-nmzm9 1/1 Running 0 39s 10.244.14.52 vms12.rhce.cc
nginx-7cf7d6dbc8-zb4t7 1/1 Running 0 39s 10.244.14.46 vms12.rhce.cc
[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的值。
5.8.2 设置operator的值为Equal
在pod里定义tolerations的时候,如果operator的值为Equal的话,则value和effect的值要与节点的taint的值匹配才可以。
步骤1:删除vms12上的diskxx=ssdxx标签。
[root@vms10 ~]# kubectl label nodes vms12.rhce.cc diskxx-
node/vms12.rhce.cc labeled
[root@vms10 ~]#
步骤2:在vms11上设置标签diskxx=ssdxx。
[root@vms10 ~]# kubectl label nodes vms11.rhce.cc diskxx=ssdxx
node/vms11.rhce.cc labeled
[root@vms10 ~]#
做这个的目的是创建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 apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE ...
web1 0/1 Pending 0 5s <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]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
web1 1/1 Running 0 3s 10.244.1.18 vms11.rhce.cc
[root@vms10 pod]#
可以看到此时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]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web1 0/1 Pending 0 2s <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
web1 1/1 Running 0 4s 10.244.1.19 vms11.rhce.cc
[root@vms10 pod]#
说明:如果节点有多个污点的话,则需要在pod里设置容忍所有的污点,pod才能在此节点上运行。
自行删除此pod。
5.8.3 operator的值等于Exists的情况
在设置节点taint的时候,如果value的值为非空,在pod里的tolerations字段只能写Equal,不能写Exists。
步骤1:取消vms11的key123=value123这个taint值。
[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc key123-
node/vms11.rhce.cc untainted
[root@vms10 ~]#
[root@vms10 ~]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: keyxx=valuexx:NoSchedule
[root@vms10 ~]#
这里vms11还有一个污点keyxx=valuexx:NoSchedule,此处keyxx的值是valuexx,是非空的。
步骤2:修改podtaint.yaml,配置operator的值为Exist。
[root@vms10 ~]# 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 pod taint.yaml
The Pod "web1" is invalid:
* spec.tolerations[0].operator: Invalidvalue: 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 ~]# kubectl taint nodes vms11.rhce.cc keyxx=:NoSchedule --overwrite
node/vms11.rhce.cc modified
[root@vms10 ~]#
[root@vms10 ~]# kubectl describe nodes vms11 | grep -E '(Roles|Taints)'
Roles: <none>
Taints: keyxx:NoSchedule
[root@vms10 ~]#
“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 configured
[root@vms10 pod]#
[root@vms10 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web1 1/1 Running 0 10m
[root@vms10 pod]#
正常运行,说明在没有设置value的时候可以用Exists。
步骤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-
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]#