1. 程式人生 > 其它 >K8S-控制器進階之標籤管理

K8S-控制器進階之標籤管理

控制器進階 標籤管理

Always 總是去倉庫下載, Nerver 從不下載, IfNotPresent:先載入本地,若無再去倉庫下載
自主式pod資源 —不受控制器控制的pod

叢集管理命令

]# kubectl cordon  node1    //標記node1 不被排程,pod將不會被排程到該node節點上面
]# kubectl   get nodes    
    NAME     STATUS                               ROLES    AGE   VERSION
    master   Ready                                master   48d   v1.
17.11 node1 Ready,SchedulingDisabled <none> 48d v1.17.11 //可以看到排程被關閉 node2 Ready <none> 48d v1.17.11

]# kubectl uncordon node1
//取消node1標記,node1恢復排程 叢集node下線,pod驅逐 ]# kubectl drain node1 //node1節點pod驅逐,用於node下線等場景(可下線無狀態服務)

關於label與taint可以參考:Kubernetes對Pod排程指定Node以及Node的Taint 和 Toleration

叢集node標記汙點,實現反親pod與node反親和性

親和性:label 標籤:實現pod與node的親和性,只要標籤相同都會被排程到這個node上面

反親和性:taint:給node標記汙點,只要node有這個標籤,都不會被排程到這個node節點

pod標籤管理

master ~]# kubectl  get pods  -l  app    //只顯示標籤為app的pod
NAME       READY   STATUS             RESTARTS   AGE
pod
-demo 1/2 CrashLoopBackOff 4 4m12s master ~]# kubectl get pods -L app //顯示所有標籤中帶有app的pod NAME READY STATUS RESTARTS AGE APP nginx-9ffc7d87b-4gjtn 1/1 Running 3 4d nginx-9ffc7d87b-5llww 1/1 Running 3 4d nginx-9ffc7d87b-jrq2h 1/1 Running 3 4d nginx-deploy-98c9d6c66-n25dd 1/1 Running 4 6d16h pod-demo 2/2 Running 5 4m32s myapp master ~]# kubectl get pods -l app --show-labels //更詳細的 NAME READY STATUS RESTARTS AGE LABELS pod-demo 1/2 CrashLoopBackOff 5 6m51s app=myapp,tier=frontend master ~]# kubectl get pods -L app,run //顯示多個pod的標籤值 NAME READY STATUS RESTARTS AGE APP RUN nginx-9ffc7d87b-4gjtn 1/1 Running 3 4d nginx nginx-9ffc7d87b-5llww 1/1 Running 3 4d nginx nginx-9ffc7d87b-jrq2h 1/1 Running 3 4d nginx nginx-deploy-98c9d6c66-n25dd 1/1 Running 4 6d16h nginx-deploy pod-demo 1/2 Running 6 8m8s myapp

pod增加或修改標籤

說明:標籤型別為key:vaule型別

master ~]# kubectl   get pods pod-demo --show-labels   //檢視標籤
NAME       READY   STATUS    RESTARTS   AGE   LABELS
pod-demo   2/2     Running   7          13m   app=myapp,tier=frontend

master ~]# kubectl  label pods  pod-demo release=stable    //增加標籤

master ~]# kubectl  get pods  pod-demo --show-labels   //再次檢視標籤
NAME       READY   STATUS             RESTARTS   AGE   LABELS
pod-demo   1/2     CrashLoopBackOff   7          15m   app=myapp,release=stable,tier=frontend

master ~]# kubectl  label pods  pod-demo release=release   --overwrite    //當該標籤已存在值需要加上--overwrite選項

master ~]# kubectl  get pods  pod-demo --show-labels    //再次檢視
NAME       READY   STATUS             RESTARTS   AGE   LABELS
pod-demo   1/2     CrashLoopBackOff   8          20m   app=myapp,release=stable,tier=frontend

標籤選擇器:
等值關係:=,==,!=
集合關係:key in (value1,value2…) 或者 key not in (value1,value2…)
等值關係演示

master ~]# kubectl   get pod   --show-labels
NAME                           READY   STATUS             RESTARTS   AGE     LABELS
nginx-9ffc7d87b-4gjtn          1/1     Running            3          4d1h    pod-template-hash=9ffc7d87b,run=nginx
nginx-9ffc7d87b-5llww          1/1     Running            3          4d1h    pod-template-hash=9ffc7d87b,run=nginx
nginx-9ffc7d87b-jrq2h          1/1     Running            3          4d1h    pod-template-hash=9ffc7d87b,run=nginx
nginx-deploy-98c9d6c66-n25dd   1/1     Running       4      6d16h   pod-template-hash=98c9d6c66,run=nginx-deploy
pod-demo                    1/2     CrashLoopBackOff   9          28m     app=myapp,release=stable,tier=frontend

master ~]# kubectl  label pods nginx-deploy-98c9d6c66-n25dd release=canary   //增加標籤

master ~]# kubectl   get pod   -l   release=stable   --show-labels  //檢視標籤release=stable的pod 

master ~]# kubectl   get pods -l   release  //檢視標籤為release的pod
NAME                           READY   STATUS             RESTARTS   AGE
nginx-deploy-98c9d6c66-n25dd   1/1     Running            4          6d16h
pod-demo                       1/2     CrashLoopBackOff   10         32m

master ~]# kubectl   get pods -l release,app    //檢視標籤為release且等於app的pod

master ~]# kubectl   get pods -l release=stable,app=myapp   //檢視標籤值release=stable,app=myapp的pod

master ~]# kubectl   get pod -l  release!=stable,release!=canary  --show-labels    //檢視標籤值不等於xxx且xxx不等於xxpod

集合關係演示

集合關係:key in (value1,value2...) 或者 key not in (value1,value2...)說明:value必須為非空欄位
                    exists ,noexists  (values欄位必須為空列表)
 
master ~]# kubectl   get pods -l "release in (stable,canary,aaa)"     //或的關係,有一個也行,沒有就算了
NAME                           READY   STATUS             RESTARTS   AGE
nginx-deploy-98c9d6c66-n25dd   1/1     Running            4          6d21h
pod-demo                       1/2     CrashLoopBackOff   65         5h31m

-master ~]# kubectl   get pods -l "release notin (stable,canary,aaa)"    //取反
NAME                    READY   STATUS    RESTARTS   AGE
nginx-9ffc7d87b-4gjtn   1/1     Running   3          4d6h
nginx-9ffc7d87b-5llww   1/1     Running   3          4d6h
nginx-9ffc7d87b-jrq2h   1/1     Running   3          4d6h

nodeSelector 節點標籤選擇器
master ~]# kubectl   label nodes  k8s-node1 disktype=ssd   //對node節點打標籤,在新增資源時,可以指定標籤來執行

master yaml]# vim  pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:     //資源註解
    rt/create-by: cluster-admin   //建立人
spec:
  containers:
    - name: nginx
      image: nginx:latest
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80
    - name: busybox
      image: busybox:latest
      imagePullPolicy: IfNotPresent
      command:
      - "/bin/sh"
      - "-c"
      - "sleep 20"
  nodeSelector:       //Node標籤選擇器
    disktype: ssd       //新建的資源只會建立在擁有disktype: ssd標籤的node上,當多個node擁有,根據排程演算法輪詢
  nodeName: k8s-node2     /或者可以直接指定執行在指定的node節點上