Kubernetes is a popular open-source platform for managing containerized applications. Keeping your applications up-to-date with the latest features and bug fixes is important for security and stability. In this tutorial, we will show you how to automatically roll out updates to your Kubernetes deployments.
- A Kubernetes cluster
- A deployment with a container image
- A container registry (e.g. Docker Hub, Google Container Registry)
Step 1: Create a new image version
In this step, you will create a new version of the container image with the updated code and push it to a container registry. The following are the detailed steps:
- Update the application code
Make the necessary changes to the codebase, including bug fixes, new features, or security patches. Ensure that the code changes are well tested and ready for deployment.
- Build a new image
Use a Dockerfile to build a new image with the updated code. A Dockerfile is a script that contains instructions for building an image. You will use this Dockerfile to build a new image with the updated code.
The Dockerfile should contain instructions for copying the updated code into the image, installing any dependencies, and specifying the command to run the application.
# Use an existing image as the base FROM node:14 # Set the working directory WORKDIR /app # Copy the updated code into the image COPY . . # Install the dependencies RUN npm install # Specify the command to run the application CMD ["npm", "start"]
- Build the new image using the Dockerfile
docker build command to build a new image from the updated code and the Dockerfile.
docker build -t my-image:v2 .
This command will build a new image named
my-image with the tag
. at the end of the command specifies the location of the Dockerfile.
- Push the image to a container registry
A container registry is a place where you can store and distribute your container images. Examples of container registries include Docker Hub, Google Container Registry, and Amazon Elastic Container Registry.
docker push command to push the new image to a container registry.
docker push my-image:v2
This command will push the image to the default container registry specified in your Docker configuration.
Now that you have created a new version of the container image, you are ready to update your deployment with the new image.
Step 2: Update the deployment definition
In this step, you will update the deployment definition file to use the new image version. The deployment definition file is a YAML file that specifies the desired state of the deployment. It includes information such as the number of replicas, the container image to use, and the resources required by the deployment.
Here are the detailed steps to update the deployment definition:
- Open the deployment definition file
Use a text editor to open the deployment definition file for the deployment that you want to update. The deployment definition file should be in YAML format.
- Update the image name
Find the section of the deployment definition file that specifies the container image. This section should look something like this:
spec: template: spec: containers: - name: my-container image: my-image:v1
Update the image name to use the new image version that you created in Step 1.
spec: template: spec: containers: - name: my-container image: my-image:v2
- Save the file
Save the changes to the deployment definition file.
Now that you have updated the deployment definition file with the new image version, you are ready to apply the changes to your Kubernetes cluster.
Step 3: Apply the deployment definition to your cluster
- Run the command:
kubectl apply -f deployment.yaml
- Verify that the deployment has been updated by running the command:
kubectl get deployments
Step 4: Monitor the rollout
- Run the command:
kubectl rollout status deployment/deployment-name
- Wait until the rollout is complete
- Verify that the updated image is running by running the command:
kubectl get pods
Step 5: Verify the update
- Test your application to make sure the update is functioning as expected
Congratulations! You have successfully rolled out an update to your Kubernetes deployment. By automating this process, you can ensure that your applications are always running the latest version and reduce the risk of downtime or security vulnerabilities.