惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
F
Full Disclosure
AI
AI
罗磊的独立博客
博客园 - 【当耐特】
U
Unit 42
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
博客园_首页
H
Help Net Security
量子位
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog RSS Feed
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
爱范儿
爱范儿
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程 开发 Tips(5)
Kubernetes 之 Labels、Selectors
微信公众号 · 2019-08-04 · via 陈少文的网站

Please enable Javascript to view the contents

1. Labels

1.1 什么是 Labels

Labels 是一对关联到对象的键值对。可以在创建对象时,直接添加 Labels ,也可以在创建之后动态修改。

Labels 格式:

1
2
3
4
"labels": {
  "key1" : "value1",
  "key2" : "value2"
}

格式要求:

  • Key,不能重复
  • Value,须以字母或数字开头,可以使用字母、数字、连字符、点和下划线,最长63个字符

1.2 Labels 的用途

用户使用 Labels,对资源对象进行标识、组织和选择。

标签对于系统运行,没有直接意义,也不用于存储结构化或复杂数据。因为,标签将建立索引和反索引,用于查询和监控。

对于附加在资源对象上的复杂结构,应该使用 annotation。

annotation 可以将任意非标识元数据附加到对象上。使用工具和类库等客户端,我们可以检索这些数据。annotation 中的元数据可以是结构化的,也可以是非结构化的,同时对字符范围没有限制。

1.3 Labels 操作

  • 查看名字为 name 的 Pod 详情
1
kubectl describe pods name
  • 给名字为 name 的 Pod 添加 Label: tempLabel=True
1
2
kubectl label pods name tempLabel=True
pod/name labeled
  • 将名字为 name 的 Pod 修改为 Label: tempLabel=False
1
2
kubectl label --overwrite pods name tempLabel=False
pod/name labeled
  • 给所有 Pod 添加 Labels
1
2
3
kubectl label pods --all tempLabel2=True
pod/kube-nginx-7c765ffd95-2pxfk labeled
...
  • 删除名字为 name 的 Pod 中,Key 为 tempLabel 的 Labels

删除时,只需要在 Key 值后面拼上 - 即可。

1
2
kubectl label pods name tempLabel-
pod/name labeled

2. Labels Selectors

2.1 什么是 Labels Selectors

Labels Selectors,即标签选择器。

标签选择器是 Kubernetes 中的核心组成部分。在使用的过程当中,通常多个对象具有相同的标签。通过标签过滤出一组资源对象,批量进行操作。

2.2 Labels Selectors 的类型

Kubernetes 的 API 目前支持两种类型的选择器:

  1. equality-based,基于等式

基于等式的标签条件支持三种操作符:=、==、!=。其中,= 与 == 同义,多个条件可以使用逗号分隔连接。例如:

1
frontend:environment=production, tier!=frontend
  1. set-based,基于集合

基于集合的标签条件支持三种操作符: in , notin 和 exists。例如:

1
2
3
4
environment in (production, qa)
tier notin (frontend, backend)
partition
!partition

2.3 如何使用 Labels Selectors

  • 使用 API 进行查询过滤时,添加 GET 过滤参数即可

基于等式:

?labelSelector=environment%3Dproduction,tier%3Dfrontend

基于集合:

?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29

  • 使用 Kubectl 在 Console 中进行操作

基于等式:

1
kubectl get pods -l environment=production,tier=frontend

基于集合:

1
kubectl get pods -l 'environment in (production),tier in (frontend)'

3. Field Selectors

Field Selectors,字段选择器允许通过字段值过滤,来筛选资源对象。

不同的资源对象,支持的过滤字段不同。所有资源类型都支持 metadata.namemetadata.namespace 字段过滤。

下面是一个使用样例,选择 phase 字段为 Running 的所有 Pod :

1
2
3
4
5
kubectl get pods --field-selector status.phase=Running
NAME                                              READY   STATUS    RESTARTS   AGE
kube-nginx-7c765ffd95-2pxfk                       1/1     Running   3          17d
my-harbor-harbor-chartmuseum-567fb69cb6-q88ss     1/1     Running   0          3d20h
...

微信公众号