zoukankan      html  css  js  c++  java
  • 超好用的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
    

      

  • 相关阅读:
    CppUnit
    vconfig in linux
    POSIX semaphore: sem_open, sem_close, sem_post, sem_wait
    (Windows Command) diskpart
    亮块检测及取出亮块的中心坐标
    CacheMetaData Method of Activity
    (C#) Encoding.
    (C#) 线程操作 限制可同时访问某一资源或资源池的线程数。Semaphore 类。Mutex类
    (C# 基础) 跳转 (break, continue, goto, return, throw ).
    (C#基础) 字符串数据和操作
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/11214266.html
Copyright © 2011-2022 走看看