[CKA备考实验][BASIC]1.2 K8S资源对象的标签基本操作

Posted Greyplayground

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CKA备考实验][BASIC]1.2 K8S资源对象的标签基本操作相关的知识,希望对你有一定的参考价值。

在K8S中Label是一等公民,跟Pod有相同的地位,标签不仅可以提供给工程师查看,也是可以被K8S系统玩出各种花样来
在配置文件中可以为资源对象打上多个标签
每个标签都是键值对,我们可以将“键”或者“键值对”作为过滤条件过滤出我们想要的资源对象,例如Pods,Namespaces或者Nodes
操作标签有两种手段,一是命令行,二是配置文件

例:

我们为一个名为my-nginx的Pod创建几个自定义标签

1. 首先创建该Pod:

# vim my-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: my-nginx
  name: my-nginx
spec:
  containers:
  - image: nginx:1.23
    name: my-nginx
    resources: 
  dnsPolicy: ClusterFirst
  restartPolicy: Always
# kubectl apply -f my-nginx.yaml 
pod/my-nginx created
# kubectl get pods 
NAME       READY   STATUS    RESTARTS       AGE
busybox    1/1     Running   48 (12m ago)   2d
my-nginx   1/1     Running   0              2m12s
nginx      1/1     Running   0              2d

查看一下该Pod是否存在默认的标签值。从结果中我们能够看到该Pod的默认标签是 Labels: run=my-nginx

# kubectl describe pods my-nginx
Name:         my-nginx
Namespace:    default
Priority:     0
Node:         node-2/222.1.1.22
Start Time:   Tue, 11 Oct 2022 14:01:15 +0000
Labels:       run=my-nginx
Annotations:  cni.projectcalico.org/containerID: cb62225509217afb6e4b3e6777b76a3ef19139f244aca6774330d21905061a7c
              cni.projectcalico.org/podIP: 10.200.247.2/32
              cni.projectcalico.org/podIPs: 10.200.247.2/32
Status:       Running
...

2. 然后我们用两种方式为该Pod增删标签:

2.1 命令行添加:

为该Pod增加一个标签test1=aaa

# kubectl label pods my-nginx test1=aaa
pod/my-nginx labeled

再次查看该Pod的标签情况,可以发现标签信息中增加了一个名为 test1=aaa 的标签

# kubectl describe pods my-nginx
Name:         my-nginx
Namespace:    default
Priority:     0
Node:         node-2/222.1.1.22
Start Time:   Tue, 11 Oct 2022 14:01:15 +0000
Labels:       run=my-nginx
              test1=aaa
Annotations:  cni.projectcalico.org/containerID: cb62225509217afb6e4b3e6777b76a3ef19139f244aca6774330d21905061a7c
              cni.projectcalico.org/podIP: 10.200.247.2/32
              cni.projectcalico.org/podIPs: 10.200.247.2/32
Status:       Running

通过以下4种方法我们都可以过滤出我们创建的名为my-nginx的Pod

root@node-1:~# kubectl get pods -l test1
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          27m

root@node-1:~# kubectl get pods -l test1=aaa
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          27m

root@node-1:~# kubectl get pods -l run
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          28m

root@node-1:~# kubectl get pods -l run=my-nginx
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          28m

我们怎么通过命令行删除刚刚增加的标签呢?在标签的键名后加一个减号就OK啦!

# kubectl label pods my-nginx test1-    
pod/my-nginx unlabeled

如果想要覆盖原有标签键的值,则执行如下指令。注意 –overwrite是一个必要的后缀,否则将报错:

# kubectl label pods my-nginx test1=bbb --overwrite
pod/my-nginx labeled

再次查看该Pod的标签,我们使用一个新的指令来查询,该指令就是在get指令后添加后缀 –show-labels
如此一来我们可以很方便的查询Pod当前携带的标签键值对

# kubectl get pods my-nginx --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
my-nginx   1/1     Running   0          55m   run=my-nginx,test1=bbb

总结一下跟标签相关的指令

增:kubectl label pods my-nginx test1=aaa

删:kubectl label pods my-nginx test1-

改:kubectl label pods my-nginx test1=bbb --overwrite

查:
kubectl describe pods my-nginx
kubectl get pods -l test1
kubectl get pods -l test1=aaa
kubectl get pods my-nginx --show-labels

2.2 配置文件添加:

我们将my-nginx的配置文件修改为如下所示,即增加了一个标签 test2: bbb

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: my-nginx
    test2: bbb
  name: my-nginx
spec:
  containers:
  - image: nginx:1.23
    name: my-nginx
    resources: 
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: 

