[TOC] # 介绍 * ReplicaSet(RS)是新一代ReplicationController(RC) * 与RC区别在于能支持更多种类的**匹配模式** * RS被Deployment用于Pod的创建、删除和更新 * RS能确保运行指定数量的Pod * Deployment管理ReplicatSet * 通常作为Deployment的理想状态参数使用 * Deployment、ReplicaSet和Pod的关系如下 ![](https://img.kancloud.cn/9b/72/9b72a05f049fd43f3af4a84a89401ebd_908x543.png) # 示例 `vi frontend.yaml` ``` 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: [frontend]} template: metadata: labels: app: guestbook tier: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v3 resources: requests: cpu: 100m memory: 100Mi env: - name: GET_HOSTS_FROM value: dns # If your cluster config does not include a dns service, then to # instead access environment variables to find service host # info, comment out the 'value: dns' line above, and uncomment the # line below. # value: env ports: - containerPort: 80 ``` 创建Pod ```shell $ kubectl create -f http://k8s.io/examples/controllers/frontend.yaml replicaset.apps/frontend created $ kubectl describe rs /frontend Name: frontend Namespace: default Selector: tier=frontend,tier in (frontend) Labels: app=guestbook tier=frontend Annotations: <none> Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=guestbook tier=frontend Containers: php-redis: Image: gcr.io/google_samples/gb-frontend:v3 Port: 80/TCP Requests: cpu: 100m memory: 100Mi Environment: GET_HOSTS_FROM: dns Mounts: <none> Volumes: <none> Events: FirstSeen LastSeen Count From SubobjectPath Type Reason Message --------- -------- ----- ---- ------------- -------- ------ ------- 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-qhloh 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-dnjpy 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-9si5l $ kubectl get pods NAME READY STATUS RESTARTS AGE frontend-9si5l 1/1 Running 0 1m frontend-dnjpy 1/1 Running 0 1m frontend-qhloh 1/1 Running 0 1m ``` # RS应用 ## 删除 ReplicaSet 和它的 Pod ## horizontal-pod-autoscale(hpa) * Pod 水平自动伸缩(Horizontal Pod Autoscaler)特性, 可以基于CPU利用率自动伸缩 replication controller、deployment和 replica set 中的 pod 数量,(除了 CPU 利用率)也可以 基于其他应程序提供的度量指标[custom metrics](https://git.k8s.io/community/contributors/design-proposals/instrumentation/custom-metrics-api.md) * pod 自动缩放不适用于无法缩放的对象,比如 DaemonSets。 * Pod 水平自动伸缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。 控制器会周期性的获取平均 CPU 利用率,并与目标值相比较后来调整 replication controller 或 deployment 中的副本数量。 ### hpa示例 https://my.oschina.net/u/3330830/blog/1931230?spm=a2c4e.10696291.0.0.224819a4soedV5