運維之美2019-08-18 02:27:06
export TARGET_ID=666666666
# 加入目標容器的 network, pid 以及 ipc namespace
docker run -it --network=container:$TARGET_ID --pid=container:$TARGET_ID --ipc=container:$TARGET_ID busybox
插件查詢 ApiServer:demo-pod 是否存在,所在節點是什麼
ApiServer 返回 demo-pod 所在所在節點
插件請求在目標節點上創建 Debug Agent Pod
Kubelet 創建 Debug Agent Pod
插件發現 Debug Agent 已經 Ready,發起 debug 請求(長連接)
Debug Agent 收到 debug 請求,創建 Debug 容器並加入目標容器的各個 Namespace 中,創建完成後,與 Debug 容器的 tty 建立連接
brew install aylei/tap/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
# macos
curl -Lo kubectl-debug.tar.gz https://github.com/aylei/kubectl-debug/releases/download/v${PLUGIN_VERSION}/kubectl-debug_${PLUGIN_VERSION}_darwin_amd64.tar.gz
tar -zxvf kubectl-debug.tar.gz kubectl-debug
sudo mv kubectl-debug /usr/local/bin/
kubectl debug target-pod --agentless --port-forward
➜ ~ kubectl debug demo-pod
root @ /
[2] 🐳 → iftop -i eth0
interface: eth0
IP address is: 10.233.111.78
MAC address is: 86:c3:ae:9d:46:2b
# (圖片略去)
root @ /
[3] 🐳 → drill -V 5 demo-service
;; ->>HEADER opcode: QUERY, rcode: NOERROR, id: 0
;; flags: rd ; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;; demo-service. IN A
;; ANSWER SECTION:
;; AUTHORITY SECTION:
;; ADDITIONAL SECTION:
;; Query time: 0 msec
;; WHEN: Sat Jun 1 05:05:39 2019
;; MSG SIZE rcvd: 0
;; ->>HEADER opcode: QUERY, rcode: NXDOMAIN, id: 62711
;; flags: qr rd ra ; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;; demo-service. IN A
;; ANSWER SECTION:
;; AUTHORITY SECTION:
. 30 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2019053101 1800 900 604800 86400
;; ADDITIONAL SECTION:
;; Query time: 58 msec
;; SERVER: 10.233.0.10
;; WHEN: Sat Jun 1 05:05:39 2019
;; MSG SIZE rcvd: 121
root @ /
[4] 🐳 → tcpdump -i eth0 -c 1 -Xvv
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
12:41:49.707470 IP (tos 0x0, ttl 64, id 55201, offset 0, flags [DF], proto TCP (6), length 80)
demo-pod.default.svc.cluster.local.35054 > 10-233-111-117.demo-service.default.svc.cluster.local.8080: Flags [P.], cksum 0xf4d7 (incorrect -> 0x9307), seq 1374029960:1374029988, ack 1354056341, win 1424, options [nop,nop,TS val 2871874271 ecr 2871873473], length 28
0x0000: 4500 0050 d7a1 4000 4006 6e71 0ae9 6f4e E..P[email protected]@.nq..oN
0x0010: 0ae9 6f75 88ee 094b 51e6 0888 50b5 4295 ..ou...KQ...P.B.
0x0020: 8018 0590 f4d7 0000 0101 080a ab2d 52df .............-R.
0x0030: ab2d 4fc1 0000 1300 0000 0000 0100 0000 .-O.............
0x0040: 000e 0a0a 08a1 86b2 ebe2 ced1 f85c 1001 .............\..
1 packet captured
11 packets received by filter
0 packets dropped by kernel
root @ /
[5] 🐳 → tail -f /proc/1/root/log_
Hello, world!
新 Pod 的所有 Labels 會被刪掉,避免 Service 將流量導到 fork 出的 Pod 上
新 Pod 的 ReadinessProbe 和 LivnessProbe 也會被移除,避免 kubelet 殺死 Pod
新 Pod 中目標容器(待排障的容器)的啟動命令會被改寫,避免新 Pod 繼續 Crash
➜ ~ kubectl debug demo-pod --fork
root @ /
[4] 🐳 → chroot /proc/1/root
root @ /
[#] 🐳 → ls
bin entrypoint.sh home lib64 mnt root sbin sys tmp var
dev etc lib media proc run srv usr
root @ /
[#] 🐳 → ./entrypoint.sh
# 觀察執行啟動腳本時的信息並根據信息進一步排障
不支持診斷 CrashLoopBackoff 中的 Pod
強制要求預先安裝一個 Debug Agent 的 DaemonSet
不支持公有云(節點沒有公網 IP 或公網 IP 因為防火牆原因無法訪問時,就無法 debug)
沒有權限限制,安全風險很大
https://github.com/kubernetes/kubernetes/issues/27140
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/troubleshoot-running-pods.md
https://github.com/aylei/kubectl-debug
https://github.com/aylei/kubectl-debug/releases/tag/v0.1.1
https://github.com/aylei/kubectl-debug/blob/master/docs/zh-cn.md
https://github.com/nicolaka/netshoot
https://blog.softwaremill.com/docker-support-in-new-java-8-finally-fd595df0ca54
來源:Aylei's Blog
原文:http://t.cn/AiWl4YKl
題圖:來自谷歌圖片搜索
版權:本文版權歸原作者所有
投稿:歡迎投稿,投稿郵箱: [email protected]
你可能還喜歡
點擊下方圖片即可閲讀
談談 Kubernetes 架構
朋友會在“發現-看一看”看到你“在看”的內容