超好用的K8s診斷工具:kubectl-debug


在K8s環境部署應用后,經常遇到需要進入pod進行排錯。除了查看pod logs和describe方式之外,傳統的解決方式是在業務pod基礎鏡像中提前安裝好procps、net-tools、tcpdump、vim等工具。但這樣既不符合最小化鏡像原則,又徒增Pod安全漏洞風險。
有沒有一種即插即用的排錯工具呢?
今天為大家推薦一款K8s pod診斷工具,kubectl-debug是一個簡單、易用、強大的 kubectl 插件, 能夠幫助你便捷地進行 Kubernetes 上的 Pod 排障診斷。它通過啟動一個排錯工具容器,並將其加入到目標業務容器的pid, network, user 以及 ipc namespace 中,這時我們就可以在新容器中直接用 netstat, tcpdump 這些熟悉的工具來解決問題了, 而業務容器可以保持最小化, 不需要預裝任何額外的排障工具。
kubectl-debug 包含兩部分:
kubectl-debug:命令行工具;
debug-agent:部署在K8s的node上,用於啟動關聯排錯工具容器;

 

# install kubectl-debug


export PLUGIN_VERSION=0.1.1

#linux x86_64

curl -Lo kubectl-debug.tar.gz https://github.com/aylei/kubectl-debug/releases/download/v${PLUGIN_VERSION}/kubectl-debug_${PLUGIN_VERSION}_linux_amd64.tar.gz

tar -zxvf kubectl-debug.tar.gz kubectl-debug
sudo mv kubectl-debug /usr/local/bin/
# install debug-agent daemonset

kubectl apply -f https://raw.githubusercontent.com/aylei/kubectl-debug/master/scripts/agent_daemonset.yml
或者使用 helm 安裝
helm install -n=debug-agent ./contrib/helm/kubectl-debug}}}

 

簡單使用(K8s v1.15.0):

{{{# kubectl 1.12.0 或更高的版本, 可以直接使用:
kubectl debug -h

老版本的 kubectl 無法自動發現插件, 需要直接調用 binary
kubect-debug POD_NAME

假如安裝了 debug-agent 的 daemonset, 可以略去 --agentless 來加快啟動速度
之后的命令里會略去 --agentless
kubectl debug POD_NAME --agentless

假如 Pod 處於 CrashLookBackoff 狀態無法連接, 可以復制一個完全相同的 Pod 來進行診斷
kubectl debug POD_NAME --fork

假如 Node 沒有公網 IP 或無法直接訪問(防火牆等原因), 請使用 port-forward 模式
kubectl debug POD_NAME --port-forward --daemonset-ns=kube-system --daemonset-name=debug-agent

  

Demo:

 

 

debug-agent 兩種運行方式:
daemon-set模式,agent pod預先部署在所有node上,會始終占用資源,對於排錯調試頻率不高的環境造成資源浪費;
agentless模式,kubectl-debug執行命令后,才創建agent pod和排錯工具容器,並在退出后刪除工具容器和agent pod。由於每次執行都要重新拉起agent,啟動會比daemon-set模式稍慢。
使用-a, --agentless開啟agentless模式:

 

進階使用:
排錯init-container:
kubectl debug demo-pod --container=init-pod


排錯crash pod:
kubectl debug POD_NAME --fork

  

離線配置:
--image:可自定義排錯工具容器鏡像,改為私有鏡像倉庫,默認為nicolaka/netshoot:latest

 

--agent-image:在agentless模式下,自定義debug-agent鏡像,默認為aylei/debug-agent:latest。在daemon-set模式下,直接將debug-agent daemonset pod template修改為私有倉庫鏡像即可。

 

配置文件:
~/.kube/debug-config,通過配置文件修改默認參數,免去使用命令時設置flag。
# debug agent listening port(outside container)
default to 10027
agentPort: 10027
​
whether using agentless mode
default to false
agentless: true
namespace of debug-agent pod, used in agentless mode
default to 'default'
agentPodNamespace: default
prefix of debug-agent pod, used in agentless mode
default to  'debug-agent-pod'
agentPodNamePrefix: debug-agent-pod
image of debug-agent pod, used in agentless mode
default to 'aylei/debug-agent:latest'
agentImage: aylei/debug-agent:latest
​
daemonset name of the debug-agent, used in port-forward
default to 'debug-agent'
debugAgentDaemonset: debug-agent
daemonset namespace of the debug-agent, used in port-forwad
default to 'default'
debugAgentNamespace: kube-system
whether using port-forward when connecting debug-agent
default false
portForward: true
image of the debug container
default as showed
image: nicolaka/netshoot:latest
start command of the debug container
default ['bash']
command:
- '/bin/bash'
- '-l'

  

實例使用:

[root@k8s-hd-master03 ~]# kubectl-debug ai-****-dev-9c6cb7488-5l5nl  -n zhiyi-system-test  --port-forward --daemonset-ns=default --daemonset-name=debug-agent
pod ai-live-dev-9c6cb7488-5l5nl PodIP 172.20.2.167, agentPodIP 192.168.0.66
wait for forward port to debug agent ready...
Forwarding from 127.0.0.1:10027 -> 10027
Handling connection for 10027


bash-5.0# ps aux
PID   USER     TIME  COMMAND
    1 root      1:30 java -jar /home/ai-live-web.jar  -Xms512m -Xmx2048m --spring.profiles.active=dev --server.port=8080
   55 root      0:00 bash
   61 root      0:00 ps aux
bash-5.0# 
bash-5.0# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      1/java
bash-5.0# 

  

對於沒有安裝yum ,apt-get 的鏡像可以掛載 centos 的sidecar 鏡像, 再進行操作, 如安裝 redis 命令, 再使用redis-cli 命令


 kubectl-debug mall-order-provider-6b8665fd67-w74lb -n xitu-qa --image centos --port-forward --daemonset-ns=default --daemonset-name=debug-agent

[root@mall-order-provider-6b8665fd67-w74lb /]# yum install -y redis

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM