1. 程式人生 > 其它 >|NO.Z.00151|——————————|CloudNative|——|KuberNetes&服務釋出.V02|-------------------------------------------------------|label&selector.v02|label定位pod|

|NO.Z.00151|——————————|CloudNative|——|KuberNetes&服務釋出.V02|-------------------------------------------------------|label&selector.v02|label定位pod|



[CloudNative:KuberNetes&服務釋出.V02]                                                               [Applications.KuberNetes] [|DevOps|k8s|服務釋出|Label&Selector|]








一、Label和selector
### --- 通過label來劃分宿主機部署pod
### --- 假如實驗環境是5個節點

[root@k8s-master01 ~]# kubectl get node -owide
NAME           STATUS     ROLES    AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
k8s-master01   Ready      <none>   10d   v1.20.0   192.168.1.11   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://19.3.15
k8s-master02   Ready      <none>   10d   v1.20.0   192.168.1.12   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://19.3.15
k8s-master03   Ready      <none>   10d   v1.20.0   192.168.1.13   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://19.3.15
k8s-node01     NotReady   <none>   10d   v1.20.0   192.168.1.14   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://19.3.15
k8s-node02     NotReady   <none>   10d   v1.20.0   192.168.1.15   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://19.3.15
### --- 假設k8s-node02是7網段的,給它打個標籤:新增一個label
~~~     比如我們公司和銀行業務往來,銀行為我們拉了一個專線,
~~~     且這個專線不是所有伺服器都可以使用的,
~~~     銀行那邊可能只設置了你7網段的IP才可以訪問該專線。
~~~     我們需要把呼叫銀行API的這個容器部署在7網段的這個容器上,
~~~     所以我們可以使用label給一批node節點搭上一個label標籤,
~~~     這個label就說明它是subnet7的,這樣我們就可以使用nodeSelector部署到我們節點上面

[root@k8s-master01 ~]# kubectl label node k8s-node02 region=subnet7
node/k8s-node02 labeled
### --- 使用node節點把它過濾出來
~~~     過濾出來之後通過nodeSelector去部署它的容器了

[root@k8s-master01 ~]# kubectl get node -l region=subnet7
NAME         STATUS     ROLES    AGE   VERSION
k8s-node02   NotReady   <none>   10d   v1.20.0 
二、通過新增label的形式篩選宿主機
### --- 還可以對其它資源進行label區分

[root@k8s-master01 ~]# kubectl get po --show-labels
NAME          READY   STATUS    RESTARTS   AGE     LABELS
nginx-sfqkz   1/1     Running   0          3h57m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-ts5x4   1/1     Running   0          4h4m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-v245j   1/1     Running   0          4h4m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
### --- 看所有namespace下的容器

~~~     給pod新增一個label
~~~     這個label只是臨時加的,下次更新的時候被替換掉,
~~~     因為它的Deployment並沒有這個labeled,
~~~     所以說不要去更改這個label的標籤,
~~~     若是想要更改的話,需要使用deployment或者高階資源進行更改,
~~~     但是更改的話也不可以直接去更改,只能起一個新的,把新的pod改個名,
~~~     然後把label標籤打上;打上之後部署上去。部署完成之後再把這service指過來。
~~~     service指過來之後再把舊的刪除掉。
[root@k8s-master01 ~]# kubectl get po -A --show-labels
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE     LABELS
default                nginx-sfqkz                                  1/1     Running   0          3h57m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
default                nginx-ts5x4                                  1/1     Running   0          4h5m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
default                nginx-v245j                                  1/1     Running   0          4h4m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
kube-system            calico-kube-controllers-5f6d4b864b-6clrl     1/1     Running   0          9d      k8s-app=calico-kube-controllers,pod-template-hash=5f6d4b864b
kube-system            calico-node-6hbtl                            1/1     Running   0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-77c2f                            1/1     Running   3          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-hrqpt                            1/1     Running   1          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-trkhw                            1/1     Running   0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-z4gkj                            1/1     Running   0          9d      controller-revision-hash=dc4b7567d,k8s-app=calico-node,pod-template-generation=1
kube-system            coredns-867d46bfc6-sk5dp                     1/1     Running   0          9d      k8s-app=kube-dns,pod-template-hash=867d46bfc6
kube-system            metrics-server-595f65d8d5-slhtd              1/1     Running   1          9d      k8s-app=metrics-server,pod-template-hash=595f65d8d5
kubernetes-dashboard   dashboard-metrics-scraper-7645f69d8c-7dd2b   1/1     Running   0          9d      k8s-app=dashboard-metrics-scraper,pod-template-hash=7645f69d8c
kubernetes-dashboard   kubernetes-dashboard-78cb679857-mqccg        1/1     Running   13         9d      k8s-app=kubernetes-dashboard,pod-template-hash=78cb679857 
三、為實驗pod打上標籤
### --- 在這個namespace下的pod新增標籤。