然后重新apply该配置文件

# kubectl apply -f my-nginx.yaml 
pod/my-nginx configured

查看该Pod的标签信息,我们发现my-nginx的标签信息中增加了标签test2=bbb,但是之前通过命令行添加的test1=bbb仍然存在,命令行增加的标签不会被配置文件中的标签配置

# kubectl get pods --show-labels
NAME       READY   STATUS    RESTARTS       AGE    LABELS
busybox    1/1     Running   49 (23m ago)   2d1h   <none>
my-nginx   1/1     Running   0              73m    run=my-nginx,test1=bbb,test2=bbb
nginx      1/1     Running   0              2d1h   <none>

开脑洞时间到,非战斗人员请迅速离场,并空降到下一小节

在这里我们思绪飞扬一下,如果配置文件中将命令行配置的标签test1=bbb做一下修改,是否会被覆盖呢?
首先修改配置文件如下所示:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: my-nginx
    test1: abc
    test2: bbb
  name: my-nginx
spec:
  containers:
  - image: nginx:1.23
    name: my-nginx
    resources: 
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: 

然后应用该配置文件:

# kubectl apply -f my-nginx.yaml 
pod/my-nginx configured

揭晓答案的时候到了,查看此时my-nginx的标签信息:
答案是:配置文件将命令行生成的标签键的键值给覆盖掉了,从原来的bbb变为abc

# kubectl get pods my-nginx --show-labels     
NAME       READY   STATUS    RESTARTS   AGE   LABELS
my-nginx   1/1     Running   0          81m   run=my-nginx,test1=abc,test2=bbb

那么命令行是否可以将test1=abc给改回来呢?

root@node-1:~# kubectl label pods my-nginx test1=bbb --overwrite
pod/my-nginx labeled
root@node-1:~# kubectl get pods my-nginx --show-labels
NAME       READY   STATUS    RESTARTS   AGE   LABELS
my-nginx   1/1     Running   0          87m   run=my-nginx,test1=bbb,test2=bbb

答案是:可以
结论:只要键值对的键名不同,命令行和配置文件就相安无事,否则必将是一场标签争夺战

以上就是我们的废话脑洞

3. 标签使用案例

标签的作用可以等同于Excel表格中的过滤功能

实验场景:我们有3个Pod都在运行nginx,属性如下表所示

主机版本部门
nginx1oldtest
nginx2olddev
nginx3newtest

按照如上要求为nginx1,nginx2,nginx3打上了标签

root@node-1:~# kubectl get pods --show-labels            
NAME       READY   STATUS      RESTARTS       AGE     LABELS
busybox    0/1     Completed   49 (60m ago)   2d2h    <none>
my-nginx   1/1     Running     0              109m    run=my-nginx,test1=bbb,test2=bbb
nginx      1/1     Running     0              2d1h    <none>
nginx1     1/1     Running     0              7m6s    department=test,special=special,version=old
nginx2     1/1     Running     0              7m3s    department=dev,version=old
nginx3     1/1     Running     0              6m59s   department=test,version=new

下面我们开始通过标签来选择性的查看pod:

筛选部门为test的Pod

root@node-1:~# kubectl get pods -l department=test
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          9m1s
nginx3   1/1     Running   0          8m54s

筛选部门为dev的Pod

root@node-1:~# kubectl get pods -l department=dev
NAME     READY   STATUS    RESTARTS   AGE
nginx2   1/1     Running   0          9m29s

筛选版本为旧的Pod

root@node-1:~# kubectl get pods -l version=old
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          10m
nginx2   1/1     Running   0          10m

筛选版本为新的Pod

root@node-1:~# kubectl get pods -l version=new
NAME     READY   STATUS    RESTARTS   AGE
nginx3   1/1     Running   0          10m

筛选出带有特殊标签的Pod

root@node-1:~# kubectl get pods -l special
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          10m

以上是关于[CKA备考实验][BASIC]1.2 K8S资源对象的标签基本操作的主要内容,如果未能解决你的问题,请参考以下文章

[CKA备考实验][BASIC]1.1资源对象的YAML文件模板生成

[CKA备考实验][BASIC]1.1资源对象的YAML文件模板生成

[CKA备考实验][Service] 3.1 创建ClusterIP和NodePort类型的Service

[CKA备考实验][Service] 3.1 创建ClusterIP和NodePort类型的Service

[CKA备考实验][Pod]2.1 Pod的关键属性

[CKA备考实验][ingress-nginx] 4.1如何优雅部署自己的ingress-nginx环境