Tutorial: Run and debug locally with Bridge to Kubernetes on Visual Studio

In this tutorial, you'll learn how to redirect traffic between your Kubernetes cluster and your development computer. This tutorial uses Bridge to Kubernetes and Visual Studio for debugging a service. To use Visual Studio Code, see Run and debug locally with Bridge to Kubernetes with VS Code.

To learn more about Bridge to Kubernetes, see How Bridge to Kubernetes works.

In this tutorial, you learn how to:

  • Connect to your cluster with Bridge to Kubernetes.
  • Route requests to a locally running service for development purposes.
  • Debug a running service on your local machine.

Prerequisites

Set up a service

This tutorial uses Bridge to Kubernetes to work with a simple todo sample application on any Kubernetes cluster.

The sample application has a frontend to interact and a backend that provides persistent storage.

  1. Open a Bash window and check that your cluster is available and ready. Then set the context to that cluster.

    kubectl cluster-info
    kubectl config use-context <kubernetes-cluster>
    
  2. Clone the sample repo.

    git clone https://github.com/hsubramanianaks/b2k-samples
    
  3. Change directory to todo-app and then create a namespace for the sample.

    kubectl create namespace todo-app
    
  4. Apply the deployment manifest:

    kubectl apply -n todo-app -f deployment.yaml
    

    This simple deployment exposes the frontend using a service of type LoadBalancer. Wait for all the pods to be running and for the external IP of the frontend service to become available.

    If you're testing with MiniKube, use minikube tunnel to resolve an external IP. If you're using AKS or another cloud-based Kubernetes provider, an external IP is assigned automatically.

  5. Use the following command to monitor the frontend service to wait until it's up and running:

    kubectl get service -n todo-app frontend --watch
    
    NAME       TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
    frontend   LoadBalancer   10.0.245.78   10.73.226.228   80:31910/TCP   6m26s
    

Connect to your cluster

  1. Open Visual Studio. In the Get started window, select Continue without code.

  2. Select Open > Project/Solution, then find the todo-app\database-api\databaseApi.csproj project and select Open.

  3. In the project, select Bridge to Kubernetes from the launch settings as shown here:

    Screenshot shows debugging tools with Bridge to Kubernetes selected.

  4. Select the start button next to Bridge to Kubernetes. In the Create profile for Bridge to Kubernetes dialog box, enter the following values:

    • Select your cluster name.
    • Select todo-app for your namespace.
    • Select database-api for the Service to redirect.
    • Select the same URL you used previously to launch your browser.

    Screenshot shows the Create profile for Bridge to Kubernetes dialog box with the values entered.

  5. If you want to run isolated, select Enable routing isolation. If you enable routing isolation, others who are using the cluster aren't affected by your changes. Isolation mode routes your requests to your copy of each affected service. It routes other traffic normally. For more information, see How Bridge to Kubernetes Works.

  6. Select Save and debug to save your changes.

    Screenshot shows the todo service displayed from your debugging, with an entry box for tasks.

    Note

    EndpointManager prompts you to allow elevated privileges to your hosts file.

    Your development computer connects to the cluster. The status bar shows that you're connected to the database-api service.

    Screenshot shows the status bar that verifies that your development computer is connected.

  7. Try entering tasks and marking them as complete.

  8. Select Debug > Stop Debugging to stop debugging. A shortcut for this action is Shift+F5 or use the Stop Debugging button in the toolbar.

Bridge to Kubernetes redirects all traffic for the database-api service. It redirects to the version of your application on your development computer. Bridge to Kubernetes also routes all outbound traffic from the application back to your Kubernetes cluster.

Note

By default, stopping the debugging task also disconnects your development computer from your Kubernetes cluster. To change this behavior, select Tools > Options, then select Kubernetes Debugging Tools. Set Disconnect After Debugging to False.

Screenshot shows the Disconnect After Debugging value in the Kubernetes Debugging Tools.

After updating this setting, your development computer remain connected when you stop and start debugging. To disconnect your development computer from your cluster click on the Disconnect button on the toolbar.

Set a breakpoint

In this section, you set a breakpoint in your service.

  1. In Solution Explorer, select MongoHelper.cs to open the file in the editor. If you don't see Solution Explorer, select View > Solution Explorer.

  2. Set your cursor on the first line of the CreateTask method body. Then select Debug > Toggle Breakpoint to set a breakpoint.

    Screenshot shows the CreateTask method with a breakpoint set in the first line.

    A shortcut for this action is F9.

  3. Select the start button next to Bridge to Kubernetes, as you did in the previous section. Debugging starts with the values you entered previously.

  4. In the browser that opens, enter a value into the todos and select Enter. The code reaches the breakpoint you entered. While doing real debugging tasks, you can use the debugging options to step through the code.

  5. Select Debug > Stop Debugging to stop debugging.

  6. To remove the breakpoint, select that line and then select Debug > Toggle Breakpoint or select F9.

Edit launch profile

If you need to change how Bridge to Kubernetes connects to your cluster, in this section, you'll edit the launch profile settings.

  1. In the Visual Studio command bar, click on the arrow next to the start button (green triangle or "play" icon) to open the dropdown, then click on databaseApi Debug Properties. Screenshot shows the Bridge to Kubernetes drop down menu.

  2. Click on the Edit profile for Bridge to Kubernetes link in the Launch Profiles dialog. Screenshot shows Launch Profiles dialog with a link to edit the Bridge to Kubernetes profile

Another way to get to this screen:

  1. Right-click on the project node in Solution Explorer, and choose Properties (or press Alt+Enter).

  2. Scroll down to Debug, and choose Open debug launch profiles UI.

Clean up resources

If you used the sample todo app for this tutorial, you can remove it from your cluster by using the Azure portal. If you cloned that repo locally, you can delete it manually.

Next steps

Learn more about Bridge to Kubernetes at How Bridge to Kubernetes works.

For information about supported features and a roadmap for Bridge to Kubernetes, see Bridge to Kubernetes roadmap.

To learn how to connect your development computer to a cluster by using Visual Studio Code, check out this article: