Understanding maxSurge and maxUnavailable

Bubu Tripathy
3 min readJul 1, 2023

Introduction

In Kubernetes, the maxSurge and maxUnavailable properties are used to control the rolling updates of a deployment. These properties are defined in the spec.strategy.rollingUpdate section of a deployment manifest and play a crucial role in maintaining the availability of the application.

maxSurge

The maxSurge property controls the maximum number of additional pods that can be created during a rolling update. It specifies the number or percentage of pods above the desired replica count that can be temporarily created. During an update, Kubernetes creates new pods to replace the old ones, and the maxSurge property ensures that the total number of pods does not exceed a certain limit.

maxUnavailable

The maxUnavailable property determines the maximum number or percentage of pods that can be unavailable during a rolling update. It specifies the maximum number of pods that can be simultaneously removed from service during the update progresses. By default, Kubernetes terminates one pod at a time while creating new pods, ensuring that the desired replica count is maintained.

Example

To set the values for maxSurge and maxUnavailable, you need to edit the spec.strategy.rollingUpdate section of the deployment manifest. Here’s an example:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080

In this example, we have set both maxSurge and maxUnavailable to 1, indicating that during the rolling update, one additional pod can be created and one pod can be unavailable at a time.

The values for maxSurge and maxUnavailable can be specified in two formats: absolute numbers and percentages.

  • Absolute Numbers: You can set a fixed number of pods as the value for maxSurge and maxUnavailable. For example, maxSurge: 2 means that a maximum of 2 additional pods can be created, and maxUnavailable: 1 indicates that a maximum of 1 pod can be unavailable at a time.
  • Percentages: You can specify the values as percentages of the desired replica count. For example, maxSurge: 50% means that 50% of the desired replica count can be temporarily exceeded, and maxUnavailable: 25% indicates that 25% of the desired replica count can be unavailable.

Let’s explore few scenarios.

Scenario 1: maxSurge: 1, maxUnavailable: 0

  • Desired replica count: 3
  • During the update, Kubernetes creates 1 additional pod at a time while keeping all existing pods running.
  • No pods are removed before the new pods become ready.

Scenario 2: maxSurge: 0, maxUnavailable: 1

  • Desired replica count: 3
  • During the update, no additional pods are created (maxSurge: 0), but one pod can be unavailable (maxUnavailable: 1).
  • Kubernetes terminates one pod at a time, ensuring that the desired replica count is maintained. So, at any given time, there will be 2 pods running and 1 pod unavailable.

Scenario 3: maxSurge: 25%, maxUnavailable: 25%

  • Desired replica count: 4
  • During the update, Kubernetes can create up to 25% of the desired replica count as additional pods (maxSurge: 25%). In this case, it can create a maximum of 1 additional pod.
  • Similarly, up to 25% of the desired replica count can be unavailable (maxUnavailable: 25%). In this case, it can have a maximum of 1 pod unavailable at a time.
  • This allows flexibility during the update process, ensuring that there is no significant impact on the availability of the application.

The actual behavior of maxSurge and maxUnavailable may depend on various factors such as the Kubernetes version, configuration, and resource availability in the cluster. It is important to test and validate the deployment behavior under different scenarios to ensure it meets your requirements.

Conclusion

In this tutorial, we explored the concepts of maxSurge and maxUnavailable in Kubernetes deployments. We learned that these properties play a crucial role in controlling the rolling updates and maintaining application availability during the update process. By setting appropriate values for maxSurge and maxUnavailable, you can ensure a smooth and controlled transition during updates, minimizing any disruption to your application.

--

--

Bubu Tripathy
Bubu Tripathy

Written by Bubu Tripathy

Senior Software Engineer l Microservices l Cloud Computing l DevOps || LinkedIn:https://www.linkedin.com/in/bubu-tripathy-8682187/

Responses (2)