Containers, Kubernetes and DevOps for an Old as Dirt Developer (Or DevOps Nirvana with Kubernetes), Part 4/6

Table Of Content

Part 1: Intro to containers, Kubernetes and DevOps

Part 2: Getting started (setting up my dev environment)

Part 3: My first app –  .net core, vscode, WSL, containerize and deploy to local k8s AND debug it while running in k8s??? GTFOH

Part 4: Deploy to k8s in the cloud (Azure Container Service – AKS) and debug from VSCode

Part 5: Set up CI/CD pipeline for Kubernetes using DevOps best practices (coming soon)

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.

Setup

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.

  1. Go to portal.azure.com and click on Create a resource
    image
  2. Enter kub in the search and select Kubernetes Service
    image
  3. Click Create
    image
  4. 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 >>
    image
  5. Turn on HTTP application routing and click Review + create
    image
  6. Click Create
    image

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.

  1. Download the VS Azure Dev Spaces extension
  2. 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

  1. Open a powershell console, browse to the directory for your app. From here type dotnet new mvc –name ADSApp
    image
  2. Change directory to the ADSAPP directory
    cd ADSApp
  3. Launch code from that directory
    code .
  4. 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.
  5. From within VSCode, open up the powershell terminal
  6. 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
    image

 

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.

image

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.

image

 

Making non compiling changes immediately show up.

 

  1. Edit /Home/Views/Index.cshmtl, line 70, add the text Azure Dev Spaces!!!! inside the h2 and save
    image
  2. Refresh your app in the browser and you will immediately see your changes in your container in AKS
    image

 

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.

 

 

image
This pops up a dialog asking to install missing assets, select Yes

image
And now, if you look at /.vscode/launch.json, you’ll see a configuration section for .NET Core Launch (AZDS). Go into debug view and select .NET Coure Launch (AZDS) as your debug configuration

image

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….

image

Bam! Breakpoint hit! We are now debugging our app running in a Kubernetes cluster in AKS using VSCode with just a simple f5!!!!!

Conclusion

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

  1. azds prep –public to containerize your app with docker and helm
  2. 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

  1. Make changes to your app
  2. 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

Next > Set up CI/CD pipeline for Kubernetes using DevOps best practices (coming soon)

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *