Using Azure Dev Spaces to develop for Azure Kubernetes Service

This is part 1 of a 3-part series about Azure Dev Spaces:

Developing container-based solutions used to be challenging. You needed to create and run the complete solution locally, with all the containers that run all the services you need. This takes a lot of compute resources and makes it difficult to easily update components and debug them.

Azure Dev Spaces makes developing container-based solutions for Kubernetes a lot easier. It provides a way to run your solution in the cloud and update it easily and quickly to create a great developer experience.

In this article, we'll take a look at how you can use Azure Dev Spaces to create and run an ASP.NET Core application.

Prerequisites

If you want to follow along, you'll need the following:

We are going to use the Azure CLI to perform most of our actions. You can also do all of this through the Azure portal. Open a command prompt with the Azure CLI Tools installed and log into your Azure account to get started:

Create a Kubernetes cluster enabled for Azure Dev Spaces

Let's create a Kubernetes cluster in Azure and enable Azure Dev Spaces in it.

Open a command prompt and log into your Azure account to get started:

az login
1

If you have multiple Azure subscriptions, you can select one by first finding the ID of the subscription that you want with this command:

az account list
1

And if the subscription which has the isDefault:true is not the one you want to use, set the subscription using this command:

az account set --subscription <subscription ID>
1

Now that we are logged in, we'll create a new resource group that will contain the Kubernetes cluster. In the <region> placeholder, fill in a region that supports Azure Dev Spaces, like westeurope.

az group create --name TipsAndTricksAKS --location <region>
1

Next, create the Kubernetes cluster in Azure by running this command:

az aks create -g TipsAndTricksAKS -n MyAKS --location <region> --disable-rbac --generate-ssh-keys
1

And finally, enable the Kubernetes cluster for Azure Dev Spaces with this command:

az aks use-dev-spaces -g TipsAndTricksAKS -n MyAKS
1

This will also prompt you to install the Azure Dev Spaces CLI on your local machine, like in the image below:

(Install the Azure Dev Spaces CLI)

Run and debug an application on Kubernetes using Azure Dev Spaces

Now that we have a Kubernetes cluster with Azure Dev Spaces, we can deploy and run an application in it.

To make it easy on ourselves, we will use an already existing application:

  1. Go to the Azure Dev Spaces sample application and clone or download it to your local machine

  2. On your local machine, navigate to the samples/dotnetcore/getting-started/webfrontend directory

  3. Open the webfrontend folder with VS Code. This will open a standard ASP.NET Core web application

  4. In VS Code, open the terminal by clicking on the View > Terminal menu-item

  5. In the terminal, make sure that the prompt is in the webfrontend directory. If it isn't, navigate to the webfrontend directory by typing

    cd [location of the code on your computer]samples/dotnetcore/getting-started/webfrontend
    
    1
  6. Now run the following command to prepare the application to run in a container on Kubernetes:

    azds prep --public
    
    1

    This adds a Docker file to the application, that enables it to run in a container. And a Helm chart that it can use to run in Kubernetes. And also an azds.yaml file, which configures the application to work with Azure Dev Spaces:

    (Docker, Kubernetes and Azure Dev Spaces files in the project)

Run and update the application

Now for some container magic! Let's run the application and see how it works with Azure Dev Spaces.

  1. In VS Code, in the terminal, make sure that you are still in the webfrontend directory

  2. Run the following command to run the code:

    azds up
    
    1

    The first time that you run this command, it will take several minutes, as it is creating and uploading a container to Azure.

  3. When the application is running, you will see the URL to the application in the output of the terminal, like in the image below:

    (Running the app produces output in the VS Code terminal)

    Notice that there are two URLs to the application. One that runs in Azure and one on localhost. Azure Dev Spaces routes the application to localhost with the Kubernetes port-forward functionality (so it is still running in Azure), so that you can interact with the application from your local machine. The terminal also shows the logs of any interaction with the application. When you use the application, you'll see the requests to the app show up

  4. Use one of the application URLs to open the application in a browser

  5. Now go to VS Code and change some HTML in one of the views of the application. When you save the changes, Azure Dev Spaces instantly sends the changes to Kubernetes and the application is updated. This work really fast and facilitates a very fast developer experience. This work so fast, because ASP.NET Core doesn't require a compilation for changed files. However, if you change some code, like code in a controller of the application, it is still fast. When you change code, you do need to run the azds up command again to recompile the application

    (The application in a browser, also reachable from localhost)

Debug the application Using Azure Dev Spaces, we can also debug the application easily. To do that, the code in VS Code needs to communicate with the Azure Dev Space.

  1. In VS Code, go to View > Command Palette and type Azure Dev Spaces: Prepare configuration files for Azure Dev Spaces. This adds debug configuration for Azure Dev Spaces to the project

  2. Click on the Debug icon on the left sidebar of VS Code

  3. Select .NET Core Launch (AZDS), like in the image below:

    (The debug configuration in VS Code)

  4. Hit F5 to start debugging

  5. You can now put breakpoints in your code and diagnose the application

The beauty of working with Azure Dev Spaces is that you can quickly make changes and continue working.

  1. Make a change to code in the HomeController.cs file

  2. Now restart the debugging process by clicking the Restart button in the Debug actions pane

    (Restart debugging)

    The change will be uploaded and you can continue to debug with the new code. This is very fast, because Azure Dev Spaces only incrementally recompiles code within the existing container to provide a faster edit/debug loop.

Conclusion

Running containers offers a lot of freedom but can also slow down your develop-deploy-debug cycle as you need to compile containers and deploy and run them everytime you change your code. Azure Dev Spaces helps with that. It works with Kubernetes to provide a fast and seamless developer experience. When you use Azure Dev Spaces, your container-based solution runs in the cloud, but you can easily develop your code locally and run and debug it, without running a local Docker engine. Go and check it out!