kubernets/kubernetes-MD/kubernetes资源对象Label.md

131 lines
4.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1><center>kubernetes资源对象label</center></h1>
------
## 一:标签
#### 1.pod标签
作用:
解决同类型的资源对象越来越多,为了更好的管理,按照标签分组
常见标签:
release版本stable稳定版、canary金丝雀版本、可以理解为测试版、beta测试版
environment环境变量dev开发、qa测试、production生产
application应用ui、as应用软件、pc、sc
tier架构层级frontend前端、backend后端、cache缓存、隐藏
partition分区customerA客户A、customerB客户B
track品控级别daily每天、weekly每周
注意:
K8s集群中虽然没有对有严格的要求但是标签还是要做到见名知意方便自己也方便别人
使用:
为现有的pod添加一个标签
```shell
[root@master nginx]# kubectl label pod nginx-xingdian app=nginx -n default
pod/nginx-xingdian labeled
注意:
-n: 指定namespect名字空间
```
查看pod标签
```shell
[root@master nginx]# kubectl get pods --show-labels -n default
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-585449566-9459t 1/1 Running 0 99m app=nginx,pod-template-hash=585449566
nginx-deployment-585449566-z6qs8 1/1 Running 0 94m app=nginx,pod-template-hash=585449566
nginx-xingdian 1/1 Running 0 67m app=nginx,run=nginx-xingdian
```
删除标签
```shell
[root@master nginx]# kubectl label pod nginx-xingdian app- -n default
pod/nginx-xingdian labeled
```
修改标签
```shell
[root@master nginx]# kubectl label pod nginx-xingdian release=stable -n default
pod/nginx-xingdian labeled
[root@master nginx]# kubectl get pods --show-labels -n default
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-585449566-9459t 1/1 Running 0 112m app=nginx,pod-template-hash=585449566
nginx-deployment-585449566-z6qs8 1/1 Running 0 106m app=nginx,pod-template-hash=585449566
nginx-xingdian 1/1 Running 0 80m app=xingdian,release=stable,run=nginx-xingdian
[root@master nginx]# kubectl label pod nginx-xingdian release=beta --overwrite -n default
pod/nginx-xingdian labeled
[root@master nginx]# kubectl get pods --show-labels -n default
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-585449566-9459t 1/1 Running 0 112m app=nginx,pod-template-hash=585449566
nginx-deployment-585449566-z6qs8 1/1 Running 0 107m app=nginx,pod-template-hash=585449566
nginx-xingdian 1/1 Running 0 81m app=xingdian,release=beta,run=nginx-xingdian
```
标签与标签选择器的关系:
如果标签有多个,标签选择器选择其中一个,也可以关联成功!
如果选择器有多个,那么标签必须满足条件,才可关联成功!
标签选择器:标签的查询过滤条件
基于等值关系的equality-based”=“、”==“、”!=“前两个等于,最后一个不等于
基于集合关系set-basedin、notin、exists三种
使用标签选择器的逻辑:
同时指定的多个选择器之间的逻辑关系为”与“操作
使用空值的标签选择器意味着每个资源对象都将被选择中
空的标签选择器无法选中任何资源
#### 2.node节点标签
查看标签:
```shell
[root@master nginx]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master=
node-1 Ready <none> 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-1,kubernetes.io/os=linux
node-2 Ready <none> 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-2,kubernetes.io/os=linux
```
增加标签:
```shell
[root@master nginx]# kubectl label nodes node-1 kubernetes.io/username=xingdian
node/node-1 labeled
```
减少标签:
```shell
[root@master nginx]# kubectl label nodes node-1 a-
node/node-1 labeled
```
修改标签:
```shell
[root@master nginx]# kubectl label nodes node-1 kubernetes.io/username=diandian --overwrite
node/node-1 labeled
```