在上一篇文章里我们主要介绍安装k8s集群内的基础服务nginx-ingress-controller,这里我们介绍一下k8s的pod调试工具。在k8s的容器环境中,去排查问题是避免不了的,这就需要在pod里有各种工具,例如top,ps,pidstat,vmstat,nslookup,netstat,tcpdump,dig,traceroute等。可是有些pod里就没有这些工具(当然从制作image的角度来说确实是不应该把和应用自身无关的tools放到image里),这无疑给我们调试排查问题带来巨大的困难。kubectl-debug就是为了解决这个问题的,它不是k8s官方工具,是个人项目,但是用起来却比较方便,这里我们用0.1.1本版。kubectl-debug具体原理不在这里详细展开,有兴趣的同学可以到github的项目里看一下(https://github.com/aylei/kubectl-debug)。
下载binary:
这里在github下载有时候会很慢,甚至断掉。
mkdir -p /opt/sw/kubernetes/kube-pod-debug
cd /opt/sw/kubernetes/kube-pod-debug
wget https://github.com/aylei/kubectl-debug/releases/download/v0.1.1/kubectl-debug_0.1.1_linux_amd64.tar.gz
tar -zxvf kubectl-debug_0.1.1_linux_amd64.tar.gz
cp kubectl-debug /usr/bin
whereis kubectl-debug
在私有harbor repo中创建public project:
以前文章里介绍过如何搭建私有的harbor repo,可以参考。
创建debug-agent镜像,并push到上面步骤的repo project里:
docker pull aylei/debug-agent:latest
docker images|grep debug
docker tag aylei/debug-agent:latest 172.20.11.41:1034/infra-pub/kubectl-debug-agent:latest
docker push 172.20.11.41:1034/infra-pub/kubectl-debug-agent:latest
创建debug-tool镜像,并push到上面步骤的repo project里:
docker pull nicolaka/netshoot:latest
docker images|grep nicolaka
docker tag nicolaka/netshoot:latest 172.20.11.41:1034/infra-pub/kubectl-debug-container:latest
docker push 172.20.11.41:1034/infra-pub/kubectl-debug-container:latest
创建kube-debug配置文件:
对于配置文件里的参数比较多,就不逐一在这里详述,有兴趣的同学可以去github上(https://github.com/aylei/kubectl-debug#configuration)看一下kubectl-debug的参数配置文档。
cat > ~/.kube/debug-config << EOF
# 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: 172.20.11.41:1034/infra-pub/kubectl-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 tool container, default as nicolaka/netshoot:latest
image: 172.20.11.41:1034/infra-pub/kubectl-debug-container:latest
# start command of the debug container, default ['bash']
command:
- '/bin/bash'
- '-l'
EOF
使用kubectl-debug工具调试pod:
请注意,当开始对一个pod debug的时候,kubectl-debug会在目标pod所在的node创建2个items。一个是debug-agent,它的本质是一个pod。另一个是debug-tool container,它的本质是docker container,不是pod。当然,在退出kubectl-debug的target pod的时候这两个items都会被销毁。
调试pod:
我们以集群中的coredns的pod为例子进行调试,当debug进入之后我们就可以用预置在debug-tool container里的curl,ping,tracroute,nslookup等诸多工具了。
kubectl get pods -n kube-system
kubectl-debug deployment-coredns-c58b8b7fc-kp2j8 --namespace kube-system
查看debug-agent pod和debug-tool container:
kubectl get pods --all-namespaces=true
docker ps|grep debug-container
退出debug:
exit