🦄 Checkout my Twitch Azure Developer Streaming (opens new window)!
📺 Watch the video : Get started with .NET Core on Kubernetes with Azure Dev Spaces (opens new window).
💡 Learn more : Azure Dev Spaces (opens new window).
# Part 1 - Get started with .NET Core on Kubernetes with Azure Dev Spaces
# Using Azure Dev Spaces to develop for Azure Kubernetes Service
This is part 1 of a 3-part series about Azure Dev Spaces (opens new window):
- Part 1 Get started with .NET Core on Kubernetes with Azure Dev Spaces (this post) (opens new window)
- Part 2 Develop multi-service applications on Kubernetes with Azure Dev Spaces (opens new window)
- Part 3 Use Azure Dev Spaces to collaborate with a team on Kubernetes (opens new window)
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 (opens new window) makes developing container-based solutions for Kubernetes (opens new window) 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.
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account (opens new window) before you begin)
- The latest version of the Azure CLI (opens new window) on your local machine
- The .NET Core SDK (opens new window)
- The latest version of Visual Studio Code (opens new window)
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:
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
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>
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 (opens new window), like westeurope.
az group create --name TipsAndTricksAKS --location <region>
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
And finally, enable the Kubernetes cluster for Azure Dev Spaces with this command:
az aks use-dev-spaces -g TipsAndTricksAKS -n MyAKS
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:
Go to the Azure Dev Spaces sample application (opens new window) and clone or download it to your local machine
On your local machine, navigate to the samples/dotnetcore/getting-started/webfrontend directory
Open the webfrontend folder with VS Code. This will open a standard ASP.NET Core web application
In VS Code, open the terminal by clicking on the View > Terminal menu-item
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/webfrontend1
Now run the following command to prepare the application to run in a container on Kubernetes:
azds prep --public1
This adds a Docker file to the application, that enables it to run in a container. And a Helm chart (opens new window) 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.
In VS Code, in the terminal, make sure that you are still in the webfrontend directory
Run the following command to run the code:
The first time that you run this command, it will take several minutes, as it is creating and uploading a container to Azure.
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
Use one of the application URLs to open the application in a browser
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.
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
Click on the Debug icon on the left sidebar of VS Code
Select .NET Core Launch (AZDS), like in the image below:
(The debug configuration in VS Code)
Hit F5 to start debugging
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.
Make a change to code in the HomeController.cs file
Now restart the debugging process by clicking the Restart button in the Debug actions pane
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.
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 (opens new window) helps with that. It works with Kubernetes (opens new window) 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!