登录 Pod 所在节点的 Shell 脚本
| #!/usr/bin/env bash |
| |
| |
| |
| |
| if [ $# -lt 1 ];then |
| printf "Usage: %s <Pod> [Namespace]\n" "$0" |
| printf " The pod parameter is the name of the target pod that needs to be debugged\n" |
| printf " The namespace paramater is the namespace where the target pod is located\n" |
| exit 1 |
| fi |
| |
| for cmd in jq |
| do |
| which $cmd &>/dev/null |
| if [ $? -ne 0 ];then |
| echo "$cmd command not found" |
| exit |
| fi |
| done |
| |
| pod="$1" |
| namespace="${2:-default}" |
| nodename="$(kubectl get pod $pod -n $namespace -o json | jq -r '.spec.nodeName')" |
| cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", "bash"]' |
| container_id="$(kubectl get pod $pod -n $namespace -o json | jq -r '.status.containerStatuses[].containerID')" |
| container_id="${container_id/containerd:\/\/}" |
| pod="kube-nodeshell-$(env LC_ALL=C tr -dc a-z0-9 </dev/random | head -c 6)" |
| template='{"spec": {"nodeName": "'"$nodename"'","hostPID": true,"hostNetwork": true,"containers": [{"securityContext": {"privileged": true},"image": "alpine","name": "nsenter","stdin": true,"stdinOnce": true,"tty": true,"command": '$cmd', "env": [{"name": "container_id", "value": "'"$container_id"'"}]}],"tolerations": [{"operator": "Exists"}]}}' |
| |
| kubectl -n $namespace run --image=docker.m.daocloud.io/alpine:3.15 --restart=Never --rm --overrides="$template" -it $pod |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!