website/docs/user-guide/replicasets.md

103 lines
4.1 KiB
Markdown
Raw Normal View History

2016-03-16 18:23:47 +00:00
---
2016-07-29 17:36:25 +00:00
assignees:
- Kashomon
- bprashanth
- madhusudancs
2016-12-15 20:16:54 +00:00
title: Replica Sets
2016-03-16 18:23:47 +00:00
---
* TOC
{:toc}
## What is a ReplicaSet?
2016-03-16 18:23:47 +00:00
ReplicaSet is the next-generation Replication Controller. The only difference
between a _ReplicaSet_ and a
2016-03-16 18:23:47 +00:00
[_Replication Controller_](/docs/user-guide/replication-controller/) right now is
the selector support. ReplicaSet supports the new set-based selector requirements
2016-03-16 18:23:47 +00:00
as described in the [labels user guide](/docs/user-guide/labels/#label-selectors)
whereas a Replication Controller only supports equality-based selector requirements.
Most [`kubectl`](/docs/user-guide/kubectl/) commands that support
Replication Controllers also support ReplicaSets. One exception is the
2016-03-16 18:23:47 +00:00
[`rolling-update`](/docs/user-guide/kubectl/kubectl_rolling-update/) command. If
you want the rolling update functionality please consider using Deployments
instead. Also, the
[`rolling-update`](/docs/user-guide/kubectl/kubectl_rolling-update/) command is
imperative whereas Deployments are declarative, so we recommend using Deployments
through the [`rollout`](/docs/user-guide/kubectl/kubectl_rollout/) command.
While ReplicaSets can be used independently, today it's mainly used by
2016-03-16 18:23:47 +00:00
[Deployments](/docs/user-guide/deployments/) as a mechanism to orchestrate pod
creation, deletion and updates. When you use Deployments you don't have to worry
about managing the ReplicaSets that they create. Deployments own and manage
their ReplicaSets.
2016-03-16 18:23:47 +00:00
## When to use a ReplicaSet?
2016-03-16 18:23:47 +00:00
A ReplicaSet ensures that a specified number of pod “replicas” are running at any given
time. However, a Deployment is a higher-level concept that manages ReplicaSets and
2016-03-16 18:23:47 +00:00
provides declarative updates to pods along with a lot of other useful features.
Therefore, we recommend using Deployments instead of directly using ReplicaSets, unless
2016-03-16 18:23:47 +00:00
you require custom update orchestration or don't require updates at all.
This actually means that you may never need to manipulate ReplicaSet objects:
use directly a Deployment and define your application in the spec section.
## Example
2016-03-16 18:23:47 +00:00
{% include code.html language="yaml" file="replicasets/frontend.yaml" ghlink="/docs/user-guide/replicasets/frontend.yaml" %}
Saving this config into `frontend.yaml` and submitting it to a Kubernetes cluster should
create the defined ReplicaSet and the pods that it manages.
2016-03-16 18:23:47 +00:00
```shell
$ kubectl create -f frontend.yaml
2016-03-16 18:23:47 +00:00
replicaset "frontend" created
$ kubectl describe rs/frontend
Name: frontend
Namespace: default
Image(s): gcr.io/google_samples/gb-frontend:v3
Selector: tier=frontend,tier in (frontend)
Labels: app=guestbook,tier=frontend
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
No volumes.
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
```
## ReplicaSet as an Horizontal Pod Autoscaler target
2016-03-16 18:23:47 +00:00
A ReplicaSet can also be a target for
2016-03-16 18:23:47 +00:00
[Horizontal Pod Autoscalers (HPA)](/docs/user-guide/horizontal-pod-autoscaling/),
i.e. a ReplicaSet can be auto-scaled by an HPA. Here is an example HPA targeting
the ReplicaSet we created in the previous example.
2016-03-16 18:23:47 +00:00
{% include code.html language="yaml" file="replicasets/hpa-rs.yaml" ghlink="/docs/user-guide/replicasets/hpa-rs.yaml" %}
Saving this config into `hpa-rs.yaml` and submitting it to a Kubernetes cluster should
create the defined HPA that autoscales the target ReplicaSet depending on the CPU usage
2016-03-16 18:23:47 +00:00
of the replicated pods.
```shell
kubectl create -f hpa-rs.yaml
```
2016-11-06 14:32:07 +00:00
Alternatively, you can just use the `kubectl autoscale` command to acomplish the same
2016-03-16 18:23:47 +00:00
(and it's easier!)
```shell
kubectl autoscale rs frontend
```