-
Notifications
You must be signed in to change notification settings - Fork 123
Add ephemeral debugging guide #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7037734
Add ephemeral debugging guide
1c17806
Use edge in dlv image target
20080f1
debug load images
2324b99
Add wait
ebee886
Use edge in load too
9244217
Add kind to quickstart prereqs
f4b46c8
Add instructions for M1/M2 users
a3bf00f
Add plus
7626238
Fixes
515a1ec
Add GOARCH note to quickstart and include instructions on setting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# syntax=docker/dockerfile:1.5 | ||
# This Dockerfile builds an image with the dlv debugger. See the debugging guide in the developer docs for details | ||
# on how to use it. | ||
FROM golang:1.21-alpine AS builder | ||
|
||
RUN go install github.com/go-delve/delve/cmd/dlv@latest | ||
|
||
FROM alpine:latest | ||
|
||
COPY --from=builder /go/bin/dlv /usr/bin/ | ||
|
||
CMD ["sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Debugging | ||
|
||
## Debugging NGF Remotely in Kubernetes | ||
|
||
This section will walk you through how to attach an ephemeral [dlv](https://github.com/go-delve/delve) debugger | ||
container to NGF while it's running in Kubernetes. This will allow you to remotely debug NGF running in Kubernetes | ||
using your IDE. | ||
|
||
- Create a `kind` cluster: | ||
|
||
```console | ||
make create-kind-cluster | ||
``` | ||
|
||
- Set GOARCH environment variable: | ||
|
||
The [Makefile](/Makefile) uses the GOARCH variable to build the binary and container images. The default value of GOARCH is `amd64`. | ||
|
||
If you are deploying NGINX Gateway Fabric on a kind cluster, and the architecture of your machine is not `amd64`, you will want to set the GOARCH variable to the architecture of your local machine. You can find the value of GOARCH by running `go env`. Export the GOARCH variable in your `~/.zshrc` or `~/.bashrc`. | ||
|
||
```console | ||
echo "export GOARCH=< Your architecture (e.g. arm64 or amd64) >" >> ~/.bashrc | ||
source ~/.bashrc | ||
``` | ||
|
||
or for zsh: | ||
|
||
```console | ||
echo "export GOARCH=< Your architecture (e.g. arm64 or amd64) >" >> ~/.zshrc | ||
source ~/.zshrc | ||
``` | ||
|
||
- Build debug images and install NGF on your kind cluster: | ||
|
||
- **For NGINX OSS:** | ||
|
||
```console | ||
make GOARCH=$GOARCH debug-install-local-build | ||
``` | ||
|
||
- **For NGINX Plus:** | ||
|
||
```console | ||
make GOARCH=$GOARCH debug-install-local-build-with-plus | ||
``` | ||
|
||
> Note: The default value of GOARCH in the [Makefile](/Makefile) is `amd64`. If you try and debug an amd64 container on an ARM machine you will see the following error in the dlv container logs: `could not attach to pid <pid>: function not implemented`. | ||
pleshakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> This is a known issue and the only workaround is to create an arm64 image by specifying `GOARCH=arm64` the above commands. | ||
> For more information, see this [issue](https://github.com/docker/for-mac/issues/5191) | ||
|
||
- Start kubectl proxy in the background: | ||
|
||
```console | ||
kubectl proxy & | ||
``` | ||
|
||
- Save the NGF Pod name: | ||
|
||
```console | ||
POD_NAME=<NGF Pod> | ||
``` | ||
|
||
- Run the following curl command to create an ephemeral debug container: | ||
pleshakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```console | ||
curl --location --request PATCH 127.0.0.1:8001/api/v1/namespaces/nginx-gateway/pods/$POD_NAME/ephemeralcontainers \ | ||
--header 'Content-Type: application/strategic-merge-patch+json' \ | ||
--data '{ | ||
"spec": | ||
{ | ||
"ephemeralContainers": | ||
[ | ||
{ | ||
"name": "dlv", | ||
"command": [ | ||
"/bin/sh", | ||
"-c", | ||
"PID=$(pgrep -f /usr/bin/gateway) && dlv attach $PID --headless --listen 127.0.0.1:40000 --api-version=2 --accept-multiclient --only-same-user=false" | ||
], | ||
"image": "dlv-debug:edge", | ||
"imagePullPolicy": "Never", | ||
"targetContainerName": "nginx-gateway", | ||
"stdin": true, | ||
"tty": true, | ||
"securityContext": { | ||
"capabilities": { | ||
"add": [ | ||
"SYS_PTRACE" | ||
] | ||
}, | ||
"runAsNonRoot":false | ||
} | ||
} | ||
] | ||
} | ||
}' | ||
``` | ||
|
||
- Verify that the dlv API server is running: | ||
|
||
```console | ||
kubectl logs -n nginx-gateway $POD_NAME -c dlv | ||
``` | ||
|
||
you should see the following log: | ||
|
||
```text | ||
API server listening at: 127.0.0.1:40000 | ||
kate-osborn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
- Kill the kubectl proxy process: | ||
|
||
```console | ||
kill <kubectl proxy PID> | ||
``` | ||
|
||
- Port-forward the dlv API server port on the NGF Pod: | ||
|
||
```console | ||
kubectl port-forward -n nginx-gateway $POD_NAME 40000 | ||
``` | ||
|
||
- Connect to the remote dlv API server through your IDE: | ||
- [jetbrains instructions](https://www.jetbrains.com/help/go/attach-to-running-go-processes-with-debugger.html) | ||
- [vscode instructions](https://github.com/golang/vscode-go/blob/master/docs/debugging.md) | ||
|
||
- Debug! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.