Controllers
Controller are using to monitor k8s objects and respond accordingly.
ReplicaSet (old term ReplicationController)
- High availability
- Load balancing + Scaling
A ReplicaSet ensures that a specified number of pod replicas are running at any given time.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The selector in the ReplicaSet definition helps the ReplicaSet identify which pods belong to it. The selector enables the ReplicaSet to manage pods that were not created as part of the ReplicaSet creation.
For example, if there were pods created before the creation of the ReplicaSet that matched the labels specified in the selector, the ReplicaSet would also consider those pods when creating replicas.
The process of creating ReplicaSet
- K8s client (kubectl) sent a request to the API server requesting the creation of ReplicaSet
- The controller is watching the API server for new events and it detected that there is a new ReplicaSet object
- The controller creates 2 new pod definitions because we have configured replica value as 2 in the above example
- The scheduler is watching the API server for new events and it detected that there are 2 unasigned pods
- The scheduler decided which node to assign the Pod and sent that information to the API server
- Kublet is also watching the API server. It detected that the 2 pods were assigned to the node it is running on
- Kublet sent request to Docker requesting the creation of the containers that form the Pod.
- Finally, Kublet sent a request to the API server notifying it that the pods were created successfully
Deployments
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 provides declarative updates to Pods along with a lot of other useful features. Therefore, k8s recommend using Deployments instead of directly using ReplicaSets, unless you require custom update orchestration or don't require updates at all.
Deployment manages the overall lifecycle of an application, including rolling updates and rollbacks, while a ReplicaSet ensures that a specified number of identical replicas of the application are running at any given time. Deployments use ReplicaSets to manage scaling and ensure the desired state is achieved.
This actually means that you may never need to manipulate ReplicaSet objects: use a Deployment instead, and define your application in the spec section.
Define a zero-downtime deployment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
spec.minReadySeconds
: defines the number of seconds before k8s starts considering the Pod healthy. The default value is0
, meaning that the Pods will be considered available as soon as they are ready and, when specifiedlivenessProbe
returns OK.spec.revisionHistoryLimit
: defines the number of oldReplicaSet
we can rollback (default value is10
)spec.strategy.type
: can be eitherRollingUpdate
orRecreate
type.
Deployment strategies
Recreate
The recreate strategy is a dummy deployment which consists of shutting down version A then deploying version B after version A is turned off. This technique implies downtime of the service that depends on both shutdown and boot duration of the application.
Pros:
- Easy to setup
- Application state entirely renewed.
Cons:
- High impact on the user, expect downtime that depends on both shutdown and boot duration of the application.
Rolling update
The rolling update deployment strategy consists of slowly rolling out a version of an application by replacing instances one after the other until all the instances are rolled out.
Pros:
- Easy to setup
- Version is slowly released across instances
- Convenient for stateful applications that can handle rebalancing of the data.
Cons:
- Rollout/rollback can take time.
- Supporting multiple APIs is hard.
- No control over traffic.