☸️ Kubernetes - ReplicaSet
ReplicaSet is a controller for horizontal scaling of pods. ReplicaSet ensures that a specific number of pod replicas are running at any given time.
ReplicaSet is the upgraded version of ReplicationController. You should always first try to use ReplicaSet; they should have the same functionality except ReplicaSet supports more complex selectors.
Consider using Deployment controller instead. ReplicaSet can be used independently, but is mainly used with Deployment to manage the underlying pods.
Deleting a ReplicaSet though REST API won't delete the pods. First scale to 0, then delete the controller. kubectl delete will do this automatically as --cascade=true is the default.
Deleting just the ReplicaSet allows you to replace it. Any other ReplicaSet or Deployment can adopt orphaned pods if the label selector matches.
You can remove pods from a ReplicaSet by changing the labels. Useful for debugging and data recovery.
YAML:
.metadata.labelsthe same as you have in the pod template.spec.replicatells how many pod replicas to maintain.spec.selectormanage all pods that match this selector, make sure no other Deployment or ReplicaSet has pod labels that would match this selector.spec.templatedefined the pod to replicate.spec.template.metadata.labelsmust match selector
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# this replicas value is default
# modify it 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
ports:
- containerPort: 80
kubectl create -f frontend.yaml
kubectl describe rs/frontend
kubectl get pods
You can use Horizontal Pod Autoscalers (HPA) to scale ReplicaSets or Deployments. This does require Metrics Server to be deployed on your cluster.
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: frontend-scaler
spec:
scaleTargetRef:
kind: ReplicaSet
name: frontend
minReplicas: 3
maxReplicas: 10
targetCPUUtilizationPercentage: 50
kubectl create -f hpa.yaml
# or, simply
kubectl autoscale rs frontend