Deploying your first Kubernetes app with Azure DevOps
Jun 08, 2023 • 8 Minute Read
Hi, I'm Chad Crowell, Microsoft Azure course instructor at A Cloud Guru. I'm truly passionate about helping you accomplish your dreams and helping you utilize your skills to push past adversity. Together, we can accomplish anything.
In the world of tech, we love creating new buzz words! Some just roll off your tongue, and some of them don't. Some of them even find themselves in our job titles, even though they probably shouldn't. One of those super cool buzz-words is DevOps. DevOps is one of those words that sounds cool (because Martin Fowler said so), and also seems to persist even in our conversations today. So, I thought it would be fun to explore what's new in Azure DevOps specifically and walk you through a simple pipeline.
Ready to dive into Azure content with A Cloud Guru?
What Is DevOps?
To put it simply, DevOps is the merging of two processes that were previously separated due to the constraints of physical systems. Those two processes are development (the process of building a computer program/application) and operations (the process of creating the infrastructure/machines that run said computer program). Whether you are a developer or not, working alone or in a team, Azure DevOps training can help you organize the way you plan, create and deliver software.
What was the constraint? The physical machines had to be put together by a group of engineers in a fairly intelligent way. For example, build the computer, add it to the network, implement identity management, enforce security, etc. Fast forward to today, this is no longer the case. Everything has changed.
The Cloud Changed Everything
The virtualization of many hardware components and the prevalence of the cloud (not to mention the low barrier to entry) has many ops folks changing their job functions. Where they were handling hardware previously, they are now building infrastructure as code (IaC), amongst other coding related tasks.
Now that we are up to date on the shift of an engineer's job function over the years, let's talk about Microsoft's 800-pound gorilla (a.k.a. Azure). Azure delivers much more than infrastructure in the cloud. With PaaS, SaaS, and everything in-between, Azure is the one-stop-shop for elasticity and high-availability in the cloud.
Azure DevOps
Formerly Visual Studio Team Services (VSTS), Azure DevOps makes developers happy by streamlining the process from the integration of code, to the release and testing of software applications. Whereas Jenkins has been the dominant CI/CD software, Microsoft is hoping to steal some of that market, but still embracing Jenkins to ease that transition.
AKS App Integration Tutorial
In true learn by doing fashion, let us dive right in and create a project in Azure DevOps, to show how easy it can be to integrate and deploy an app into AKS using some simple command-line magic. Follow along, and at the end, you too can have a shiny new web application running in AKS, accessible from any web browser, which includes a friendly message from all of us here at A Cloud Guru.
To follow along, you must have access to an Azure and GitHub account. A GitHub account is free, so head on over to https://github.com and sign up if you haven't already!
Also, before proceeding, head on over to https://aex.dev.azure.com and create an Azure DevOps Organization if you do not already have one. Just click the "Create a new organization" button.
All of the following commands should be run in Azure Cloud Shell. Access this shell by going to https://shell.azure.com from any browser and logging into your Azure account.
PRO TIP: You can use the PowerShell screen, but type "bash" in the terminal to switch to bash commands.
Let's Begin!
First, add the Azure DevOps extension to our cloud shell session:
~$ az extension add --name azure-devops
Next, let's add context for our shell to reference our DevOps organization (mine organization name is "https://dev.azure.com/ccrowellla/," but yours will be different):
~$ az devops configure --defaults organization=https://dev.azure.com/ccrowellla/
From cloud shell, we must login again, so our account can access Azure DevOps. Follow the prompts in the terminal to proceed with the login process:
~$ az login
Next, let's create a new DevOps project:
~$ az devops project create --name project1
Now, set the default project to work with:
~$ az devops configure --defaults project=project1
Let's create a resource group, in order to logically organize the Azure resources we'll create in the proceeding steps:
~$ az group create --name aks-rg --location eastus
We can create a service principal to use for our AKS cluster. Our AKS cluster will use this service principal to access the Azure Container Registry and pull container images.
IMPORTANT: copy the output of the following command to a notepad, you will need it later:
~$ az ad sp create-for-rbac --skip-assignment
Now, let's create an AKS cluster to deploy our app into (here's where you use the output from the previous command to paste the "appId" after "--service-principal" and paste the "password" after "--client-secret):
~$ az aks create -g aks-rg -n myakscluster --node-count 1 --service-principal "e848493e-1614-4f58-b6a7-a880045e7d8f" --client-secret "47b01a18-903e-463b-ac68-641b40be80b6"
Next, let's create an Azure Container Registry (ACR). This will be the repository for our containers used in AKS. The ACR name will have to be globally unique, meaning nobody in the world can have the same name. Time to get creative:
~$ az acr create -g aks-rg -n uniqueacrnamehere --sku Basic --admin-enabled true
In order to allow AKS to pull images from ACR, we must set our Azure RBAC permissions for the service principal (fill in your unique ACR name here):
~$ ACR_ID=$(az acr show --name uniqueacrnamehere --resource-group aks23 --query "id" --output tsv)
~$ CLIENT_ID=$(az aks show -g aks-rg -n myakscluster --query "servicePrincipalProfile.clientId" --output tsv)
~$ az role assignment create --assignee $CLIENT_ID --role acrpull --scope $ACR_ID
Now, let's get to the good stuff; creating and deploying the application!
For the application code, you're going to fork the repo, clone that repo (from your GitHub account) and change into the "pipelines-sample-app" directory:
Fork this GitHub repo (open this link in a new tab and click "fork"): https://github.com/chadmcrowell/pipelines-sample-app
Once forked, clone it down to the terminal session within cloud shell with a "git clone" but change the github username to your username:
~$ git clone https://github.com/<your-github-username-goes-here>/pipelines-sample-app.git
~$ cd pipelines-sample-app
Let's now create a pipeline in Azure DevOps:
~$ az pipelines create --name "pipeline1"
Follow the prompts in your terminal to set up the pipeline:
1st prompt: Enter your GitHub username; press enter
2nd prompt: Enter your GitHub password; press enter
3rd prompt: Confirm by entering your github password again; press enter
4th prompt: Enter a service connection name (e.g. pipeline); press enter
5th prompt: Choose [3] to deploy to Azure Kubernetes Service; press enter
6th prompt: Select the k8s cluster you just created; press enter
7th prompt: Choose [2] for the "default" kubernetes namespace; press enter
8th prompt: Select the ACR you just created; press enter
9th prompt: Enter a value for image name (press enter to accept the default); press enter
10th prompt: Enter a value for the service port (press enter to accept the default); press enter
11th prompt: Enter a value for enable review app flow for pull requests (press enter without typing a value)
12th prompt: Choose [1] to continue with generated YAML; press enter
13th prompt: Choose [1] to commit directly to the master branch; press enter
CONGRATULATIONS! You've created an Azure DevOps Project! Wait about four minutes for the app to build the container, push to ACR, then deploy to AKS.
Access your AKS cluster, by getting the kubeconfig credentials:
~$ az aks get-credentials --resource-group aks-rg --name myakscluster
View the Kubernetes resources your project has created:
~$ kubectl get all
Amongst the new deployment and pods is a service. copy the service IP address (under "External IP") and paste into a new browser tab with ":8080" (e.g. 104.45.182.156:8080) tacked on to the end.
Here is your final result:
Summary
In this short article, we've created a new project in Azure DevOps. Within that project, we set up a CI/CD pipeline. That pipeline built our application inside of a container, pushed that container to a container repository, and deployed the container to AKS. Finally allowing us to view our web application running in AKS from the web via Kubernetes service. Pretty cool, huh?
IMPORTANT: Notice the trigger for our pipeline. Head back over to your forked repo and check out the file "azure-pipelines.yml". You should see the line "trigger: - master" which means every time we make a change to the master branch, a new build will kick off automatically.
So, the big takeaway here is NOT that we got the web app to run. Furthermore, as all changes to the app from here on out will automatically be deployed to our AKS cluster. This means as you start to develop the application, you can instantly see the changes "live". Awesome, right?!?
If you're like me and get excited about building this kind of automation into your deployment process, check out my course called "Build and Deploy Pipelines with Microsoft Azure" where I demonstrate lots of other examples of this very thing.
Are you ready to take your skills to the next level? Start training now.