How to Setup CI/CD Pipeline Using Azure DevOps for AKS
Setting up CI/CD pipeline for development team is crucial for fast development and fast deployment. Without any automation, servers and system components should get set up manually which isn't efficient and takes time.
For that reason, creating a pipeline that has capabilities to manage the whole deployment with a single touch is required. The pipeline should be able to rollback to previous versions easily to prevent unavailability. Furthermore, the pipeline should also be able to build, test, and deploy the app in an automated manner.
In this Article, you will find answers to:
- What is Azure DevOps?
- What is Azure Repos?
- How to set up local agent to run Tasks.
- How to set up service connections in Azure DevOps?
- How to set up connection between AKS and ACR?
- Basic commands to manipulate AKS
- How to create a build pipeline in Azure DevOps?
What is Azure DevOps?
Azure DevOps supports a collaborative culture and set of processes that bring together developers, project managers, and contributors to develop software. It allows organizations to create and improve products at a faster pace than they can with traditional software development approaches.
What is Azure Repos?
Azure Repos is a set of version control tools that you can use to manage your code. Whether your software project is large or small, using version control as soon as possible is a good idea. Version control systems are software that helps you track changes you make in your code over time.
You can use git commands to manipulate azure repos which is great. In short terms, Azure repos is kind of enhanced version of Github and git actions. You have the whole capability of managing your teams, projects, testing, DevOps pipelines and your artifacts.
Requirements
Before diving into deep of CI/CD creation, make sure you have the necessary resources to start. First of all, you need to have an Azure subscription. If you don't have an Azure subscription yet, no need to worry create one now and get 200$ free credits.
If you already have an Azure subscription, simply create AKS and ACR services with the following commands: (Dont forget to change the variables according to yourself)
This will create Azure Kubernetes service and Azure container registry. By attaching the ACR to AKS, you enable AKS to have necessary AcrPull and permissions to fetch images from the registry. Luckily, attaching directly in the command-line gives us the relief of not configuring RBAC explicitly.
Since I assume you have already set up the Azure DevOps organization, please refer to creating Azure Devops organization for project creation in case you dont have yet.
Also, I will be using Azure Repos, however I will not show how to set up one. This is trivial task, you can find out on the internet easily. Resource
How to Set Up Local Agent to Run Tasks?
Let me explain, If you use free tier Azure or Azure for Students, so you need to set up a local agent which essentially uses your local machine to run tasks that you declared in your azure pipeline.
Also, If you don't want to use your computer, then you can spin up a virtual machine in Azure to use it as an agent. To set up, in Azure DevOps go to > /organization settings/agent pools/ and create an agent pool. Click on it, and then click new agent which will show you the commands to run on the agent machine to set up connection.
The image looks like this
After setting up the Agent machine, go ahead and click to the agent and check if it's online or not. If you get stuck here, check out other resources.
Here how it should look like.
How to Set Up Service Connections?
Since Azure Devops and Azure Cloud are different environments, we will need to set up neccessary connections to enable pipelines to connect respective resources. You can use the created Service connections in azure pipelines to connect with azure resources.
There are a lot of connection types, for the sake of this article we need only Kubernetes and Docker registry connection services. You may find the docker registry name related to Docker Hub, please don't think that :). Because, you can either connect to ACR or Docker registry with this connection type.
Go to /project settings/service connections directory which will look like this
Add two new connection types as mentioned: Docker registry and Kubernetes. To add them simply click to new service connection and type their name. It will show your resources within the your subscription' scope. (comment if you get stuck)
Creating Pipelines
I have already set up my Azure repo, and the repository has the necessary manifests and azure-pipeline.yml.
Please, configure the YAML files for your use cases. For example, you may want to change the agent name mypool-agent001 to your-agent-name.
azure-pipeline.yml
user-service-deployment.yaml
We've set up the necessary services and configured Azure DevOps to manipulate these services with the provided YAML files. It's time to run.
1- It will connect to the Agent you've set up.
2 - Then build the image on the Agent machine.
3 - Deploy the image in the ACR to AKS.
The image successfully fetched from the ACR and run in the AKS, since my image has a small developer issue :). It CrashLoopBackOff, but its okay we got what we want I guess :9.
Summary
We created Azure DevOps project and repo, then created service connections to enable azure and azure devops to communicate, after that we created the CI/CD pipeline and manifests YAML files.
This basically enables us to deploy automatically as the developers pushes new features. However, this pipeline requires significant tasks like testing case, staging case, and logging of the pipeline results.
Overall, the article covered the necessary details to get started for Azure DevOps CI/CD pipeline and if you get stuck or have questions, feel free to comment.