Table Of Content
Part 4: Deploy to k8s in the cloud (Azure Kubernetes Service – AKS) and debug from VSCode
Part 6: Debugging generic Kubernetes Clusters with Draft (coming soon)
Kubernetes in the cloud
In the previous installments, I walked through how to set up a dev environment and use tools like draft and helm to jumpstart and scaffold out your app into a container app. I also showed how you can use draft in your inner dev loop to build/deploy/debug to a Kubernetes cluster. And all that works great, but it’s a little bit cumbersome right? Needing to copy the container id for debugging, although not a deal breaker, is not optimal. I want to just f5 and start debugging. And I found that better way when I started playing with k8s in the cloud.
As I started researching k8s in the cloud, I naturally gravitated towards Azure Kubernetes Service (AKS). AKS is managed Kubernetes in Azure, where Azure manages your cluster for you and you only pay for the agent nodes. Cool. Making my life simple, I’m all about that! As I started reading about AKS and how to use AKS, I ran into something ridiculously cool. Azure Dev Spaces. Using Azure Dev Spaces, I get that same scaffolding ability for a .net core or node.js app to turn it into a container app. I also get a slick inner dev loop experience including f5 debugging into AKS. Let’s dive in and see how this works.
To setup all of this, we need to have a Kubernetes cluster in AKS. We need to make sure the Azure CLI is installed with 2.0.33 or higher. We need configure our AKS cluster to use Azure Dev Spaces and we need the debug tools installed for VSCode (You can debug .net core apps in AKS with visual studio too).
Create a Kubernetes Cluster using AKS
You can create a cluster using AKS from the command line or using the portal. For this blog, I’ll walk you through the portal.
- Go to portal.azure.com and click on Create a resource
- Enter kub in the search and select Kubernetes Service
- Click Create
- Pick your subsscription, create a new Resource group, pick a cluster name, picke East US for the region, pick Kubernetes version 1.9.6 or higher, give a DNS name prefix and click Next: Networking >>
- Turn on HTTP application routing and click Review + create
- Click Create
This creates your Kubernetes cluster. It takes a while for it to finish so while Azure creates your cluster for you, make sure you have the Azure CLI version 2.0.33 or higher installed. If you don’t, you can get it from here.
After installing the Azure CLI, go ahead and login and if you have more than one subscription, set the default subscription to the one where you create your Kubernetes cluster (az account set –subscription <subscription ID>).
Configure your AKS cluster to use Azure Dev Spaces
After azure creates your AKS cluster, you will need to configure it to use Azure Dev Spaces. From the command line enter
az aks use-dev-spaces –g <ResourceGroupName> –n <AKSClusterName>
This installs and configures Azure Dev Spaces into your Kubernetes cluster in AKS as well as points your local kubectl to this AKS Kubernetes cluster. All of this takes quite some time to finish so feel free to go on to the next section
Install Debug Components for VSCode
To debug a .net core app in AKS with VSCode, you will need to download the VS Azure DevSpaces Extension and then, you will need to install the extension from the command line.
- Download the VS Azure Dev Spaces extension
- install the extension from the command line with
code –install-extension path-to-downloaded-extension/azds-0.1.1.vsix
Time to do container dev into AKS with Azure Dev Spaces
In the last blog post, I showed what the inner dev loop would be like using Helm and Draft into a Kubernetes cluster. Now, I’ll show you how your inner dev loop becomes even easier if you use Azure Kubernetes Service and Azure Dev Spaces
Create and containerize .net core mvc app
First let’s go and create a .net core mvc app
- Open a powershell console, browse to the directory for your app. From here type dotnet new mvc –name ADSApp
- Change directory to the ADSAPP directory
- Launch code from that directory
- This should open VSCode in the directory of the freshly created .net core mvc app. VSCode should detect this is a .net core app and ask to add the necessary assets. Click yes and VSCode should add a .vscode directory with a launch.json and a tasks.json file.
- From within VSCode, open up the powershell terminal
- In the powershell terminal type azds prep –public. This will generate Docker and Kubernetes assets with default settings for your project. It will create a default Dockerfile for you as well as create default helm charts. It will also add an azds.yaml file. It will also configure kubectl to use
Build and run code in AKS
After containerizing your app with azds, it’s simple to build and run your app in AKS. Just type azds up in the terminal. This will synchronize our files with our AKS cluster, install our chart, build the container image and then deploy our image into our AKS cluster.
Notice that in the output, it has listed the service ports. One is our local proxy to our Kubernetes cluster and the other is the public access point. Remember when we set up this project (azds prep –public) we added the –public flag?
Those are clickable links in VSCode so Cntrl-clicking them launches your browser to the url and displays your app.
Making non compiling changes immediately show up.
- Edit /Home/Views/Index.cshmtl, line 70, add the text Azure Dev Spaces!!!! inside the h2 and save
- Refresh your app in the browser and you will immediately see your changes in your container in AKS
For changes that require compilation (like editing a .cs file) you will need to stop azds in your terminal (cntrl-c) and do another azds up. This will rebuild your image with freshly compiled binaries (with your changes) and will then deploy the image to your AKS cluster.
Debug your code in AKS
Ok, so all of that is cool and all, but I promised f5 debugging right? Ok, first we have to configure code to do debugging into AKS with Azure Dev Space. We can modify our launch.json by hand (boo) we can open the command pallet in VSCode (cntrl+shift+p or View > Command Pallet) and type and select Azure Dev Spaces: Create configuration files for Azure Dev Spaces.
Now set a breakpoint in your code and press f5. VS Code will build a new image, deploy the image to AKS in debug mode with the debugger attached. Which means, launch the app in your browser and….
Bam! Breakpoint hit! We are now debugging our app running in a Kubernetes cluster in AKS using VSCode with just a simple f5!!!!!
Using AKS simplifies my life. No more managing my cluster, just let Azure do it for you. And AKS plus Azure Dev Space equals super nice, slick inner dev loop experience. To setup your app
- azds prep –public to containerize your app with docker and helm
- ctrl+shift+p Azure Dev Spaces: Prepare configuration files for Azure Dev Spaces to configure your launch.json file correctly
And now your inner dev loop becomes super slick
- Make changes to your app
- Set breakpoints and f5
Rinse and repeat!!! Cool huh? There are many more benefits to using Azure Dev Spaces. I will be doing a deeper dive into Azure Dev Spaces later this year.
So… some questions and answers. Does this only work with .net core? Nope, this works with .net core and node.js with more languages to come. What if I’m not using AKS, can I still use Azure Dev Spaces in another Kubernetes cluster? Nope. Azure Dev Spaces only works with AKS. Then… how can I make my inner dev loop easier and debug in a generic Kubernetes cluster? Remember Draft? There is now actually a way of using Draft to f5 debug any Kubernetes cluster. We’ll be covering that later in this blog series as well. But next installment will be about setting up a CI/CD pipeline for your app into AKS