Updating Applications on Kubernetes
Updating applications on Kubernetes without downtime is a key benefit of using Kubernetes. To update your application without downtime, follow these steps:
- Update your application code and create a new Docker image: First, you need to update your application code and create a new Docker image. You can follow the same steps as in the previous example to create a new Docker image with the updated code. For example:
docker build -t my-app:2.0 .
- Update the deployment object with the new image version: Next, you need to update the deployment object with the new Docker image version. You can do this by modifying the YAML configuration file for the deployment object and setting the
image
field to the new image tag. 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:2.0 # <-- new image version
ports:
- containerPort: 5000
Save the updated configuration to a file, for example, deployment-v2.yaml
.
- Apply the updated configuration: Finally, you can apply the updated configuration using the
kubectl apply
command. Kubernetes will automatically create new replicas with the updated image version and gradually replace the old replicas with the new ones, ensuring that there is always at least one replica of the application running. For example:
kubectl apply -f deployment-v2.yaml
Kubernetes will perform a rolling update, starting with one replica, and gradually scaling up the new version of the application while scaling down the old version. By default, Kubernetes will update one replica at a time, waiting for the new replica to become ready before scaling down the old one. This ensures that there is always at least one replica of the application running and that there is no downtime during the update.
You can monitor the progress of the rolling update using the kubectl rollout status
command:
kubectl rollout status deployment/my-app
This command will show the status of the rolling update, including the number of replicas that have been updated and the total number of replicas.
Once the rolling update is complete, all replicas of the application will be running the new version of the Docker image, and your application will be updated without downtime.
Leave a Comment