Updating Kubernetes Deployments
As your application evolves, you'll need to update the container images used in your Kubernetes Deployment. Kubernetes provides several mechanisms to facilitate this process, ensuring a smooth and controlled update of your application.
One of the most common ways to update a Deployment is to change the container image version. You can do this by modifying the Deployment manifest and applying the changes to the cluster. For example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:v2 ## Update the image version
ports:
- containerPort: 80
After applying the updated Deployment manifest, Kubernetes will gradually roll out the new image version, managing the transition between the old and new pods.
Alternatively, you can use the kubectl set image
command to update the container image for a specific Deployment:
kubectl set image deployment/my-app my-app=my-app:v2
This command will trigger a rolling update, replacing the old pods with the new ones running the updated container image.
Kubernetes Deployments support several update strategies, including:
- RollingUpdate: This is the default strategy, where Kubernetes gradually replaces old pods with new ones, ensuring that a specified number of pods are available at all times.
- Recreate: This strategy first terminates all existing pods, then creates new ones with the updated configuration. This approach results in downtime, but may be necessary for certain types of updates.
You can configure the update strategy in the Deployment manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
## ...
By understanding the different update mechanisms and strategies provided by Kubernetes Deployments, you can effectively manage the lifecycle of your containerized applications, ensuring a smooth and controlled update process.