当前位置:网站首页>goland IDE和delve调试位于kubernetes集群中的go程序
goland IDE和delve调试位于kubernetes集群中的go程序
2022-06-28 07:15:00 【nangonghen】
1 概述:
在本地电脑的ide中调试远程go程序,IDE为goland-2019.3,go程序暴露的http接口为GET /ip,go程序是以pod的形式运行在kubernetes集群中。当客户端请求到达目标go程序时,本地的IDE断点生效。
2 delve:
2.1简介
delve是一款go程序调式工具(git clone https://github.com/go-delve/delve.git),是一个简单易用的二进制程序,可用来启动目标go程序,进入dlv环境后可通过各种内置命令进行调试目标go程序。
2.2下载二进制文件
#下载的文件位于$GOPATH/bin/目录下
go get -u github.com/go-delve/delve/cmd/dlv
2.3 运行方式
方式1:
$GOPATH/bin/dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec /目标go二进制文件 --continue
方式2:
#先启动目标go程序,得到pid再运行以下命令
dlv attach 目标go程序的pid --headless --listen=:2345 --api-version=2 --accept-multiclient
2.4 其他说明
2.4.1 编译目标go程序时需要带上参数-gcflags=“all=-N -l”
2.4.2 推荐B站的一个delve的简易教程。
https://www.bilibili.com/video/BV1pb411i7nw?from=search&seid=1674747649731508669
3 go程序:
https://github.com/gzlj/http-demo
go程序暴露的http接口为:GET /ip
4 制作docker镜像:
4.1编译go程序
#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 编写Dockerfile
#将目标go程序的二进制可执行文件main和dlv可执行文件放到Dockerfile的目录
4.3 执行docker build命令构建镜像
docker build -t "192.168.1.70:5000/http:dlv" .
5 部署go程序至kubernetes集群
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

成功部署后,得知节点的31883端口映射至容器的2345端口(delve程序监听的端口)。
6 goland开启remote debug模式
6.1 新增go remote debug配置
在菜单栏中进行点击 Run —> Edit Configurations —> 新增 —> Go Remote —> 确定。

6.2 新增go remote debug配置
在代码/ip接口的http handler方法中开启断点,点击IDE的虫子图标以debug方式运行代码。
7 访问目标go程序并查看IDE的端点
以kubernetes node port方式访问go程序的/ip接口
#192.168.1.71是kubernetes集群的节点,31175是go程序的service NodePort。
curl 192.168.1.71:31175/ip && echo


边栏推荐
- R language Kolmogorov Smirnov tests whether the two samples follow the same distribution.
- Server body 18: understanding and thinking of UDP reliable transmission (feeling from reading Yunfeng blog)
- BACnet/IP网关如何采集楼宇集中控制系统数据
- Pytorch RNN learning notes
- 实时数据库 - 笔记
- [C language] detailed explanation of C language to obtain array length
- 自律挑战30天
- linux下修改mysql端口号
- DOM parsing of XML file case code sentence by sentence analysis
- It will cost 700 yuan to realize this issue. Does anyone do it?
猜你喜欢

Vivo browser rapid development platform practice - Overview

云原生(待更新)

Wechat applets - basics takes you to understand the life cycle of applets (I)

CRC32概述以及实现和使用

Practice and exploration of vivo live broadcast application technology

pytorch RNN 学习笔记

Devtools implementation principle and performance analysis practice

Pytorch RNN learning notes

什么是一致性哈希?可以应用在哪些场景?

Principle and practice of bytecode reference detection
随机推荐
Hungry for a long time, the brain will enter the "power saving mode"! Your senses are weakened and you can't see clearly
Mysql8.0和Mysql5.0访问jdbc连接
东方财富上开户是安全的吗
R 语言 Hitters 数据分析
R 语言 Kolmogorov-Smirnov 检验 2 个样本是否遵循相同的分布。
Encyclopedia of scala operators
hack the box:RouterSpace题解
Force buckle 515 Find the maximum value in each tree row
Self discipline challenge 30 days
「杰伦熊」暴跌96.6% 明星带货NFT为何遇冷?
Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
Modifying MySQL port number under Linux
R语言绘制 ggplot2 季节性图
Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
Overview, implementation and use of CRC32
Last 29 days
MySQL installation steps - how to create a virtual machine under Linux (1)
linux下修改mysql用户名root
Comprehensive analysis of real enterprise software testing process
网传互联网公司加班表,排名第一的没悬念