Kubernetes-基础知识学习-Label和Selector

1、概念

Label:对k8s中的各种资源镜像分类、分组,添加一个具有特别属性的一个标签。

Selector:通过一个过滤的语法镜像查找到对应的标签资源。

2、给一个node打标签

[root@master01 ~]# kubectl label node work02 region=subnet5
node/work02 labeled

3、通过一个标签过滤(临时添加)

[root@master01 ~]# kubectl get node -l region=subnet5
NAME     STATUS   ROLES    AGE   VERSION
work02   Ready    <none>   9d    v1.18.0

4、创建一个pod

apiVersion:  v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image:  ikubernetes/myapp:v1
  - name: busybox
    imagePullPolicy: IfNotPresent
    image: busybox:latest
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 50"
  nodeSelector: 
    disktype: ssd

5、查看pod的标签

[root@master01 moneyfast]# kubectl get pod  --show-labels
NAME       READY   STATUS    RESTARTS   AGE     LABELS
pod-demo   2/2     Running   2          2m19s   app=myapp,tier=frontend

 6、删除pod的标签

[root@master01 moneyfast]# kubectl label pod pod-demo app-
pod/pod-demo labeled

7、多重筛选标签

[root@master01 moneyfast]# kubectl get pod -A -n kube-system -l 'k8s-app  in (kube-dns,kube-proxy)'
NAMESPACE     NAME                       READY   STATUS    RESTARTS   AGE
kube-system   coredns-7ff77c879f-68nrf   1/1     Running   6          4d23h
kube-system   coredns-7ff77c879f-s55zh   1/1     Running   6          4d23h
kube-system   kube-proxy-dhwv4           1/1     Running   13         9d
kube-system   kube-proxy-m7j22           1/1     Running   13         9d
kube-system   kube-proxy-ndzgt           1/1     Running   13         9d

 

posted @ 2023-03-22 09:25  中仕  阅读(5)  评论(0编辑  收藏  举报