工作负载

工作负载

Pod一样,工作负载也是kubernetes中的一种资源,同样使用资源清单进行描述。

使用工作负载可以批量管理Pod

ReplicaSet

ReplicaSet维护一组在任何时候都处于运行状态的 Pod 副本的稳定集合。 因此,它通常用来保证给定数量的、完全相同的 Pod 的可用性。

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
matchExpressions:
- {key: tier, operator: In, values: [cache]}
- {key: environment, operator: NotIn, values: [dev]}
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3

格式

  • kind: 必须写为ReplicaSet

  • metadata: 元数据

    • name: 名称, 生成的pod的名称会基于它

    • labels: RC的标签

  • spec: 描述

    • replicas: 一个整数,表示目标pod数量

    • selector: 匹配方式, 匹配pod副本是否为有效pod

      • matchLabels: k-v键值对匹配方式见下面的匹配方式

      • matchExpressions: 匹配表达式,较复杂,这里不介绍

    • template: 模板,RS会依据此模板生成pod的副本

      • metadata: pod的元数据

        • labels: pod的标签,这里用于被selector用于判断是否为有效pod
      • spec: pod的容器的描述,见文章[资源清单与Pod](资源清单与Pod | eustrain的小站

匹配方式

spec>selector>matchLabels是Pod的labels的子集

ReplicationController

ReplicaSet基本相同,但不支持matchExpressions,这里不多介绍,可以把ReplicaSet看作ReplicationController的升级版,当需要使用ReplicationController的时候使用ReplicaSet即可。

RC的kind字段要准确写为ReplicationController

Deployment

DeploymentPods和ReplicaSets提供声明式更新能力。

虽然可用于命令式更新,但这样使用Deployment就没什么意义了

Deployment可以看作ReplicaSet的封装

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

格式

  • apiVersion: 使用的api版本

  • kind: 必须准确写为Deployment

  • metadata: 元数据

    • name: Deployment的名称

    • labels: Deployment具有的标签

  • spec: 描述,只有.spec.template 和 .spec.selector 是必需的字段。

    • template: 一个pod模板,见ReplicaSet

    • selector: 选择算符,同见ReplicaSet

    • replicas: 副本数量,默认是1

部署策略

声明式部署

文章目录
  1. 1. 工作负载
  2. 2. ReplicaSet
    1. 2.1. Demo
    2. 2.2. 格式
    3. 2.3. 匹配方式
  3. 3. ReplicationController
  4. 4. Deployment
    1. 4.1. Demo
    2. 4.2. 格式
    3. 4.3. 部署策略
|