[root@k8s-master01 ~]# kubectl get po --show-labels
NAME          READY   STATUS    RESTARTS   AGE     LABELS
busybox       1/1     Running   0          91s     <none>
nginx-sfqkz   1/1     Running   0          4h7m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-ts5x4   1/1     Running   0          4h15m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-v245j   1/1     Running   0          4h14m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
### --- 為busybox打上標籤

[root@k8s-master01 ~]# kubectl label po busybox app=busybox
pod/busybox labeled
[root@k8s-master01 ~]# kubectl get po --show-labels
NAME          READY   STATUS    RESTARTS   AGE     LABELS
busybox       1/1     Running   0          2m17s   app=busybox                      //可以檢視到這個label已經新增上了
nginx-sfqkz   1/1     Running   0          4h8m    app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-ts5x4   1/1     Running   0          4h15m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
nginx-v245j   1/1     Running   0          4h15m   app=nginx,controller-revision-hash=6495b6bbfc,pod-template-generation=4
四、select過濾出符合標籤的pod,重新啟動該pod
### --- 通過select過濾一下符合我們標籤的;檢視namespace下符合busybox標籤的容器有哪些

[root@k8s-master01 ~]# kubectl get po -A -l app=busybox
NAMESPACE   NAME      READY   STATUS    RESTARTS   AGE
default     busybox   1/1     Running   0          4m5s // 目前環境下只有這一個
### --- 再啟動一個busybox

[root@k8s-master01 ~]# kubectl run busybox --image=busybox:1.28 -n kube-public      //-n指定名稱空間
pod/busybox created 
[root@k8s-master01 ~]# kubectl get po -n kube-public
NAME      READY   STATUS      RESTARTS   AGE
busybox   0/1     Completed   2          35s            // 這個不用去管,因為它沒有前臺啟動的程序,所以就顯示completed狀態
五、再次打上標籤過濾
### --- 給它也打上標籤

[root@k8s-master01 ~]# kubectl label po busybox  app=busybox -n kube-public
pod/busybox labeled 
### --- 再次過濾一下

[root@k8s-master01 ~]# kubectl get po -A -l app=busybox
NAMESPACE     NAME      READY   STATUS             RESTARTS   AGE
default       busybox   1/1     Running            0          7m39s
kube-public   busybox   0/1     CrashLoopBackOff   4          2m16s
### --- 檢視service的標籤
~~~     在建立資源的時候一定要給這個資源新增上標籤,
~~~     這樣我們才能區分這個service是幹嘛用的,它的角色是什麼
~~~     或者對它進行分組,更好的去管理

[root@k8s-master01 ~]# kubectl get svc -A --show-labels
NAMESPACE              NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE   LABELS
default                kubernetes                  ClusterIP   10.96.0.1       <none>        443/TCP                  10d   component=apiserver,provider=kubernetes
default                nginx                       ClusterIP   None            <none>        80/TCP                   8h    app=nginx








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)