- Hands-On Kubernetes on Windows
- Piotr Tylenda
- 475字
- 2021-06-24 16:54:10
ReplicaSets
Kubernetes builds many powerful concepts on top of Pods, which makes container management easy and predictable. The simplest one is the ReplicaSet API Object (the successor of ReplicationController), which aims at maintaining a fixed number of healthy Pods (replicas) to fulfill certain conditions. In other words, if you say I want three nginx Pods running in my cluster, ReplicaSet does that for you. If a Pod is destroyed, ReplicaSet will automatically create a new Pod replica to restore the desired state.
Let's look at an example ReplicaSet manifest nginx-replicaset.yaml file that creates three replicas of the nginx Pod:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset-example
spec:
replicas: 3
selector:
matchLabels:
environment: test
template:
metadata:
labels:
environment: test
spec:
containers:
- name: nginx
image: nginx:1.17
ports:
- containerPort: 80
There are three main components of the ReplicaSet Spec:
- replicas: Defines the number of Pod replicas that should run using the given template and matching selector. Pods may be created or deleted in order to maintain the required number.
- selector: A label selector, which defines how to identify Pods that the ReplicaSet will acquire. Note that this may have a consequence of acquiring existing bare Pods by ReplicaSet!
- template: Defines the template for Pod creation. Labels used in metadata must positively match the selector.
You can apply the ReplicaSet manifest in a similar manner to how we applied a Pod in the Katacoda playground:
kubectl apply -f https://raw.githubusercontent.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/master/Chapter04/02_replicaset-example/nginx-replicaset.yaml
You can observe how three Pod replicas are created using the following command:
kubectl get pod -o wide -w
ReplicaSets mark the newly created or acquired Pods by assigning themselves to the .metadata.ownerReferences property of the Pod (if you are curious, you can check by using the kubectl get pod <podId> -o yaml command). This means that if you create exactly the same ReplicaSet, with exactly the same selectors but with a different name, for example, nginx-replicaset-example2, they will not steal Pods from each other. However, if you have already created bare Pods with matching labels, such as environment: test, the ReplicaSet will acquire them and may even delete the Pods if the number of replicas is too high!
This can be seen in the following diagram:
Usually, you don't create ReplicaSets on your own as they are not capable of performing rolling updates or rolling back to earlier versions easily. To facilitate such scenarios, Kubernetes provides objects built on top of ReplicaSets: Deployment and StatefulSet. Let's take a look at Deployment first.
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- 深入淺出Java虛擬機(jī):JVM原理與實(shí)戰(zhàn)
- Python高效開(kāi)發(fā)實(shí)戰(zhàn):Django、Tornado、Flask、Twisted(第3版)
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- Web前端應(yīng)用開(kāi)發(fā)技術(shù)
- Elasticsearch Essentials
- 網(wǎng)絡(luò)數(shù)據(jù)采集技術(shù):Java網(wǎng)絡(luò)爬蟲(chóng)實(shí)戰(zhàn)
- Mastering jQuery Mobile
- 軟件測(cè)試分析與實(shí)踐
- 基于JavaScript的WebGIS開(kāi)發(fā)
- Unity 3D UI Essentials
- 深度學(xué)習(xí):基于Python語(yǔ)言和TensorFlow平臺(tái)(視頻講解版)
- C++ Windows Programming
- 計(jì)算機(jī)視覺(jué)增強(qiáng)現(xiàn)實(shí)應(yīng)用平臺(tái)開(kāi)發(fā)