K8S 之 POD标签的应用

一、创建POD时指定相应标签

apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual
  namespace: test
  labels:
    app: web_html         #定义了app标签为:web_html
    env: prod                  #定义了env标签为:prod
spec:
  containers:
    - name: kubia
      image: luksa/kubia
      ports:
        - containerPort: 8080
          protocol: TCP

二、通过标签查看POD

[root@test-nodes1 ~]# kubectl get pod --show-labels -n test      #查看所有pod的标签
NAME                        READY   STATUS    RESTARTS   AGE    LABELS
kubia-manual                1/1     Running   0          47h    app=web_test,env=prod
nginx-dp-856666d759-bfrgj   1/1     Running   0          3d9h   app=nginx-proxy,env=test,pod-template-hash=856666d759
nginx-dp-856666d759-c5p4l   1/1     Running   0          3d9h   app=nginx-proxy,env=test,pod-template-hash=856666d759

[root@test-nodes1 ~]# kubectl get pod -l env=prod -n test   #查看env=prod的标签
NAME           READY   STATUS    RESTARTS   AGE
kubia-manual   1/1     Running   0          47h

三、修改POD标签的属性

[root@test-nodes1 ~]# kubectl get pod -l env=prod -n test
NAME           READY   STATUS    RESTARTS   AGE
kubia-manual   1/1     Running   0          47h
[root@test-nodes1 ~]# kubectl label pod kubia-manual env=debug --overwrite -n test
pod/kubia-manual labeled
[root@test-nodes1 ~]# kubectl get pod --show-labels -n test | grep env=debug
kubia-manual                1/1     Running   0          47h    app=web_test,env=debug

四、为现有POD添加一个标签

[root@test-nodes1 ~]# kubectl label pod kubia-manual mingkang_test=test -n test
pod/kubia-manual labeled
[root@test-nodes1 ~]# kubectl get pod --show-labels -n test | grep kubia-manual
kubia-manual                1/1     Running   0          47h    app=web_test,env=debug,mingkang_test=test

五、通过POD标签,固定到某个NODE上

apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual
  namespace: test
  labels:
    app: web_html
    env: prod
spec:
  nodeSelector:       #节点标签选择器
    gpu: true             #gpu为true的节点
  containers:
    - name: kubia
      image: luksa/kubia
      ports:
        - containerPort: 8080
          protocol: TCP
上一篇:k8s学习笔记


下一篇:Docker,Linux,Kubernetes,postgres常用的命令行(持续更新)