Skip to content

Latest commit

 

History

History
180 lines (112 loc) · 8.87 KB

get-started-netcore-visualstudio.md

File metadata and controls

180 lines (112 loc) · 8.87 KB
title services ms.custom ms.workload ms.date ms.topic description keywords
Create a Kubernetes dev space: Visual Studio & .NET Core
azure-dev-spaces
vs-azure
azure-vs
07/09/2018
tutorial
This tutorial shows you how to use Azure Dev Spaces and Visual Studio to debug and rapidly iterate a .NET Core application on Azure Kubernetes Service
Docker, Kubernetes, Azure, AKS, Azure Kubernetes Service, containers, Helm, service mesh, service mesh routing, kubectl, k8s

Create a Kubernetes dev space: Visual Studio and .NET Core with Azure Dev Spaces

In this guide, you will learn how to:

  • Set up Azure Dev Spaces with a managed Kubernetes cluster in Azure.
  • Iteratively develop code in containers using Visual Studio.
  • Independently develop two separate services, and used Kubernetes' DNS service discovery to make a call to another service.
  • Productively develop and test your code in a team environment.

Note

If you get stuck at any time, see the Troubleshooting section.

Install the Azure CLI

Azure Dev Spaces requires minimal local machine setup. Most of your dev space's configuration gets stored in the cloud, and is shareable with other users. Start by downloading and running the Azure CLI.

Sign in to Azure CLI

Sign in to Azure. Type the following command in a terminal window:

az login

Note

If you don't have an Azure subscription, you can create a free account.

If you have multiple Azure subscriptions...

You can view your subscriptions by running:

az account list --output table

Locate the subscription which has True for IsDefault. If this isn't the subscription you want to use, you can change the default subscription:

az account set --subscription <subscription ID>

Create a Kubernetes cluster enabled for Azure Dev Spaces

At the command prompt, create the resource group in a region that supports Azure Dev Spaces.

az group create --name MyResourceGroup --location <region>

Create a Kubernetes cluster with the following command:

az aks create -g MyResourceGroup -n MyAKS --location <region> --generate-ssh-keys

It takes a few minutes to create the cluster.

Configure your AKS cluster to use Azure Dev Spaces

Enter the following Azure CLI command, using the resource group that contains your AKS cluster, and your AKS cluster name. The command configures your cluster with support for Azure Dev Spaces.

az aks use-dev-spaces -g MyResourceGroup -n MyAKS

Important

The Azure Dev Spaces configuration process will remove the azds namespace in the cluster, if it exists.

Get the Visual Studio tools

Install the latest version of Visual Studio 2019 on Windows with the Azure Development workload.

Create a web app running in a container

In this section, you'll create an ASP.NET Core web app and get it running in a container in Kubernetes.

Create an ASP.NET web app

From within Visual Studio, create a new project. Currently, the project must be an ASP.NET Core Web Application. Name the project 'webfrontend'.

Select the Web Application (Model-View-Controller) template and be sure you're targeting .NET Core and ASP.NET Core 2.0 in the two dropdowns at the top of the dialog. Click OK to create the project.

Enable Dev Spaces for an AKS cluster

With the project you just created, select Azure Dev Spaces from the launch settings dropdown, as shown below.

In the dialog that is displayed next, make sure you are signed in with the appropriate account, and then either select an existing Kubernetes cluster.

Leave the Space dropdown defaulted to default for now. Later, you'll learn more about this option. Check the Publicly Accessible checkbox so the web app will be accessible via a public endpoint. This setting isn't required, but it will be helpful to demonstrate some concepts later in this walkthrough. But don’t worry, in either case you will be able to debug your website using Visual Studio.

Click OK to select or create the cluster.

If you choose a cluster that hasn't been enabled to work with Azure Dev Spaces, you'll see a message asking if you want to configure it.

Choose OK.

Important

The Azure Dev Spaces configuration process will remove the azds namespace in the cluster, if it exists.

A background task will be started to accomplish this. It will take a number of minutes to complete. To see if it's still being created, hover your pointer over the Background tasks icon in the bottom left corner of the status bar, as shown in the following image.

Note

Until the dev space is successfully created you cannot debug your application.

Look at the files added to project

While you wait for the dev space to be created, look at the files that have been added to your project when you chose to use a dev space.

First, you can see a folder named charts has been added and within this folder a Helm chart for your application has been scaffolded. These files are used to deploy your application into the dev space.

You will see a file named Dockerfile has been added. This file has information needed to package your application in the standard Docker format.

Lastly, you will see a file named azds.yaml, which contains development-time configuration that is needed by the dev space.

Debug a container in Kubernetes

Once the dev space is successfully created, you can debug the application. Set a breakpoint in the code, for example on line 20 in the file HomeController.cs where the Message variable is set. Click F5 to start debugging.

Visual Studio will communicate with the dev space to build and deploy the application and then open a browser with the web app running. It might seem like the container is running locally, but actually it's running in the dev space in Azure. The reason for the localhost address is because Azure Dev Spaces creates a temporary SSH tunnel to the container running in AKS.

Click on the About link at the top of the page to trigger the breakpoint. You have full access to debug information just like you would if the code was executing locally, such as the call stack, local variables, exception information, and so on.

Iteratively develop code

Azure Dev Spaces isn't just about getting code running in Kubernetes - it's about enabling you to quickly and iteratively see your code changes take effect in a Kubernetes environment in the cloud.

Update a content file

  1. Locate the file ./Views/Home/Index.cshtml and make an edit to the HTML. For example, change line 73 that reads <h2>Application uses</h2> to something like:

    <h2>Hello k8s in Azure!</h2>`
  2. Save the file.

  3. Go to your browser and refresh the page. You should see the web page display the updated HTML.

What happened? Edits to content files, like HTML and CSS, don't require recompilation in a .NET Core web app, so an active F5 session automatically syncs any modified content files into the running container in AKS, so you can see your content edits right away.

Update a code file

Updating code files requires a little more work, because a .NET Core app needs to rebuild and produce updated application binaries.

  1. Stop the debugger in Visual Studio.
  2. Open the code file named Controllers/HomeController.cs, and edit the message that the About page will display: ViewData["Message"] = "Your application description page.";
  3. Save the file.
  4. Press F5 to start debugging again.

Instead of rebuilding and redeploying a new container image each time code edits are made, which will often take considerable time, Azure Dev Spaces will incrementally recompile code within the existing container to provide a faster edit/debug loop.

Refresh the web app in the browser, and go to the About page. You should see your custom message appear in the UI.

Next steps

[!div class="nextstepaction"] Learn about multi-service development