上次也遇到过一次需要在k8s的容器里debug程序的情况,当时找了个偷懒的办法绕过去了,
这回绕不过去了,老老实实整一把。主要还是用dlv配合goland的远程调试功能。
kubectl top node
查看资源使用情况,选一个相对清闲的node,记住这个主机名nodeNamegit clone
便于分辨脏文件)将本地代码传到node上的某个路径下,记录这个代码路径codePathdocker build -f fileName -t image:tag .
# 选用go1.19的alpine版本
FROM golang:1.19-alpine
# 设置go代理并下载dlv源码并编译安装
RUN go env -w GOPROXY="https://goproxy.cn,direct" && go mod download github.com/go-delve/delve@v1.9.1
WORKDIR /go/pkg/mod/github.com/go-delve/delve@v1.9.1/
RUN go mod tidy
RUN go mod vendor
RUN go install ./cmd/dlv/
# 替换源并下载gcc,非必须,看编译是否依赖
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk --no-cache add gcc
WORKDIR /go
kubectl apply -f deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: go-env
name: go-env
spec:
selector:
matchLabels:
k8s-app: go-env
template:
metadata:
creationTimestamp: null
labels:
k8s-app: go-env
spec:
containers:
- image: ${ImageTag}
imagePullPolicy: IfNotPresent
name: go-env
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
volumeMounts:
- mountPath: /go/src/code
name: code
dnsPolicy: ClusterFirst
nodeName: ${NodeName} # node name
restartPolicy: Always
schedulerName: default-scheduler
volumes:
- hostPath:
path: ${CodePath} # /path/to/code
type: Directory
name: code
apiVersion: v1
kind: Service
metadata:
name: go-env
spec:
externalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 32345
port: 2345
protocol: TCP
targetPort: 2345
selector:
k8s-app: go-env
sessionAffinity: None
type: NodePort
kubectl exec
进入容器dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient ./your_package
,等待输出API server listening at: [::]:2345