|NO.Z.00151|——————————|CloudNative|——|KuberNetes&服务发布.V02|——|label&selector.v02|label定位pod|
一、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
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)