# How to debug and run code locally with Bridge to Kubernetes for VS Code

# Troubleshoot complex container solutions

You can use containers to run complex solutions that consist of multiple services, like an HTTP server, a database and a cache. To manage these containers, you can use Kubernetes as an orchestrator. In Azure, you would use Azure Kubernetes Service (opens new window) for this. Troubleshooting applications in such a complex solution seems difficult, but Bridge to Kubernetes (opens new window) can help. Bridge to Kubernetes allows you to run and debug code on your development computer for an application that runs in your Kubernetes cluster.

In this post, we'll use Bridge to Kubernetes (opens new window) in VS Code (opens new window) to debug a sample application that runs in Azure Kubernetes Service (opens new window).

# Prerequisites

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

# Use Bridge to Kubernetes

This tutorial assumes that you have an Azure Kubernetes Service that is running the sample voting application (opens new window). This application has a frontend and backend, and you can find its source code here (opens new window).

We'll start by installing the Bridge to Kubernetes extension (opens new window) in VS Code.

  1. Open VS Code and open the folder with the source code of the sample application (opens new window)
  2. Click on the Extensions menu
  3. Search for "Bridge to Kubernetes". Select the result and click Install

(Bridge to Kubernetes extension for VS Code)

  1. You might see messages in VS Code that tell you to install dependencies like the YAML extension and Kubectl. Install the dependencies that it asks for
  2. Assuming that you didn't connect your local development environment to AKS yet, VS Code will prompt you to "Set the current cluster and name". Click on the message, which will take you to the Kubernetes menu
    1. In the Clouds section of the Kubernetes menu, you'll see Azure. When you sign into your Azure account, you will see your AKS cluster. Right-click on it and select "Merge into Kubeconfig". Your local environment can now connect to the AKS cluster
  3. In VS Code, click the View menu and Command Palette
  4. In the Command Palette, type Bridge to Kubernetes and select "Bridge to Kubernetes: Configure" to set it up

(Configure Bridge to Kubernetes in VS Code)

  1. Next, choose the azure-vote-front service to redirect to your machine. This is the frontend of the sample application

(Redirect traffic from the frontend to your local machine)

  1. Enter a local port to redirect the traffic to, like 80
  2. And next, click "+ Create a new launch configuration"
    1. Select Python
    2. And select "Python file Debug the current active Python file". This creates a launch.json file, which you need for debugging in VS Code
    3. Finally, pick No for the question "Isolate your local version of azure-vote-front from other developers?". This will redirect all the traffic for the frontend to your local machine which is okay, as you are the only developer working on it right now

That's it! Let's start debugging the app.

  1. In the sample app in VS Code, open the azure-vote folder and open the main.py file
  2. Now click on the Run and Debug menu on the left

(Debug in VS Code)

  1. Click on the Run and Debug configuration "Python: Current File with Kubernetes". This will start debugging. As you are using Bridge to Kubernetes, all traffic that flows to the frontend will be redirected to your local machine. This will open a browser with the frontend running in it

(The sample app, redirected to the local machine)

  1. In main.py, set a breakpoint at line 77 "vote = request.form['vote']"
  2. In the browser, click one of the voting buttons. This should trigger the breakpoint and allow you to debug the application, which is still running in the AKS cluster

(Debugging the app locally)

# Conclusion

Bridge to Kubernetes (opens new window) makes it easy to debug code that runs in a Kubernetes cluster. It is easy to use with the VS Code extension (opens new window). You can use it alone or with a team of developers (opens new window) and configure it (opens new window) when needed. Go and check it out!