When working on projects, you typically need to invest a lot of initial effort to set up an automated build and release process by configuring the CI/CD pipelines.
In this blog, we will learn how to create a Build pipeline and push the docker image to Azure Container Registry (ACR) by using Azure DevOps and how to create a Release pipeline to pull the image from ACR and deploy in Kubernetes. Docker majorly helps us to package an application and its dependencies in isolated environments called containers.
What is a Docker File?
- A Docker file is a text document that contains all the commands a user could call on the command line to assemble an image.
What is an Image?
- A Docker image is a template that contains a set of instructions for creating a container. It provides a convenient way to package up applications and preconfigured server environments.
What is a container?
- A docker container is a running instance of the image.
Docker runs instructions in aDockerfilein order. ADockerfilemust begin with aFROMinstruction.
Sample Docker file for a node js application:
#Base image
FROM node:11
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 3000
CMD [ “npm”, “start” ]
- The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
- The WORKDIR instruction sets the working directory for any RUN, CMD and COPY instructions.
- The RUN instruction will execute any commands on top of the current image and commit the results.
- The COPY instruction copies new files or directories from source and adds them to the filesystem of the container at the path destination
- The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime
- The main purpose of a CMD is to provide defaults for an executing container. There can only be one CMD instruction in a Dockerfile
Introduction to Azure Kubernetes Service (AKS)
Azure Kubernetes Service is a container orchestration platform adopted by Microsoft Azure based upon the open-source Kubernetes system.
What is a Master Node?
- A master node is a node which controls and manages a set of worker nodes
What is a Worker Node?
- A Kubernetes cluster consists of a set of worker machines – called nodes, that run containerized applications. Every cluster has at least one worker node.
What is a Pod?
- A set of Running containers.
Using Azure DevOps for Build and Deployment
Build Pipeline
- The Build pipeline will build the application and create the image by using the Dockerfile.
- The image will then be pushed to the ACR (Azure Container Registry).
Steps to create a Build pipeline
- Create a new pipeline.
- Select the Repository and branch of your application.
- Click on + symbol next to Agent job and search for Docker and click on Add. Please find below for reference.
- Now select the task and provide the necessary details like
– Service connection to the container registry
– Container repository name
– Select the command ‘Build and Push’
– Leave all other defaults as is
- Click on ‘Save & queue’ option to save and run the pipeline. PFB for reference.
Release Pipeline
The Release pipeline will pull the image from the ACR and then will create a POD in Azure Kubernetes Service.
- Create a new release pipeline
- Select the Azure repo as the artifact – the Kubernetes manifest file will be present in the repo.
- Click on + symbol next to Agent job and search for kubectl and click on Add. PFB for reference.
- Select the task added in the previous step and provide the following inputs.
- Select Service Connection type as ‘Kubernetes Service Connection’
- Select the service connection
- Provide the Namespace where you want to deploy – If the namespace is not provided, the commands will run in the default namespace.
- Select the command ‘apply’
- Select the checkbox next to Use configuration
- Select the configuration type as ‘File path’ and provide the path of the manifest file
Below is the reference
Save the changes and create a release for deployment
The following picture will help to understand the flow in Azure DevOps
Conclusion
Once the build process has completed its execution, the release pipeline starts execution and deploys the application to Azure Kubernetes Service.Through this blog, the user should understand the Basics of Docker and Kubernetes and can effortlessly create the Azure DevOps build and release pipelines by building the image and pushing it to the ACR and automation of deploying the image from Azure Container Registry to Azure Kubernetes Service.