Skip to content

Commit bfa4119

Browse files
authored
Add X-mode for debugging minimal systens (#51)
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent 4717bd2 commit bfa4119

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ sudo mv ./kubectl-node_shell /usr/local/bin/kubectl-node_shell
2828
# Get standard bash shell
2929
kubectl node-shell <node>
3030

31+
# Use X-mode (mount /host, and do not enter host namespace)
32+
kubectl node-shell -x <node>
33+
3134
# Execute custom command
3235
kubectl node-shell <node> -- echo 123
3336

@@ -38,4 +41,38 @@ cat /etc/passwd | kubectl node-shell <node> -- sh -c 'cat > /tmp/passwd'
3841
kubectl node-shell <node> -- sh -c 'cat /tmp/passwd; rm -f /tmp/passwd'
3942
```
4043

44+
## X-mode
45+
46+
X-mode can be useful for debugging minimal systems that do not have a built-in shell (eg. Talos).
47+
Here's an example of how you can debug the network for a rootless kube-apiserver container without a filesystem:
48+
49+
```bash
50+
kubectl node-shell -x <node>
51+
52+
# Download crictl
53+
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.28.0/crictl-v1.28.0-linux-amd64.tar.gz -O- | \
54+
tar -xzf- -C /usr/local/bin/
55+
56+
# Setup CRI endpoint
57+
export CONTAINER_RUNTIME_ENDPOINT=unix:///host/run/containerd/containerd.sock
58+
59+
# Find your container
60+
crictl ps | grep kube-apiserver
61+
#3ff4626a9f10e e7972205b6614 6 hours ago Running kube-apiserver 0 215107b47bd7e kube-apiserver-talos-rzq-nkg
62+
63+
# Find pid of the container
64+
crictl inspect 3ff4626a9f10e | grep pid
65+
# "pid": 2152,
66+
# "pid": 1
67+
# "type": "pid"
68+
# "getpid",
69+
# "getppid",
70+
# "pidfd_open",
71+
# "pidfd_send_signal",
72+
# "waitpid",
73+
74+
# Go to network namespace of the pid, but keep mount namespace of the debug container
75+
nsenter -t 2152 -n
76+
```
77+
4178
*You need to be able to start privileged containers for that.*

kubectl-node_shell

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
set -e
33

44
kubectl=kubectl
5-
version=1.7.0
5+
version=1.8.0
66
generator=""
77
node=""
88
nodefaultctx=0
99
nodefaultns=0
1010
container_cpu="${KUBECTL_NODE_SHELL_POD_CPU:-100m}"
1111
container_memory="${KUBECTL_NODE_SHELL_POD_MEMORY:-256Mi}"
12+
volumes="[]"
13+
volume_mounts="[]"
14+
x_mode=0
1215
labels="${KUBECTL_NODE_SHELL_LABELS}"
1316

1417
if [ -t 0 ]; then
@@ -55,6 +58,12 @@ while [ $# -gt 0 ]; do
5558
kubectl="$kubectl --namespace=${key##*=}"
5659
shift
5760
;;
61+
-x)
62+
x_mode=1
63+
volumes='[{"hostPath":{"path":"/","type":""},"name":"host-root"}]'
64+
volume_mounts='[{"mountPath":"/host","name":"host-root"}]'
65+
shift
66+
;;
5867
--)
5968
shift
6069
break
@@ -106,16 +115,26 @@ fi
106115

107116
# Build the container command
108117
if [ $# -gt 0 ]; then
109-
cmd="[ $cmd_start $cmd_arg_prefix"
118+
if [ "$x_mode" -eq 1 ]; then
119+
cmd='['
120+
else
121+
cmd="[ $cmd_start $cmd_arg_prefix,"
122+
fi
123+
c=""
110124
while [ $# -gt 0 ]; do
111-
cmd="$cmd, \"$(echo "$1" | \
125+
cmd="${cmd}${c} \"$(echo "$1" | \
112126
awk '{gsub(/["\\]/,"\\\\&");gsub(/\x1b/,"\\u001b");printf "%s",last;last=$0"\\n"} END{print $0}' \
113127
)\""
128+
c=,
114129
shift
115130
done
116131
cmd="$cmd ]"
117132
else
118-
cmd="[ $cmd_start $cmd_default ]"
133+
if [ "$x_mode" = 1 ]; then
134+
cmd='null'
135+
else
136+
cmd="[ $cmd_start $cmd_default ]"
137+
fi
119138
fi
120139

121140
overrides="$(
@@ -137,13 +156,15 @@ cat <<EOT
137156
"resources": {
138157
"limits": { "cpu": "${container_cpu}", "memory": "${container_memory}" },
139158
"requests": { "cpu": "${container_cpu}", "memory": "${container_memory}" }
140-
}
159+
},
160+
"volumeMounts": $volume_mounts
141161
}
142162
],
143163
"tolerations": [
144164
{ "key": "CriticalAddonsOnly", "operator": "Exists" },
145165
{ "effect": "NoExecute", "operator": "Exists" }
146-
]
166+
],
167+
"volumes": $volumes
147168
}
148169
}
149170
EOT

0 commit comments

Comments
 (0)