1 Overview:
Debug the remote go program in the ide of the local computer, the IDE is goland-2019.3, the http interface exposed by the go program is GET /ip, and the go program runs in the kubernetes cluster in the form of a pod. When the client request reaches the target go program, the local IDE breakpoint takes effect.
2 delve:
2.1 Introduction
delve is a go program debugging tool (git clone https://github.com/go-delve/delve.git). It is a simple and easy-to-use binary program that can be used to start the target go program. After entering the dlv environment, it can be passed Various built-in commands are used to debug the target go program.
2.2 Download binary files
#The downloaded file is located in the $GOPATH/bin/ directory
go get -u github.com/go-delve/delve/cmd/dlv
2.3 Mode of operation
Method 1:
$GOPATH/bin/dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec /target go binary file --continue
Method 2: #Start the target go program
first, get the pid and then run the following command
dlv attach the pid of the target go program --headless --listen=:2345 --api-version=2 --accept-multiclient
2.4 Other instructions
2.4.1 The parameter -gcflags="all=-N -l" needs to be brought when compiling the target go program
2.4.2 Recommend a simple delve tutorial at station B.
https://www.bilibili.com/video/BV1pb411i7nw?from=search&seid=1674747649731508669
3 go program:
https://github.com/gzlj/http-demo
The http interface exposed by the go program is: GET /ip
4 Make a docker image:
4.1 Compile Go Program
#cd $GOPATH/src/github.com/gzlj/http-demo/cmd/gracehttp-demo
#编译目标go程序时需要带上参数-gcflags="all=-N -l"
go build -gcflags="all=-N -l" -o main
4.2 Writing Dockerfile
#Place the binary executable file main and dlv executable files of the target go program into the Dockerfile directory
4.3 Execute the docker build command to build the image
docker build -t "192.168.1.70:5000/http:dlv" .
5 Deploy the go program to the kubernetes cluster
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- command:
- /dlv
- --listen=:2345
- --headless=true
- --api-version=2
- --accept-multiclient
- exec
- /main
- --continue
image: 192.168.1.70:5000/http:dlv
imagePullPolicy: Always
name: web
#容器需要SYS_PTRACE 或 直接作为特权容器
securityContext:
capabilities:
add:
- SYS_PTRACE
---
apiVersion: v1
kind: Service
metadata:
labels:
app: web
name: web
namespace: default
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
- name: dlv
port: 2345
protocol: TCP
targetPort: 2345
type: NodePort
selector:
app: web
After successful deployment, it is learned that port 31883 of the node is mapped to port 2345 of the container (the port monitored by the delve program).
6 goland turns on remote debug mode
6.1 Added go remote debug configuration
In the menu bar, click Run —> Edit Configurations —> Add —> Go Remote —> OK.
6.2 Added go remote debug configuration
Open the breakpoint in the http handler method of the code/ip interface, and click the bug icon of the IDE to run the code in debug mode.
7 Access the target go program and view the endpoint of the IDE
Use kubernetes node port to access the /ip interface of the go program
#192.168.1.71是kubernetes集群的节点,31175是go程序的service NodePort。
curl 192.168.1.71:31175/ip && echo