Label学习
在Kubernetes(简称K8s)中,Label(标签)是一种用于对Kubernetes资源进行分类和标记的机制。通过为Kubernetes对象添加标签,可以对它们进行灵活的组织、筛选和管理。
具体而言,Kubernetes的资源包括Pod、Service、Node、Namespace等。通过为这些资源添加自定义的标签,可以实现以下功能:
资源的分类和归类:可以根据业务需求或其他条件为资源打上不同的标签,例如将前端和后端服务分别打上"frontend"和"backend"的标签,或者根据环境将资源打上"dev"或"prod"的标签,从而方便对资源进行分类和组织。
选择器匹配:Label可以作为选择器的一部分使用,用于选择符合特定标签条件的资源。这样就可以根据标签进行资源的筛选和过滤,例如通过标签选择器将特定标签的Pod部署到指定的节点上。
资源的版本管理:可以使用不同的标签来区分资源的版本,例如为应用程序的不同版本打上不同的标签,方便进行版本切换或回滚。
监控和调试:可以通过为资源打上标签,将其与监控工具进行关联,从而快速识别和过滤所需的资源。
总的来说,Kubernetes中的Label是一种对资源进行分类和标记的机制,它提供了一种灵活且可扩展的方式来组织、筛选、管理和调度Kubernetes资源。
创建pod时添加标签
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2023-12-04T08:50:55Z"
generation: 1
labels:
app: web
name: nginx-deployment
namespace: app
resourceVersion: "420437"
uid: 4f19b2ea-1b2b-4ce0-a891-781e8615cc96
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: web
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: web #pod的标签的参数,key,value的格式app: web
spec:
containers:
- image: nginx:1.9.1
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: "2023-12-04T08:50:57Z"
lastUpdateTime: "2023-12-04T08:50:57Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2023-12-04T08:50:55Z"
lastUpdateTime: "2023-12-04T08:50:57Z"
message: ReplicaSet "nginx-deployment-6c4c55bc9" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 1
readyReplicas: 1
replicas: 1
updatedReplicas: 1
查看pod的标签的命令
[root@k8s-master1 Label]# kubectl get pods -n app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-7d7b559db5-dbdf8 1/1 Running 0 3m54s app=web,pod-template-hash=7d7b559db5
在pod上添加标签
在原来的标签基础上加上apps=host1的标签
[root@k8s-master1 Label]# kubectl label pods nginx-deployment-7d7b559db5-dbdf8 apps=host1 -n app
pod/nginx-deployment-7d7b559db5-dbdf8 labeled
[root@k8s-master1 Label]# kubectl get pods -n app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-7d7b559db5-dbdf8 1/1 Running 0 8m9s app=web,apps=host1,pod-template-hash=7d7b559db5
在pod上删除标签
要从名为nginx-pod的Pod中删除一个名为apps的标签,可以使用以下命令:
[root@k8s-master1 Label]# kubectl label pod nginx-deployment-7d7b559db5-dbdf8 apps- -n app
pod/nginx-deployment-7d7b559db5-dbdf8 labeled
[root@k8s-master1 Label]# kubectl get pods -n app --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-7d7b559db5-dbdf8 1/1 Running 0 15m app=web,pod-template-hash=7d7b559db5
修改pod中的标签的值的话可以通过修改deployment去更改
node节点的标签管理
设置k8s-node1节点的标签管理
为k8s-node1节点添加apps=host1的标签
[root@k8s-master1 Label]# kubectl label nodes k8s-node1 apps=host1
node/k8s-node1 labeled
查看node节点的标签
[root@k8s-master1 Label]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master3 Ready <none> 29d v1.22.1 app=redis-ha,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master3,kubernetes.io/os=linux
k8s-node1 Ready <none> 47d v1.22.1 app.kubernetes.io/instance=tomcat,app.kubernetes.io/managed-by=Helm,app.kubernetes.io/name=tomcat,app.kubernetes.io/version=10.1.15,app=nginx,apps=host1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,helm.sh/chart=tomcat-10.11.0,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux,pod-template-hash=7b7447795c
k8s中的NodeSelector是一种用于在集群中选择特定节点的方法。它允许你为Pod指定一组键值对,以便只有满足这些键值对条件的节点才会被调度器选中来运行该Pod。
NodeSelector通常用于满足特定的硬件或资源要求,或者用于将Pod调度到特定的节点标签组合上。通过配置NodeSelector,你可以根据你的需求,确保Pod运行在符合你期望条件的节点上,从而实现更灵活和优化的调度策略。
NodeSelector的配置方式与Pod的定义文件中的spec字段相关。其中,spec字段下的nodeSelector字段可以用来定义一个或多个键值对,每个键值对代表一个节点标签,满足这些标签条件的节点将被选中。
在这个deployment中设置了nodeSelector参数apps: host,在node标签中没有这个标签,容器找不到启动的宿主机,容器的状态是Pending的状态
[root@k8s-master1 Label]# kubectl get deployment nginx-deploy -n app -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx-deploy"},"name":"nginx-deploy","namespace":"app"},"spec":{"replicas":1,"revisionHistoryLimit":10,"selector":{"matchLabels":{"app":"nginx-deploy"}},"strategy":{"rollingUpdate":{"maxSurge":"25%","maxUnavailable":"25%"},"type":"RollingUpdate"},"template":{"metadata":{"labels":{"app":"nginx-deploy"}},"spec":{"containers":[{"image":"nginx:1.7.9","imagePullPolicy":"IfNotPresent","name":"nginx","resources":{"limits":{"cpu":"200m","memory":"128Mi"},"requests":{"cpu":"100m","memory":"128Mi"}}}],"nodeSelector":{"apps":"host"},"restartPolicy":"Always","terminationGracePeriodSeconds":30}}}}
creationTimestamp: "2023-12-04T10:24:28Z"
generation: 1
labels:
app: nginx-deploy
name: nginx-deploy
namespace: app
resourceVersion: "435333"
uid: 32c63327-f770-4ab6-b23a-e71345246e95
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx-deploy
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx-deploy
spec:
containers:
- image: nginx:1.7.9
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: 200m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
nodeSelector:
apps: host
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
conditions:
- lastTransitionTime: "2023-12-04T10:24:28Z"
lastUpdateTime: "2023-12-04T10:24:28Z"
message: Deployment does not have minimum availability.
reason: MinimumReplicasUnavailable
status: "False"
type: Available
- lastTransitionTime: "2023-12-04T10:24:28Z"
lastUpdateTime: "2023-12-04T10:24:28Z"
message: ReplicaSet "nginx-deploy-c68cbc4f6" is progressing.
reason: ReplicaSetUpdated
status: "True"
type: Progressing
observedGeneration: 1
replicas: 1
unavailableReplicas: 1
updatedReplicas: 1
在这个deployment中设置了nodeSelector参数apps: host,在node标签中没有这个标签,容器找不到启动的宿主机,容器的状态是Pending的状态
[root@k8s-master1 Label]# kubectl get pods -n app -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-c68cbc4f6-774b6 0/1 Pending 0 4m37s <none> <none> <none> <none>
[root@k8s-master1 Label]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master3 Ready <none> 29d v1.22.1 app=redis-ha,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master3,kubernetes.io/os=linux
k8s-node1 Ready <none> 47d v1.22.1 app.kubernetes.io/instance=tomcat,app.kubernetes.io/managed-by=Helm,app.kubernetes.io/name=tomcat,app.kubernetes.io/version=10.1.15,app=nginx,apps=host1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,helm.sh/chart=tomcat-10.11.0,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux,pod-template-hash=7b7447795c
咱们将nodeSelector参数apps: host改成apps=host1,容器便启动成功,落在了node1上了
[root@k8s-master1 Label]# kubectl get pods -n app -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-57675d9bb7-8fpbh 1/1 Running 0 8s 10.10.36.114 k8s-node1 <none> <none>
nodeSelector参数是容器落在定向调度指定标签的宿主机上