Building a CI-CD pipeline on Azure Kubernetes Service (AKS) — Part 1
So finally I’m at a point where I can actually talk about Dev-Ops stuff, which I recently find that is actually becoming a developer everyday’s tasks.
In this blog we will write a full CI-CD pipeline with a Node.js (very) basic app that will be pushed to Kubernets service hosted in Azure (AKS) and the new Azure Dev-Ops branding (formally known as VSTS : Visual Studio team services).
In future posts I will explore some other options such as using GCP for exactly the same task , but for now, and since I have a free trial account lets start with Azure.
Part 1 : Setup services and building the app(<- you are here)
Part 2 : Creating the build Pipeline in Azure Dev Ops
Part 3 : Creating a release and Continuous Deployment
I find it very surprising to find out that Azure DevOps has become so mature and robust in terms of Open Source projects. it now serves so many platforms, registry container options, template jobs on several platforms that there is really a small chance that you will not find what you need.
I will stay during this entire demo with node.js app hosted on linux and built upon linux tools and Azure is certainly up to this task. easily.
Here are the prerequisite you need to have before taking this tutorial :
- An Azure Account , you can get it for free for 12-months :
https://azure.microsoft.com/en-us/free/ - A Git hub repository .
- Some familiarity with Docker and Kubernetes , although I will not explain the commands here , you can follow on even if its your first time with Kubernetes.
we will be building the following pipeline :
- Building the app . it is going to be a very basic “hello-world” in node.js.
- push the code to git hub. Da.
- Create an Azure Dev-Ops (A.K.A VSTS) build pipeline
- The build will result a container image that will be pushed to Azure Container Registry Service
- Create an Azure Dev-Ops Release pipeline
- The deploy job will pull the image from the ACS and deploy it according to Deployment set we will configure in our Kubernetes cluster.
8. We will test our deployment and push new changes to GitHub to reflect those changes.
In this part we will build all the services together, including the source code and make sure it runs locally.
Building the Node.js app
The node.js app is rather simple and can be found in any node.js 101 tutorial on how to write hello world with node and express.
The source code can be found here https://github.com/tshaiman/hello-node
It is worth looking at the Dockerfile:
FROM node:8
# Create app directory
WORKDIR /usr/src/app
# copy package.json AND package-lock.json
COPY package*.json ./# Install app dependencies
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
ENV PORT=8080EXPOSE ${PORT}CMD [ "npm", "start" ]
Download the Github repository and build the docker:
docker build -t tshaiman/hello-node:2.0 .
(change tshaiman/hello-node to accordingly)
next, lets run it:
$ > docker run -p 8080:8080 tshaiman/hello-node:2.0 -d$> curl localhost:8080
hello world !
that was easy. next, we will create an Azure Registry Service and an Azure Kubernetes cluster with 3 nodes.
Creating an Azure Container Registry (ACR) Service
Assuming you have an azure account, hopefully with some free credits you can create the following services on the free tier.
you will need to install azure CLI tools from here :
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
We need to log to our azure subscription:
$> az login
this will open a browser where you can enter your credentials to login. after a successful login your subscription list will be displayed:
we also need tocreate a resource group for ACR :
$> az group create --name myACRGroup --location westeurope
and the ACR itself :
$>az acr create --resource-group myACRGroup --name myCR001 --sku Basic
after the ACR is provisioned we want to get the full server name which we will be using for the build pipeline :
$>az acr list --resource-group myACRGroup --query "[].{acrLoginServer:loginServer}" --output table
This will output all the acr service names you have on that resource group.
we only have one so the output is quite clear:
Creating an Azure Kubernetes Service (AKS):
we will be using a different resource group for our AKS cluster:
$>az group create --name myAKSCluster --location westeurope
than we can provision the cluster by calling :
az aks create --resource-group myAKSCluster --name myAKSCluster --node-count 3 --enable-addons monitoring
note that we have used 3 nodes here, but you can use less in case you just want to experiment with the service.
The provisioning process will take a significant amount of time, after which you will be able to access the cluster by running normal kubectl commands on your terminal. in case you have several clusters (docker-for-windows/ minikube/open-shift / etc) make sure the current context is selected by running :$> kubectl config get-contexts
and verify your azure cluster is marked as the default one. also we need to update our kubernetes local file with this cluster credentials :
$>az aks get-credentials --resource-group myAKSCluster --name myAKSCluster
The first command updates your ~/usr/local/.kube/config file which is the local kubernetes cli config file for selecting the contexts and credentials for each cluster.
we will be using this file later on to grant access for the Azure build pipeline to access the cluster.
To verify the cluster is up and running we can run:
$> kubectl get nodes
you can also log in to the azure portal and exploring the resource groups you have just created. Azure will create an additional resource group behind the scenes for you, that is the entire infrastructure to back up your kubernetes cluster (VM’s ,disks, routing tables, Virtual networks and network interfaces,availability sets etc). I really liked how this is organised apart from your “applicative” PaaS cluster that is located under “your” resource group (a.k.a ‘myAKSCluster’ in our previous section).
That’s it . we have all infrastructure pieces of azure in place , next we will start building our pipeline using Azure Dev Ops (a.k.a previous name :‘VSTS’ ).
In Part 2 Of This Series I will cover the CI part using Azure Dev Ops.
Stay Tuned !