As most teams move towards Github Flow or Trunk based development, where the need is to push to production directly from the feature branch before it can be merged into master, it becomes critical that teams have a feature environment for every PR being raised so that peer reviewers can easily review the changes in the PR and also get signoffs from the necessary stakeholders.
'Review Apps' is the term the industry/community is zeroing in on for this feature.
Setting up Review Apps on a Kubernetes cluster on Azure is quite straight forward, lets see how we go about doing it.
Setting up a Kubernetes Cluster on Azure.
While you can set up a K8s cluster via the Web Portal at https:portal.azure.com, its a lot more easier if you have Azure CLI.
Install Azure CLI following the steps here https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest.
Once you have Azure CLI setup and have logged in, then its time to create your K8s cluster.
We first start by creating a resource group within a certain region. Its within this resource group that your k8s cluster, the container registry and other services needed to manage your cluster will be setup.
Currently Review Apps is in preview stage and are available in only certain regions. So while setting up the resource group ensure you select useast
or uksouth
while creating your region.
# Create a resource group
az group create --name devfest-rg --location uksouth
Next we need to create a container registry within the resource group
# Create a container registry
az acr create --resource-group devfest-rg --name devfestContainerRegistry --sku Basic
And finally we create our K8s cluster, this command will take a while to complete so be a little patient. (go get some coffee or check twitter )
# Create a Kubernetes cluster
az aks create \
--resource-group devfest-rg \
--name devfest \
--node-count 1 \
--enable-addons monitoring \
--generate-ssh-keys
To check to see if the cluster is setup correctly you can run the kubectl
command, but first you will need to associate kubectl with the cluster, you do that with the following command
az aks get-credentials --name devfest --resource-group devfest-rg
Now you can run any of the following commands, and if you get a valid response then your cluster is setup correctly.
Kubectl get nodes
// or
Kubectl get services
Setting up AzureDevOps
Now that we have our cluster running its time to setup our devops Pipeline.
Before you start make sure you have an app with a Dockerfile. Feel free to fork this app to follow along this tutorial https://github.com/areai51/devfest
Login to https://dev.azure.com.
Go to Pipelines > Build > New Pipeline and follow the wizard.
Select th repo where your code resides.
Next Azure scans your repo and gives you a list of recommended templates you can choose from to setup your pipeline.
Select Deploy to Kubernetes Cluster
From the dropdown select the K8s cluster, and the container registry that you created at the start of the tutorial.
For the namespace select default or give a new name eg: dev.
The important step is to make sure you select the Enable Review App flow for Pull Requests checkbox.
Click on Validate and configure.
This will create an azure-pipelines.yml
file with all the necessary configuration to setup review apps. It would also create a deployment.yml
and service.yml
file within aa manifest folder. These are needed to deploy our app to the k8s cluster.
Azure Devops will then commit these files to your repo on your behalf.
Devops will then run the build job and once the jobs run successfully you should see the green ticks for the build and deploy stages as below.
Feel free to click on any of the stages to see the details of the various tasks it ran.
To view your app in the browser you simply need to type in the IP address of the load balancer along with the correct port.
To find out the IP address of your load balancer run the following kubectl command.
kubectl get services
This will return a list of services currently running. Look for the one that says load balancer and copy the external IP address and the port number that you can have a look at in the browser. This would be the version from the master branch of your repo.
Previewing a PR
Now create a branch off master, make some changes to the server.ts file, and push the code to the remote origin.
Raise a Pull Request on Github, and go into the Azure Devops pipeline, and you will see that the pipeline has been automatically triggered.
Wait for it to complete the build and deploy tasks.
Once they run successfully you would receive an email stating the pipeline ran successfully and it would also contain the link to the URL where you can preview the changes in the PR.
Alternately, in your github pull request, you should also see a message posted by azure-pipelines
like this, which has the link to the review app handy for anyone who wants to review the PR.
Thus you can see with Azure DevOps it is quite straight forward to setup Review Apps on a Kubernetes cluster in Azure.
Top comments (0)