Kubernetes进阶实战读书笔记:管理Pod资源对象(标签选择器)
一、标签概述
1、概述
2、App=MyApp
3、App = MyApp, Role = FE
实践中、建议键名及键值能做到到 "见名知义" 且尽可能保持简单
二、管理资源标签
1、资源清单
[root@master chapter4]# cat pod-with-labels.yaml apiVersion: v1 kind: Pod metadata: name: pod-with-labels labels: env: qa tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 protocol: TCP
2、创建显示对象的标签信息
[root@master chapter4]# kubectl apply -f pod-with-labels.yaml pod "pod-with-labels" created [root@master chapter4]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-with-labels 1/1 Running 0 27s env=qa,tier=frontend
3、指定显示有着特定键的标签信息
[root@master chapter4]# kubectl get pods -L env,tier NAME READY STATUS RESTARTS AGE ENV TIER httpget 1/1 Running 4 15d production <none> pod-with-labels 1/1 Running 0 10m production frontend
4、为pod添加标签
[root@master chapter4]# kubectl label pods/httpget env=production pod "httpget" labeled [root@master chapter4]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS httpget 1/1 Running 4 14d env=production mysql-7dfcv 1/1 Running 4 11d app=mysql myweb-7vjgt 0/1 ImagePullBackOff 8 16d app=myweb nginx 1/1 Running 0 22h app=web pod-with-labels 1/1 Running 0 27s env=qa,tier=frontend
5、对于已经附带了指定键名的标签,设定新的键值时需要为命令同事使用 "--overwrit" 命令以强制覆盖原有的键值
[root@master chapter4]# kubectl label pods/pod-with-labels env=production error: 'env' already has a value (qa), and --overwrite is false [root@master chapter4]# kubectl label pods/pod-with-labels env=production --overwrite pod "pod-with-labels" labeled [root@master chapter4]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS httpget 1/1 Running 4 14d env=production mysql-7dfcv 1/1 Running 4 11d app=mysql myweb-7vjgt 0/1 ImagePullBackOff 8 16d app=myweb nginx 1/1 Running 0 22h app=web pod-with-labels 1/1 Running 0 27s env=qa,tier=frontend
用户若期望对某标签之下的资源集合执行某类操作、例如、查看或删除等,则需要先使用 "标签选择器" 挑选出满足条件的资源对象
三、标签选择器
1、支持类型
2、遵循逻辑
3、测试用例
-l 选项能够制定使用标签选择器
1、显示名为env值不为qa的所有pod对象
[root@master chapter4]# kubectl get pods -l "env!=qa" -L env NAME READY STATUS RESTARTS AGE ENV myapp-deploy-5cbd66595b-nzfjz 1/1 Running 0 17h myapp-deploy-5cbd66595b-pldfc 1/1 Running 0 17h myapp-deploy-5cbd66595b-vmtln 1/1 Running 0 17h pod-example 1/1 Running 1 15h production pod-use-hostnetwork 1/1 Running 0 65m pod-with-labels 1/1 Running 0 3m48s testing pod-with-securitycontext 1/1 Running 0 44m
2、显示标签键名env的值不为qa、且标签键名tier的值为frontend的所有pod对象
[root@master chapter4]# kubectl get pods -l "env!=qa,tier=frontend" -L env,tier NAME READY STATUS RESTARTS AGE ENV TIER pod-with-labels 1/1 Running 0 4m31s testing frontend
3、显示标签键名env的值为production或env、且不存在键名为tier的所有pod对象
[root@master chapter4]# kubectl get pods -l "env in (production,dev)" -L env NAME READY STATUS RESTARTS AGE ENV pod-example 1/1 Running 1 15h production
4、显示标签键名env的值为production或dev的所有pod对象
[root@master chapter4]# kubectl get pods -l "env in (production,dev),!tier" -L env,tier -bash: !tier": event not found [root@master chapter4]# kubectl get pods -l 'env in (production,dev),!tier' -L env,tier NAME READY STATUS RESTARTS AGE ENV TIER pod-example 1/1 Running 1 15h production
为了避免shell解释器解析叹号(!),必须要为此类表达式使用单引号
4、小结
四、生产最佳实践(多维度标签使用)
五、Pod结点选择器nodeSelector
1、资源配置清单
[root@master chapter4]# cat pod-with-nodeselector.yaml apiVersion: v1 kind: Pod metadata: name: pod-with-nodeselector labels: env: testing spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 protocol: TCP nodeSelector: disktype: ssd1
2、给node1、node2节点设置disktype=ssd master节点设置disktype=ssd1
[root@master chapter4]# kubectl label nodes node1 disktype=ssd node/node1 labeled [root@master chapter4]# kubectl label nodes node2 disktype=ssd node/node2 labeled [root@master chapter4]# kubectl label nodes master disktype=ssd1 node/master labeled
3、查看具有ssd1的标签node资源
[root@master chapter4]# kubectl get nodes -l 'disktype' -L disktype NAME STATUS ROLES AGE VERSION DISKTYPE master Ready master 2d12h v1.18.6 ssd1 node1 Ready <none> 2d12h v1.18.6 ssd node2 Ready <none> 2d12h v1.18.6 ssd
4、创建资源清单
[root@master chapter4]# kubectl apply -f pod-with-nodeselector.yaml pod/pod-with-nodeselector created
5、验证调度结果
[root@master chapter4]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ...... pod-with-nodeselector 1/1 Running 0 2m7s 10.244.0.2 master <none> <none>
6、查看master节点标签
[root@master chapter4]# kubectl describe node master Name: master Roles: master Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux disktype=ssd1 kubernetes.io/arch=amd64 kubernetes.io/hostname=master kubernetes.io/os=linux node-role.kubernetes.io/master=
7、查看node1节点标签
[root@master chapter4]# kubectl describe node node1 Name: node1 Roles: <none> Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux disktype=ssd kubernetes.io/arch=amd64 kubernetes.io/hostname=node1 kubernetes.io/os=linux
六、资源注解
1、什么是注解?
注解与标签类似、注解也是"键值" 类型的数据,不过他不能用于标签及挑选Kubernetes对象、仅用于为资源提供"元数据" 信息
另外、注解中的元数据不受字符数量的限制、它可大可小、可以为结构化或非结构化形式、也支持使用在标签中进制使用的其他字符
2、应用场景
在Kubernetes的新版本中为某资源引入新字段时、常以注解的方式提供、以避免其增删等变动对用户带来困扰、一旦确定支持使用他们、这些新字段就将引入到资源中并淘汰相关的注解另外、为资源添加注解也可让其他用户快速了解资源的相关信息、例如创建者的身份等、以下为常用的场景案例
1、由声明式配置层(如apply命令)管理的字段:将这些字段定义为注解有助于识别由服务器或客户端设定的默认值、系统自动生成的字段以及自由自动伸缩系统生成的字段
2、构建、发行或镜像相关的信息:例如:时间戳、发型ID、git分支、PR号码、镜像哈希及仓库地址等
3、指向日志、监控、分析或审计仓库的指针
4、由客户端库或工具程序生成的用于调试目的信息:如名称、版本、构建信息等
5、用户或工具程序的来源地信息:例如来自其他生态系统组件的相关对象的url
6、轻量化滚动升级工具的元数据:如config及chechpoints
7、相关人员的电话号码等联系信息:或者执行类似信息的可寻址的目录条目、如网站站点
3、查看注解
[root@master chapter4]# kubectl describe pods pod-example Name: pod-example Namespace: default Priority: 0 Node: node2/192.168.118.20 Start Time: Wed, 05 Aug 2020 17:40:40 +0800 Labels: env=production Annotations: Status: Running ......
4、管理资源注解
1、Annotations可在资源创建是使用 metadata.annotations 字段制定
2、也可随时按需在活动的资源上使用kubectl annotate命令进行附加
[root@master chapter4]# kubectl annotate pods pod-example ilinux.io/created-by="cluster admin" pod/pod-example annotated
查看生成的注解信息
[root@master chapter4]# kubectl describe pods pod-example |grep "Annotations" Annotations: ilinux.io/created-by: cluster admin
3、如果需要在资源创建时的清单中制定,那么使用类似如下的方式即可
apiVersion: v1 kind: Pod metadata: name: pod-example namespace: default annotations: ilinux.io/created-by: cluster admin spec: ......