30

技术漫谈 | 超好用的K8s诊断工具:kubectl-debug

 4 years ago
source link: https://www.tuicool.com/articles/ueeQn2f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

原创作者:Song

在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上,用于启动关联排错工具容器;

工具安装(CentOS 7.5):

# 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:

uyE3UfR.png!web

QRbmQzb.png!web

debug-agent 两种运行方式:

daemon-set模式,agent pod预先部署在所有node上,会始终占用资源,对于排错调试频率不高的环境造成资源浪费;

agentless模式,kubectl-debug执行命令后,才创建agent pod和排错工具容器,并在退出后删除工具容器和agent pod。由于每次执行都要重新拉起agent,启动会比daemon-set模式稍慢。

使用-a, --agentless开启agentless模式:

ZBnANvb.png!web

进阶使用:

排错init-container:

kubectl debug demo-pod --container=init-pod

排错crash pod:

kubectl debug POD_NAME --fork

离线配置:

--image:可自定义排错工具容器镜像,改为私有镜像仓库,默认为nicolaka/netshoot:latest

IrYf2aU.png!web

--agent-image:在agentless模式下,自定义debug-agent镜像,默认为aylei/debug-agent:latest。在daemon-set模式下,直接将debug-agent daemonset pod template修改为私有仓库镜像即可。

YFnE7vV.png!web

配置文件:

~/.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'

​项目地址:

https://github.com/aylei/kubectl-debug

工具镜像:

https://github.com/nicolaka/netshoot

原文链接: https://mp.weixin.qq.com/s/LtrW09N6wQEdE6hnmwL0qg

关于睿云智合

深圳睿云智合科技有限公司成立于2012年,总部位于深圳,并分别在成都、深圳设立了研发中心,北京、上海设立了分支机构,核心骨干人员全部为来自金融、科技行业知名企业资深业务专家、技术专家。早期专注于为中国金融保险等大型企业提供创新技术、电子商务、CRM等领域专业咨询服务。

自2016年始,在率先将容器技术引进到中国保险行业客户后,公司组建了专业的容器技术产品研发和实施服务团队,旨在帮助中国金融行业客户将容器创新技术应用于企业信息技术支持业务发展的基础能力改善与提升,成为中国金融保险行业容器技术服务领导品牌。

此外,凭借多年来在呼叫中心领域的业务经验与技术积累,睿云智合率先在业界推出基于开源软交换平台FreeSwitch的微服务架构多媒体数字化业务平台,将语音、视频、webchat、微信、微博等多种客户接触渠道集成,实现客户统一接入、精准识别、智能路由的CRM策略,并以容器化治理来支持平台的全应用生命周期管理,显著提升了数字化业务处理的灵活、高效、弹性、稳定等特性,为帮助传统企业向“以客户为中心”的数字化业务转型提供完美的一站式整体解决方案。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK