k8s入門_label標籤、nodeSelector
阿新 • • 發佈:2021-10-04
label 標籤 nodeSelector
什麼是label
Label以key/value鍵值對的形式附加到任何物件上,如Pod,Service,Node, RC(ReplicationController)/RS(ReplicaSet)等。
一個資源如果需要找到其它關聯的資源比如rc匹配pod、deployment匹配rs都是通過selector選擇對應的lable達到關聯其它物件的目的。
k8s物件的label在資源的metadata定義,例如之前學習過的deplyment定義檔案
標籤的增刪改查
label查詢
#查詢某一類資源或者指定某個資源查詢其label資訊 [root@k8s-01 ~]# kubectl get deployment --show-labels NAME READY UP-TO-DATE AVAILABLE AGE LABELS nginx-d 3/3 3 3 2d21h app=nginx wordpress 1/1 1 1 49d app=wordpress,tier=frontend wordpress-mysql 1/1 1 1 49d app=wordpress,tier=mysql You have new mail in /var/spool/mail/root [root@k8s-01 ~]# kubectl get deployment nginx-d --show-labels NAME READY UP-TO-DATE AVAILABLE AGE LABELS nginx-d 3/3 3 3 2d21h app=nginx [root@k8s-01 ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684 nginx-d-659bf7c684-9l9jk 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684 nginx-d-659bf7c684-tmtkl 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684 wordpress-69c467c658-r7c45 1/1 Running 11 49d app=wordpress,pod-template-hash=69c467c658,tier=frontend-pod wordpress-mysql-6c696cddc9-r6dvx 1/1 Running 15 42d app=wordpress,pod-template-hash=6c696cddc9,tier=mysql-pod [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684
label的增加 刪除 修改
#指定物件label的新增和刪除,新增使用key=value,刪除keu-,修改key=value --overwrite [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684 [root@k8s-01 ~]# [root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label pod/nginx-d-659bf7c684-22nc8 labeled [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label [root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test- pod/nginx-d-659bf7c684-22nc8 labeled [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684 [root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label1 pod/nginx-d-659bf7c684-22nc8 labeled [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label1 [root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label2 error: 'test' already has a value (label1), and --overwrite is false [root@k8s-01 ~]# kubectl label pods nginx-d-659bf7c684-22nc8 test=label2 --overwrite pod/nginx-d-659bf7c684-22nc8 labeled [root@k8s-01 ~]# kubectl get pods nginx-d-659bf7c684-22nc8 --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-d-659bf7c684-22nc8 1/1 Running 1 2d20h app=nginx,pod-template-hash=659bf7c684,test=label2
一般建議k8s物件的label在定義檔案中進行定義和修改
nodeselector
nodeselector是pod的專有的選擇器,在資源定義檔案中可以指定pod在哪些node節點上建立。不配置該引數的話k8s會根據自身演算法選擇相應的node來部署pod
不配置nodeselector情況下pod的分佈,如果有多個node則pod一定分佈在多個pod上。
[root@k8s-01 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-d 3/3 3 3 2d21h nginx nginx:1.18 app=nginx [root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d nginx-d-659bf7c684-22nc8 1/1 Running 1 2d21h 10.244.2.52 k8s-03 <none> <none> nginx-d-659bf7c684-9l9jk 1/1 Running 1 2d21h 10.244.2.53 k8s-03 <none> <none> nginx-d-659bf7c684-tmtkl 1/1 Running 1 2d21h 10.244.1.71 k8s-02 <none> <none>
查詢環境node資訊,給node打上label
[root@k8s-01 ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-01 Ready master 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-01,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-02 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-02,kubernetes.io/os=linux k8s-03 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-03,kubernetes.io/os=linux [root@k8s-01 ~]# kubectl label nodes k8s-02 selectortest=k8s-02 node/k8s-02 labeled [root@k8s-01 ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-01 Ready master 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-01,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-02 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-02,kubernetes.io/os=linux,selectortest=k8s-02 k8s-03 Ready <none> 61d v1.15.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-03,kubernetes.io/os=linux
deployment定義檔案中pod部分使用nodeselector指定node建立Pod
#pod物件定義中指定nodeSelector後pod排程的效果 [root@k8s-01 ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE wordpress 1/1 1 1 49d wordpress-mysql 1/1 1 1 49d [root@k8s-01 ~]# kubectl get deployment|grep nginx [root@k8s-01 ~]# kubectl create -f deployment_demo.yml deployment.apps/nginx-d created [root@k8s-01 ~]# kubectl get deployment|grep nginx nginx-d 0/3 3 0 3s [root@k8s-01 ~]# kubectl get pods -o wide|grep nginx-d nginx-d-8668fb97b9-7tw25 1/1 Running 0 43s 10.244.1.75 k8s-02 <none> <none> nginx-d-8668fb97b9-9hfmb 1/1 Running 0 43s 10.244.1.74 k8s-02 <none> <none> nginx-d-8668fb97b9-vlzgt 0/1 ContainerCreating 0 43s <none> k8s-02 <none> <none>