Skip to content

Commit a7cf6df

Browse files
tkanngaylei
authored andcommitted
Add agentless mode document, add agentless to configuration option and fix bug when users debug pods which are on the same host,simultaneously. (#33)
1 parent 0b5c42b commit a7cf6df

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [screenshots](#screenshots)
1515
- [quick start](#quick-start)
1616
- [build from source](#build-from-source)
17+
- [port-forward and agentless](#port-forward-mode-And-agentless-mode)
1718
- [configuration](#configurations)
1819
- [future works](#future-works)
1920
- [implementation details](#details)
@@ -96,16 +97,38 @@ make plugin
9697
make agent-docker
9798
```
9899

100+
# port-forward mode And agentless mode
101+
102+
- `port-foward` mode: By default, `kubectl-debug` will directly connect with the target host. When `kubectl-debug` cannot connect to `targetHost:agentPort`, you can enable `port-forward` mode. In `port-forward` mode, the local machine listens on `localhost:agentPort` and forwards data to/from `targetPod:agentPort`.
103+
104+
105+
- `agentless` mode: By default, `debug-agent` needs to be pre-deployed on each node of the cluster, which consumes cluster resources all the time. Unfortunately, debugging Pod is a low-frequency operation. To avoid loss of cluster resources, the `agentless` mode has been added in [#31](https://github.com/aylei/kubectl-debug/pull/31). In `agentless` mode, `kubectl-debug` will first start `debug-agent` on the host where the target Pod is located, and then `debug-agent` starts the debug container. After the user exits, `kubectl-debug` will delete the debug container and `kubectl-debug` will delete the `debug-agent` pod at last.
106+
107+
99108
# Configurations
100109

101110
`kubectl-debug` uses [nicolaka/netshoot](https://github.com/nicolaka/netshoot) as the default image to run debug container, and use `bash` as default entrypoint.
102111

103112
You can override the default image and entrypoint with cli flag, or even better, with config file `~/.kube/debug-config`:
104113

105114
```yaml
106-
# debug agent listening port
115+
# debug agent listening port(outside container)
107116
# default to 10027
108117
agentPort: 10027
118+
119+
# whether using agentless mode
120+
# default to false
121+
agentless: true
122+
# namespace of debug-agent pod, used in agentless mode
123+
# default to 'default'
124+
agentPodNamespace: default
125+
# prefix of debug-agent pod, used in agentless mode
126+
# default to 'debug-agent-pod'
127+
agentPodNamePrefix: debug-agent-pod
128+
# image of debug-agent pod, used in agentless mode
129+
# default to 'aylei/debug-agent:latest'
130+
agentImage: aylei/debug-agent:latest
131+
109132
# daemonset name of the debug-agent, used in port-forward
110133
# default to 'debug-agent'
111134
debugAgentDaemonset: debug-agent

docs/zh-cn.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,36 @@ kubect-debug POD_NAME
5757

5858
Any trouble? [file and issue for help](https://github.com/aylei/kubectl-debug/issues/new)
5959

60+
61+
# port-forward 模式和 agentless 模式
62+
63+
- `port-foward`模式:默认情况下,`kubectl-debug`会直接与目标宿主机建立连接。当`kubectl-debug`无法与目标宿主机直连时,可以开启`port-forward`模式。`port-forward`模式下,本机会监听localhost:agentPort,并将数据转发至目标Pod的agentPort端口。
64+
65+
- `agentless`模式: 默认情况下,`debug-agent`需要预先部署在集群每个节点上,会一直消耗集群资源,然而调试 Pod 是低频操作。为避免集群资源损失,在[#31](https://github.com/aylei/kubectl-debug/pull/31)增加了`agentless`模式。`agentless`模式下,`kubectl-debug`会先在目标Pod所在宿主机上启动`debug-agent`,然后再启动调试容器。用户调试结束后,`kubectl-debug`会依次删除调试容器和在目的主机启动的`degbug-agent`
66+
67+
6068
# 默认镜像和 Entrypoint
6169

6270
`kubectl-debug` 使用 [nicolaka/netshoot](https://github.com/nicolaka/netshoot) 作为默认镜像. 默认镜像和指令都可以通过命令行参数进行覆盖. 考虑到每次都指定有点麻烦, 也可以通过文件配置的形式进行覆盖, 编辑 `~/.kube/debug-config` 文件:
6371

6472
```yaml
65-
# debug-agent 的端口
73+
# debug-agent 映射到宿主机的端口
6674
# 默认 10027
6775
agentPort: 10027
76+
77+
# 是否开启ageless模式
78+
# 默认 false
79+
agentless: true
80+
# agentPod 的 namespace, agentless模式可用
81+
# 默认 default
82+
agentPodNamespace: default
83+
# agentPod 的名称前缀,后缀是目的主机名, agentless模式可用
84+
# 默认 debug-agent-pod
85+
agentPodNamePrefix: debug-agent-pod
86+
# agentPod 的镜像, agentless模式可用
87+
# 默认 aylei/debug-agent:latest
88+
agentImage: aylei/debug-agent:latest
89+
6890
# debug-agent DaemonSet 的名字, port-forward 模式时会用到
6991
# 默认 'debug-agent'
7092
debugAgentDaemonset: debug-agent
@@ -84,6 +106,4 @@ command:
84106
- '-l
85107
```
86108
87-
当 debug-agent 无法直连时, 可以开启 port-forward 模式来绕过
88-
89109
> `kubectl-debug` 会将容器的 entrypoint 直接覆盖掉, 这是为了避免在 debug 时不小心启动非 shell 进程.

pkg/plugin/cmd.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"github.com/aylei/kubectl-debug/version"
87
"io"
98
"net/http"
109
"net/url"
@@ -14,6 +13,8 @@ import (
1413
"sync"
1514
"time"
1615

16+
"github.com/aylei/kubectl-debug/version"
17+
1718
"k8s.io/apimachinery/pkg/labels"
1819

1920
term "github.com/aylei/kubectl-debug/pkg/util"
@@ -70,7 +71,7 @@ You may set default configuration such as image and command in the config file,
7071
usageError = "expects 'debug POD_NAME' for debug command"
7172

7273
defaultAgentImage = "aylei/debug-agent:latest"
73-
defaultAgentPodNamePrefix = "debug-agent-pod-"
74+
defaultAgentPodNamePrefix = "debug-agent-pod"
7475
defaultAgentPodNamespace = "default"
7576
)
7677

@@ -285,6 +286,10 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, args []string, argsLenAtDash
285286
if config.PortForward {
286287
o.PortForward = true
287288
}
289+
if config.Agentless {
290+
o.AgentLess = true
291+
}
292+
288293
o.Ports = []string{strconv.Itoa(o.AgentPort)}
289294
o.Config, err = configLoader.ClientConfig()
290295
if err != nil {
@@ -332,8 +337,7 @@ func (o *DebugOptions) Run() error {
332337
var agentPod *corev1.Pod
333338
if o.AgentLess {
334339
o.AgentPodNode = pod.Spec.NodeName
335-
// add node name as suffix
336-
o.AgentPodName = o.AgentPodName + o.AgentPodNode
340+
o.AgentPodName = fmt.Sprintf("%s-%s", o.AgentPodName, uuid.NewUUID())
337341
agentPod = o.getAgentPod()
338342
agentPod, err = o.launchPod(agentPod)
339343
if err != nil {

pkg/plugin/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Config struct {
1313
DebugAgentNamespace string `yaml:"debugAgentNamespace,omitempty"`
1414
Command []string `yaml:"command,omitempty"`
1515
PortForward bool `yaml:"portForward,omitempty"`
16+
Agentless bool `yaml:"agentless,omitempty"`
1617
AgentPodNamePrefix string `yaml:"agentPodNamePrefix,omitempty"`
1718
AgentPodNamespace string `yaml:"agentPodNamespace,omitempty"`
1819
AgentImage string `yaml:"agentImage,omitempty"`

0 commit comments

Comments
 (0)