01 响应式管理标签
01-1 查看资源标签
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 42m <none>
xy 1/1 Running 0 42m <none>
![image-20250526213106987]()
图1 查看资源标签
01-2 给资源打标签
[root@master yaml]# kubectl label pod likexy school=ucas class=bs
pod/likexy labeled
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 45m class=bs,school=ucas
xy 1/1 Running 0 45m <none>
![image-20250526213425901]()
图2 打标签
01-3 修改标签
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 59m class=bs,school=ucas
xy 1/1 Running 0 59m <none>
[root@master yaml]# kubectl label pods likexy class=ys --overwrite
pod/likexy labeled
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 60m class=ys,school=ucas
xy 1/1 Running 0 60m <none>
![image-20250526214852485]()
图3 修改class的标签
01-4 删除标签
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 65m class=ys,school=ucas
xy 1/1 Running 0 65m <none>
[root@master yaml]# kubectl label pods likexy class-
pod/likexy unlabeled
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 66m school=ucas
xy 1/1 Running 0 66m <none>
![image-20250526215553951]()
图4 删除标签
01-5 基于标签过滤数据
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 69m school=ucas
xy 1/1 Running 0 69m <none>
[root@master yaml]# kubectl get pods -l school
NAME READY STATUS RESTARTS AGE
likexy 1/1 Running 0 69m
[root@master yaml]# kubectl get pods -l school!=ucas
NAME READY STATUS RESTARTS AGE
xy 1/1 Running 0 69m
![image-20250526215759021]()
图5 基于标签过滤数据
02 声明式管理标签
[root@master yaml]# cat 07-lable.yaml
apiVersion: v1
kind: Pod
metadata:
name: likexy
labels:
school: ucas
class: ss
spec:
hostNetwork: true
nodeName: node01
containers:
- name: sql
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "yes"
- name: MYSQL_USER
value: "xy"
- name: MYSQL_PASSWORD
value: "123123"
- name: MYSQL_DATABASE
value: "xy"
[root@master yaml]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
likexy 1/1 Running 0 90s class=ss,school=ucas
![image-20250527081902685]()
图6 查看声明式创建Pod的标签
# 声明式和响应式的区别:
# 相同点:
# 都可以管理资源。
# 不同点:
# 声明式修改后需要使用apply指令进行应用才能生效;
# 响应式会立即生效