204 lines
5.1 KiB
Markdown
204 lines
5.1 KiB
Markdown
|
### 一、标签
|
|||
|
|
|||
|
在 Kubernetes(k8s)中,标签(Labels)和标签选择器(Label Selectors)是用于组织和选择资源子集的关键概念。它们是元数据的一部分,可以附加到几乎所有的 Kubernetes 对象上,例如 Pods、Services、Deployments 等
|
|||
|
|
|||
|
标签是键值对,附加到 Kubernetes 对象上,用于指定这些对象的属性,这样可以通过这些属性对它们进行识别和组织。一个标签由一个键(key)和一个值(value)组成,例如:
|
|||
|
|
|||
|
```yaml
|
|||
|
labels:
|
|||
|
app: myapp
|
|||
|
env: production
|
|||
|
```
|
|||
|
|
|||
|
### 二、标签选择器
|
|||
|
|
|||
|
标签选择器用于从一组对象中选择匹配特定标签的对象。选择器可以是等式式的(equality-based)或者集合式的(set-based)。
|
|||
|
|
|||
|
- 等式式选择器(Equality-based Selectors):
|
|||
|
- `=` 或 `==`:选择标签与指定值相等的对象
|
|||
|
- `!=`:选择标签与指定值不相等的对象
|
|||
|
- 集合式选择器(Set-based Selectors):
|
|||
|
- `in`:选择标签值在指定集合中的对象
|
|||
|
- `notin`:选择标签值不在指定集合中的对象
|
|||
|
- `exists`(仅通过键指定):选择具有指定键的对象,不论其值为何
|
|||
|
- `does not exist`(通过键前的 `!` 指定):选择不具有指定键的对象
|
|||
|
|
|||
|
选择器可以在 kubectl 命令中使用,也可以在配置文件中指定,用于定义服务或者复制控制器等应该处理哪些 Pods
|
|||
|
|
|||
|
- 使用等式式选择器来获取所有 “env” 标签值为 “production” 的 Pods:
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl get pods -l env=production
|
|||
|
```
|
|||
|
|
|||
|
- 使用集合式选择器来选择 “app” 标签值为 “myapp” 或 “yourapp” 的 Pods:
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl get pods -l 'app in (myapp, yourapp)'
|
|||
|
```
|
|||
|
|
|||
|
- yaml中指定标签选择器
|
|||
|
|
|||
|
```yaml
|
|||
|
apiVersion: apps/v1
|
|||
|
kind: Deployment
|
|||
|
metadata:
|
|||
|
name: myapp-deployment
|
|||
|
spec:
|
|||
|
replicas: 2
|
|||
|
selector:
|
|||
|
matchLabels:
|
|||
|
app: myapp
|
|||
|
template:
|
|||
|
metadata:
|
|||
|
labels:
|
|||
|
app: myapp
|
|||
|
env: production
|
|||
|
spec:
|
|||
|
containers:
|
|||
|
- name: myapp-container
|
|||
|
image: myapp:1.0
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### 三、标签操作
|
|||
|
|
|||
|
#### 1. 添加标签
|
|||
|
|
|||
|
- 创建 Kubernetes 资源(如 Pods、Deployments、Services 等)时,您可以直接在资源的定义文件中添加标签。例如,为一个 Pod 添加标签:
|
|||
|
|
|||
|
```yaml
|
|||
|
apiVersion: v1
|
|||
|
kind: Pod
|
|||
|
metadata:
|
|||
|
name: example-pod
|
|||
|
labels:
|
|||
|
app: myapp
|
|||
|
spec:
|
|||
|
containers:
|
|||
|
- name: myapp-container
|
|||
|
image: myapp:latest
|
|||
|
```
|
|||
|
|
|||
|
- kubectl 添加标签
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl label pods example-pod app=myapp
|
|||
|
```
|
|||
|
|
|||
|
#### 2. 更新标签
|
|||
|
|
|||
|
- 命令行更新标签
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl label pods example-pod app=newapp --overwrite
|
|||
|
```
|
|||
|
|
|||
|
- 通过修改资源定义yaml文件,更新标签,`pod-definition.yaml` 是包含了对标签的更改的资源定义文件
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl apply -f pod-definition.yaml
|
|||
|
```
|
|||
|
|
|||
|
#### 3. 删除标签
|
|||
|
|
|||
|
- 命令行删除标签
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl label pods example-pod app-
|
|||
|
```
|
|||
|
|
|||
|
#### 4. 按照标签查询资源
|
|||
|
|
|||
|
- 命令行查询标签,列出所有带有 `app=myapp` 标签的 Pods
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl get pods -l app=myapp
|
|||
|
```
|
|||
|
|
|||
|
#### 5. 查看资源上的所有标签
|
|||
|
|
|||
|
- 命令行指令操作,列出example-pod的所有标签
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl describe pods example-pod
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### 四、标签选择器操作
|
|||
|
|
|||
|
#### 1. 等式选择器(Equality-based Selectors)
|
|||
|
|
|||
|
- 包括以下使用方式:
|
|||
|
|
|||
|
- `=` 或 `==`:选择具有特定键和值的资源
|
|||
|
|
|||
|
- `!=`:选择不具有特定键和值的资源
|
|||
|
|
|||
|
- 举例:
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl get pods -l env=production # 生产环境的pod
|
|||
|
kubectl get pods -l app=frontend # 前端 pod
|
|||
|
kubectl get pods -l 'app!=frontend' # 非前端的pod
|
|||
|
```
|
|||
|
|
|||
|
#### 2. 集合选择器(**Set-based Selectors**)
|
|||
|
|
|||
|
- 包括以下使用方式:
|
|||
|
- `in`:选择标签值属于指定集合的资源
|
|||
|
- `notin`:选择标签值不属于指定集合的资源
|
|||
|
- `exists`(只指定键):选择存在特定键的资源(不关心值)
|
|||
|
- `does not exist`(通过在键前加 `!` 表示):选择不存在特定键的资源
|
|||
|
- 命令行使用举例:
|
|||
|
|
|||
|
```bash
|
|||
|
kubectl get pods -l 'env in (production, qa)' # 环境为生产环境或QA环境的pod
|
|||
|
kubectl get pods -l 'app notin (frontend, backend)' # 不是前端也不是后端的app的pod
|
|||
|
kubectl get pods -l 'tier' # 标签包含tier的pod
|
|||
|
kubectl get pods -l '!tier' # 不含有 “tier” 标签的 pod
|
|||
|
```
|
|||
|
|
|||
|
- 资源定义清单中使用举例:
|
|||
|
|
|||
|
- Deployment 或 Service 中使用选择器来简单确定哪些pod应该被包含在内:
|
|||
|
|
|||
|
```yaml
|
|||
|
apiVersion: apps/v1
|
|||
|
kind: Deployment
|
|||
|
metadata:
|
|||
|
name: myapp-deployment
|
|||
|
spec:
|
|||
|
replicas: 3
|
|||
|
selector:
|
|||
|
matchLabels:
|
|||
|
app: myapp
|
|||
|
template:
|
|||
|
metadata:
|
|||
|
labels:
|
|||
|
app: myapp
|
|||
|
```
|
|||
|
|
|||
|
- 实用复杂选择器选择pod:
|
|||
|
|
|||
|
```yaml
|
|||
|
apiVersion: apps/v1
|
|||
|
kind: Deployment
|
|||
|
metadata:
|
|||
|
name: myapp-deployment
|
|||
|
spec:
|
|||
|
replicas: 3
|
|||
|
selector:
|
|||
|
matchExpressions:
|
|||
|
- {key: app, operator: In, values: [myapp]}
|
|||
|
- {key: tier, operator: NotIn, values: [frontend]}
|
|||
|
template:
|
|||
|
metadata:
|
|||
|
labels:
|
|||
|
app: myapp
|
|||
|
tier: backend
|
|||
|
```
|
|||
|
|
|||
|
|