博客园  :: 首页  :: 管理

关于Kubernetes-v1.23.6-Label标签-Selector选择器的说明

Posted on 2023-09-16 20:06  520_1351  阅读(50)  评论(0编辑  收藏  举报

以一个简单的yaml文件为例

apiVersion: v1      # api 文档版本
kind: Pod           # 资源对象类型,也可以配置为像Deployment、StatefulSet这一类的对象
metadata:
  name: nginx-demo  # Pod 的名称
  labels:           # 定义Pod的标签
    type: app       # 标签的格式,名称: 值
    test: 1.0.0
  namespace: 'default' # 命名空间的配置
spec:                  # 期望 Pod 按照这里面的描述进行创建
  containers:
  - name: nginx
    image: nginx:1.7.9
    imagePullPolicy: IfNotPresent

    ports:
    - name: http
      containerPort: 80
      protocol: TCP

  restartPolicy: OnFailure

查看Pod所有的Labels信息

[root@k8s-master qq-5201351]# kubectl get po --show-labels
NAME         READY   STATUS    RESTARTS   AGE     LABELS
nginx-demo   1/1     Running   0          4m29s   test=1.0.0,type=app
[root@k8s-master qq-5201351]#
[root@k8s-master qq-5201351]# kubectl get po --show-labels -n kube-system
NAME READY STATUS RESTARTS AGE LABELS
calico-kube-controllers-cd8566cf-67cxk 1/1 Running 7 (4d22h ago) 21d k8s-app=calico-kube-controllers,pod-template-hash=cd8566cf
calico-node-ctp5z 1/1 Running 8 (4d22h ago) 21d controller-revision-hash=79f798bb6d,k8s-app=calico-node,pod-template-generation=1
calico-node-g55dh 1/1 Running 7 (4d22h ago) 21d controller-revision-hash=79f798bb6d,k8s-app=calico-node,pod-template-generation=1
calico-node-lhqxc 1/1 Running 7 (4d22h ago) 21d controller-revision-hash=79f798bb6d,k8s-app=calico-node,pod-template-generation=1
coredns-6d8c4cb4d-6p7fs 1/1 Running 7 (4d22h ago) 21d k8s-app=kube-dns,pod-template-hash=6d8c4cb4d
coredns-6d8c4cb4d-8jshq 1/1 Running 7 (4d22h ago) 21d k8s-app=kube-dns,pod-template-hash=6d8c4cb4d
etcd-k8s-master 1/1 Running 7 (4d22h ago) 21d component=etcd,tier=control-plane
kube-apiserver-k8s-master 1/1 Running 7 (4d22h ago) 21d component=kube-apiserver,tier=control-plane
kube-controller-manager-k8s-master 1/1 Running 7 (4d22h ago) 21d component=kube-controller-manager,tier=control-plane
kube-proxy-6ghr7 1/1 Running 7 (4d22h ago) 21d controller-revision-hash=5747ffb5d,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-k6ttp 1/1 Running 7 (4d22h ago) 21d controller-revision-hash=5747ffb5d,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-rpmbk 1/1 Running 8 (4d22h ago) 21d controller-revision-hash=5747ffb5d,k8s-app=kube-proxy,pod-template-generation=1
kube-scheduler-k8s-master 1/1 Running 7 (4d22h ago) 21d component=kube-scheduler,tier=control-plane

为Pod资源新增加一个Label标签

[root@k8s-master ~]# kubectl label po nginx-demo author=xiaoliu
pod/nginx-demo labeled
[root@k8s-master ~]# kubectl get po --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
nginx-demo   1/1     Running   0          15m   author=xiaoliu,test=1.0.0,type=app
[root@k8s-master ~]#

修改Pod资源的Label标签

直接修改会报错,其实和添加Label一样,但是会提示已经存在,我们可以加上 --overwrite 选项

[root@k8s-master ~]# kubectl label po nginx-demo author=liu
error: 'author' already has a value (xiaoliu), and --overwrite is false
[root@k8s-master ~]# kubectl label po nginx-demo author=liu --overwrite
pod/nginx-demo unlabeled
[root@k8s-master ~]# kubectl get po --show-labels
NAME         READY   STATUS    RESTARTS   AGE   LABELS
nginx-demo   1/1     Running   0          19m   author=liu,test=1.0.0,type=app
[root@k8s-master ~]#

也可以直接通过 kubectl edit po nginx-demo 修改pod配置文件的方式进行修改,保存退出后,再查看,也可以看到有生效

 

通过Selector选择器的方式查询符合条件Label的Pod资源 

[root@k8s-master ~]# kubectl get po -l type=app
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          36m
[root@k8s-master ~]# kubectl get po -l type=app
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          36m
[root@k8s-master ~]# kubectl get po -l type=app1
No resources found in default namespace.
[root@k8s-master ~]# kubectl get po -l 'test in (1.0.0,1.1.0,1.2.0)'
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          38m
[root@k8s-master ~]# kubectl get po -l version!=1.2.0,type=app
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          48m
[root@k8s-master ~]# kubectl get po -l test!=1.2.0,type=app
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          49m
[root@k8s-master ~]# kubectl get po -l test!=1.0.0,type=app
No resources found in default namespace.
[root@k8s-master ~]#

多个条件,是and/和/与的逻辑,在写的时候,也可以写上不存在的标签,也是不会报错的

 

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17707243.html