当前位置:网站首页>Kubernetes详解(十一)——标签与标签选择器
Kubernetes详解(十一)——标签与标签选择器
2022-04-23 15:07:00 【永远是少年啊】
今天继续给大家介绍Linux运维相关知识,本文主要内容是Kubernetes中的标签与标签选择器。
一、标签与标签选择器概述
(一)标签
在Kubernetes集群中,标签本身是一个键值对类型的数据,并且可以附着在任何资源对象上,可以在资源对象被创建时就指定,或者是在资源对象创建后添加。在Kubernetes集群中,一个资源对象可以拥有多个标签,多个资源对象也可拥有相同的标签。
Kubernetes集群中标签有严格的格式,其Key的定义可以使用字母、数字而下划线,连字符以及点号,但是只能够以字符或者是数字开头。Value可以为空,也可以使用字母、数字、连字符以及点好,但是首尾必须使用数字或者是字母。
(二)标签选择器
在Kubernetes集群中,标签选择器常用于标签的查询条件或者是选择标准。Kubernetes目前支持两种标签选择器,即基于等值关系和基于集合关系的标签选择器。
基于等值关系的标签选择器有以下三种:
“==”、“=”和“!=”,前两种都表示等于,最后一个表示不等于
基于集合关系的标签选择器有以下四种:
key in (VALUE1,VALUE2,VALUE3……),表示指定的键值存在与后面的集合中。
key notin (VALUE1,VALUE2,VALUE3……),表示指定的键值不存在与后面的集合中。
key,表示存在此键名的标签。
!key,表示不存在此键名的标签。
标签选择器的使用遵循以下逻辑:
1、同时指定多个标签选择器时,这多个标签选择器的逻辑关系为“与”。
2、使用空值的标签选择器意味着选择每个资源对象。
3、空的标签选择器无法选择出任何资源。
注意,在这里,空的标签选择器和空值的标签选择器具有不同的含义。空的标签选择器即没有标签选择器,而空值的标签选择器则意味着虽然有标签选择器,但是该标签选择器的值为空。
二、标签与标签选择器相关命令
接下来,我就来介绍标签与标签选择器的相关命令:
(一)查看Pod对象的标签
我们在查看Pod对象的基础上,添加上–show-labels参数,可以查看Pod对象的标签,例如,执行命令:
kubectl get pods --show-labels
就可以查看当前运行的Pod及其标签,该命令执行结果如下所示:
除此之外,-L参数可以显示Pod对象的指定标签,执行命令:
kubectl get pods -L 【键A】
可以显示所有的Pod对象,对含有键A标签的Pod会显示其值,对不含有键A标签的Pod会显示为空。
例如,执行命令:
kubectl get pods -L label1,label2
可以显示当前的Pod对象以及其label1和label2标签,该命令执行结果如下:
(二)标签选择器筛选标签
在kubectl命令中,-l参数可以用作标签的筛选,例如,执行命令:
kubectl get pods -l label1,label2
可以筛选处同时含有lable1和label2标签的Pod,该命令执行结果如下:
(三)Pod对象创建后添加标签
我们可以在Pod对象创建时就给它指定标签,我们也可以使用label命令在Pod对象创建后添加标签,该命令格式如下:
kubectl label pods/【Pod名】 【标签键】=【标签值】
例如,执行命令:
kubectl label pods/pod-demo-test label3=label3
可以给pod-demo-test的Pod对象添加label3=label3的标签。该命令执行结果如下所示:
(四)修改Pod对象的标签
除了给已经运行的Pod对象添加标签之外,我们还可以修改Pod对象的标签。该操作也需要使用label命令,并且在最后要添加–overwrite的命令。例如,执行命令:
kubectl label pods/pod-demo-test label3=modify --overwrite
可以将该Pod原来的label3=label3修改为label3=modify,该命令执行结果如下:
原创不易,转载请说明出处:https://blog.csdn.net/weixin_40228200
版权声明
本文为[永远是少年啊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_40228200/article/details/124286570
边栏推荐
- SSH connects to the remote host through the springboard machine
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- Sword finger offer II 019 Delete at most one character to get palindrome (simple)
- 分布式事务Seata介绍
- 买卖股票的最佳时机系列问题
- Async void caused the program to crash
- Basic operation of sequential stack
- Realization of four data flow modes of grpc based on Multilingual Communication
- Little red book timestamp2 (2022 / 04 / 22)
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
猜你喜欢
Have you really learned the operation of sequence table?
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
8.5 concise implementation of cyclic neural network
API gateway / API gateway (II) - use of Kong - load balancing
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
Explanation and example application of the principle of logistic regression in machine learning
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
随机推荐
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
8.3 language model and data set
脏读、不可重复读和幻读介绍
eolink 如何助力遠程辦公
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
js——实现点击复制功能
Introduction to dirty reading, unrepeatable reading and phantom reading
C language super complete learning route (collection allows you to avoid detours)
Async void caused the program to crash
LeetCode167-两数之和II-双指针-二分-数组-查找
JS -- realize click Copy function
Explanation and example application of the principle of logistic regression in machine learning
Tencent has written a few words, Ali has written them all for a month
OPPO数据湖统一存储技术实践
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
UML learning_ Day2
22年了你还不知道文件包含漏洞?
Have you learned the basic operation of circular queue?
Daily question - leetcode396 - rotation function - recursion
Leetcode149 - maximum number of points on a line - Math - hash table