How to debug ASP.NET Core in Kubernetes from Visual Studio 2019?

Pavel Agarkov
ITNEXT
Published in
3 min readFeb 7, 2021

--

Since the original post got outdated here’s the new one for VS2019

Prepare the workstation

For this tutorial we will need:

  • kubectl - installed and configured to access the cluster
  • Visual Studio 2019

This is assuming you already have a kubernetes cluster, a docker image build pipeline and a way to publish dotnet app to that cluster.

Prepare docker image

In order to debug dotnet app remotely we need a debugger on the remote side as well. It is possible to install it on demand but I prefer to do it during docker image build like this:

RUN apt-get update \
&& apt-get install -y --no-install-recommends unzip \
&& rm -rf /var/lib/apt/lists/* \
&& curl -sSL https://aka.ms/getvsdbgsh | \
bash /dev/stdin -v latest -l /vsdbg

You can read how to do it conditionally only for dev environment in my previous article.

Also we need to build our app in Debug configuration. For example like this:

RUN dotnet publish -c Debug

Prepare Visual Studio

To make it more convenient for monorepos and teams I made a Visual Studio extension that:

  • copies launch.json into bin directory
  • replaces environment variables in launch.json
  • replaces relative paths with absolute ones in launch.json
  • starts remote debugging

We will need to install this extension and restart Visual Studio in order to continue.

Prepare the project

Create a new file kube-debug.sh with the following content and place it in the root solution folder so we can reuse it from all our projects:

Next create launch.json file with the fallowing content inside Properties folder in the project that will be debugged later:

This file can also be placed right in the project folder or even in the root solution folder but I like it to be in the same place as default launchSettings.json — since they are quite similar in nature. Hope they will be merged in the future versions…

%SolutionRootForBash% here will be replaced with the root solution folder path but since we are using bash it also replaces \ with /. But if you prefer powershell then you will need %SolutionRoot%. You can find similar script but in powershell in my previous article. Check out the source code to find more replacements.

app.kubernetes.io/name=my-app-name and my-app-namespace should be replaced here with the real values for the current app. For the--selector we can use match labels from the deployment specification.

⚠ We are ready to start debugging but first don’t forget to increase timeoutSeconds for livenessProbe or disable it completely. Otherwise kubernetes will restart the app whenever we pause on a breakpoint.

Start debugging

To start debugging just choose Remote Debug in the project context menu:

Here we are debugging from Windows an app that is running inside a kubernetes cluster:

⚠ CAUTION ⚠

Pressing 🟥 Stop Debugging (Shift+F5) will shut down the app running in the cluster! k8s will restart it, but still…

To actually stop just debugging we need to use ❌ Detach All instead!

--

--