VSCode is an amazing text editor, it offers a wide variety of features and an awesome list of extensions. One feature that I like to use is “Attach to a container in a Kubernetes cluster”, that is attaching my code to a running instance of my app running in my local kubernetes cluster.
Super useful if you need to test, debug or monitor your app while it is running. I recently found myself in a situation where I had to restart the app and reattached myself to the container over and over again. It became tedious to find the pod in the Kubernetes view, right clicking on the pod once I found it to select the attach option. Having to do this over and over again prompted me to look into whether this could be easier to do via some configuration. I wanted something that would allow me to easily attach to a pod. I looked around the web and found nothing so this is what I came up with.
First, edit or create the launch.json file, under it add a new configuration as shown below. In my case I was working with a GO app that has been configured with Delve, but this configuration will work for any other that can expose a debug port like GO does through Delve
| |
See Launch Settings Attributes for more information.
The second configuration required to make this work is a task.json as seen below.
| |
This JSON snippet defines a Visual Studio Code task for port forwarding a Kubernetes deployment. The task is named “Port Forward my-service” and uses the shell type to execute the command kubectl port-forward deployment/my-service 10000:2345. This command forwards traffic from port 2345 on the my-service deployment in your Kubernetes cluster to port 10000 on your local machine, allowing you to access services running inside the cluster as if they were local.
The isBackground property is set to true, meaning the task runs in the background and does not block other tasks or editor actions. The problemMatcher configuration helps VS Code detect when the port forwarding is active by looking for output lines that match the pattern “Forwarding from”. This allows the editor to track the task’s status and manage its lifecycle appropriately. The phrase “Forwarding from” is part of the output from kubectl when you run the port-forward command.
The presentation section customizes how the task output is displayed. It ensures command output is echoed, the output panel is not automatically revealed, focus is not shifted to the panel, and a dedicated panel is used for this task. This setup is useful for development workflows that require remote debugging or service access through Kubernetes port forwarding, integrating seamlessly with VS Code’s task system.
The result of these new configurations is this.

Now I can simply press F5 and quickly attach my code to the running app.