K8S 之 POD標籤的應用

1、建立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

2、經過標籤查看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

3、修改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

4、爲現有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

5、經過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
相關文章
相關標籤/搜索