Dev Containers: Attach to Running Containers ... to Container in minikube/kind

828 Views Asked by At

I run a local Kubernetes kind cluster.

If I use "Dev Containers: Attach to Running Containers ...", then I only see control-plane container of the kind cluster (foo-control-plane).

But I don't see the containers (pods) which are inside the control-plane.

Is there a way to connect via Dev Containers to a container in a local minikube/kind cluster?

My goal is to debug a Go application with Delve

2

There are 2 best solutions below

2
Zeke Lu On

Since your goal is to debug a Go application with Delve (remotely), I think you should refer to How can I use Delve for remote debugging? and Debugging with Go extension instead.

Most of the guides below are copied from those docs.

  1. Start a headless instance of Delve inside the pod like this:

    dlv attach --headless --listen :4040 [pid] [/path/to/executable]
    

    Note: I encountered an error when running this command in a pod: Could not attach to pid 1: this could be caused by a kernel security setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope. In order to try the solution myself, I created a docker image to start the Delve server directly like this (see Dockerfile in the attachments section):

    CMD ["/app/dlv", "exec", "/app/goapp", "--listen=:4040", "--headless=true", "--api-version=2", "--log"]
    
  2. Forward the port to make the debug server available from outside:

    kubectl port-forward [pod name] 4040
    
  3. In vscode, create the .vscode/launch.json file:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Connect to pod",
          "type": "go",
          "request": "attach",
          "mode": "remote",
          "host": "localhost",
          "port": 4040,
          "debugAdapter": "dlv-dap",
          "substitutePath": [{ "from": "${workspaceFolder}", "to": "/app" }]
        }
      ]
    }
    

    It's important to set substitutePath correctly so that you can see the source code in vscode. Since the source code of my demo app is copied to /app and built from there (built with docker), the path recorded in the binary is /app/main.go, so I have to map from ${workspaceFolder} to /app.

  4. In vscode, start debugging with this configuration.

    vscode debugging

Attachments

  1. main.go:

    package main
    
    import "net/http"
    
    func main() {
        _ = http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            _, _ = w.Write([]byte("hello world!"))
        }))
    }
    
  2. Dockerfile:

    # syntax=docker/dockerfile:1
    
    # =============== build stage ===============
    FROM golang:1.18.4-alpine3.16 AS build
    WORKDIR /app
    
    RUN go install github.com/go-delve/delve/cmd/dlv@latest
    
    COPY . ./
    RUN go build -o goapp -v main.go
    
    # =============== final stage ===============
    FROM alpine:3.16.1 AS final
    
    WORKDIR /app
    COPY --from=build /app/goapp ./
    COPY --from=build /go/bin/dlv ./
    CMD ["/app/dlv", "exec", "/app/goapp", "--listen=:4040", "--headless=true", "--api-version=2", "--log"]
    
    • Build image from this file: docker build -t goapp:1.0 -f Dockerfile ..
    • Load the image into kind node (yes, I'm testing with kind): kind load docker-image goapp:1.0.
  3. goapp.yaml:

    apiVersion: v1
    kind: Pod
    metadata:
      name: goapp
      labels:
        app: goapp
    spec:
      containers:
        - name: goapp
          image: goapp:1.0
          imagePullPolicy: Never
          ports:
            - containerPort: 8080
            - containerPort: 4040
    
    • Create a pod defined by this file: kubectl apply -f goapp.yaml.
0
Zeke Lu On

Is there a way to connect via Dev Containers to a container in a local minikube/kind cluster?

Regarding this question, please see Attach to a container in a Kubernetes cluster:

To attach to a container in a Kubernetes cluster, first install the Kubernetes extension and kubectl along with the Dev Containers extension. Then select the Kubernetes explorer from the Activity bar and expand the cluster and Pod where the container you want to attach to resides. Finally, right-click on the container and select Attach Visual Studio Code from context menu.

[Attach to Kubernetes Container]