K8S标签
- Lable为了方便管理资源对象,可以指定让pod调度到哪个node节点上运行。
- 一个资源可以拥有多个标签,实现不同维度的管理。
- 一个标签可以对应多个资源,一个资源也可以有多个标签,它们是多对多的关系。
- 标签是一对键值。
- 如果有多个节点有相同的标签,那么会随机匹配到打了标签信息的某个节点上。
1、标签组成
- key=value
- key:以数字或字母开头,可以使用数字、字母、下划线、连字符,最大不能超过63位。
- value:可以为空,只能使用字母、数字开头。
2、标签命令
kubectl get pods --show-labels
kubectl get pod -l app=nginx --show-labls
kubectl get node --show-labels
kubectl label node {节点名称} {标签}
kubectl get pod -l xxx!=xxx --show-lables
3、标签案例
[root@k8s-master1 ~]
[root@k8s-master1 ~]
NAME STATUS ROLES AGE VERSION LABELS
k8s-master1 Ready <none> 26d v1.23.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master1,kubernetes.io/os=linux
k8s-node1 Ready <none> 26d v1.23.6 app=gateway,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 26d v1.23.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master1 ~]
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: http
name: http
spec:
replicas: 1
selector:
matchLabels:
disktype: ssd
template:
metadata:
labels:
disktype: ssd
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- image: nginx
name: nginx
[root@k8s-master1 ~]
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 32s default-scheduler Successfully assigned default/http-774c5d79b6-ktnz8 to k8s-node1
Normal Pulling 30s kubelet Pulling image "nginx"
Normal Pulled 29s kubelet Successfully pulled image "nginx" in 1.170642094s
Normal Created 29s kubelet Created container nginx
Normal Started 28s kubelet Started container nginx
[root@k8s-master1 ~]
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
gateway-7cdfc7ff46-p2zrb 1/1 Running 0 8d 10.244.36.153 k8s-node1 <none> <none>
http-774c5d79b6-ktnz8 1/1 Running 0 57s 10.244.36.154 k8s-node1 <none> <none>
4、污点
[root@k8s-master1 ~]
[root@k8s-master1 ~]
[root@k8s-master1 ~]
Taints: name=zhang:NoSchedule
[root@k8s-master1 ~]
[root@k8s-master1 ~]
Taints: <none>
5、污点案例
[root@k8s-master1 ~]
[root@k8s-master1 ~]
Taints: node-role.kubernetes.io/master:PreferNoSchedule
Taints: zz=yy:NoSchedule
Taints: <none>
[root@k8s-master1 ~]
pod 1/1 Running 0 50s 10.244.169.186 k8s-node2 <none> <none>
6、Tolerations容忍
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
---
tolerations:
- key: "key"
operator: "Exists"
effect: "NoSchedule"
---
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoExecute"
tolerationSeconds: 3600
---
tolerations:
- operator: "Exists"
---
tolerations:
- key: "key"
operator: "Exists"
7、容忍案例
[root@k8s-master1 ~]
[root@k8s-master1 ~]
Taints: node-role.kubernetes.io/master:PreferNoSchedule
Taints: zz=yy:NoSchedule
Taints: aa=bb:NoSchedule
[root@k8s-master1 ~]
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod
name: pod
spec:
tolerations:
- key: "zz"
operator: "Equal"
value: "yy"
effect: "NoSchedule"
containers:
- image: nginx
name: pod
resources: {}
restartPolicy: Always
[root@k8s-master1 ~]
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod 1/1 Running 0 52s 10.244.36.155 k8s-node1 <none> <none>