美文网首页
如何在容器debug?

如何在容器debug?

作者: zhu733756 | 来源:发表于2021-07-07 14:51 被阅读0次

    前言

    之前搬运了一个youtube视频,学习了一波大佬讲解的用vscode来debug容器的技巧,yyds!

    视频链接:https://www.zhihu.com/zvideo/1384611505288044544

    本篇来做个简单的记录,希望集成至少三篇以上的vscode debug技巧,包括并不限于:

    • 编程语言debug

    • 容器debug

    • k8s debug

    废话不多说,直接从第二篇开始,有空了更新其他的debug技巧!

    准备

    dockerfile和python脚本(PS:看看这发量,肯定值得学习一波!)

    image image

    假设上面的容器已经跑起来了,名称为dalaoyyds~

    详解

    上面的教程分为三个步骤,一步一步带你debug!

    entrypoint

    debug方法是启动一个entrypoint=bash的容器:

    docker run -i -d --entrypoint=bash dalaoyyds(容器id)
    
    

    使用exec可以进入容器执行操作:

    docker exec -it bashcontainerid(上面那个容器返回的容器id)

    接着你就可以do whatever you like。

    cp

    紧接上面的entrypoint操作,在容器内部执行命令后,使用cp将产出的结果复制到容器外面:

    docker cp xxxx:/src/time.txt ./src/time.txt.Container

    debugger

    image

    将上面的dockerfile作为基础模板,新增一个debugger镜像,安装debupy插件,然后使用ENTRYPOINT暴露一个服务端口。

    外部,使用-p 5678:5678来将主机和容器网络打通,一般默认情况,使用的是bridge network模式:

    docker run -p 5678:5678 xxx unitest

    vscode插件真香

    使用python remote attach插件, 前提是的项目下有py文件,会自动侦探,如果没能自动识别,检查安装了python插件。

    image

    打上断点后,就能在左侧显示locals变量值了。

    image

    golang debug

    视频中的方法介绍了python的debug技巧,我们能debug其他编程语言吗?

    以下抛砖引玉介绍下go语言容器debug。

    Windows workspace debug Remote containers

    第一步,安装vscode、vscode remote containers extension、docker和docker-compose。remote-containers扩展在vscode扩展安装,该插件是启动远程容器来debug本地代码,远程连接方式一般采用ssh。一般采用windows连接远程linux服务器。

    参考:https://stackoverflow.com/questions/63116145/no-open-folder-in-container-or-any-other-command-in-remote-containers-vs-code

    第二步,配置devcontainer demo,并按需修改:

    https://github.com/qdm12/godevcontainer/tree/master/.devcontainer

    第三步,选择Remote-Containers: Open Folder in Container..

    通过以上设置就能开启remote debug。

    以上的方案适用于本地的Windows来debug远程容器,本地需要安装docker desktop和wsl程序。

    Linux workspace debug local containers

    也可以选择安装linux版本的vscode,来完成以上操作。

    dlv

    就我个人而言,一般都是使用vscode来ssh远程linux主机开发。

    插件:remote-ssh

    在这种情况下,可以使用golang的debug神器dlv,在容器中安装待debug的二进制包和dlv,编辑主机到容器的映射关系config.yml,外部用vscode连接暴露的服务, 即可开始调试功能。

    lauch.json

    {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Remote Debug",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "",
            "port": 2000,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/cloud/cmd/cloudcore/cloudcore.go",
            "showLog": true,
            "env": {},
            "args": []
          }
        ]
    }
    

    config.yml

    
    substitute-path:
      - {from: /, to: /root/go/src/github.com/kubeedge}
    
    

    dockerfile

    
    ENV CGO_ENABLED 0
    
    COPY . /go/src/github.com/kubeedge/kubeedge
    
    RUN go build -gcflags "all=-N -l" -o /server github.com/kubeedge/kubeedge/cloud/cmd/cloudcore
    
    # Compile Delve
    RUN apk add --no-cache git
    RUN go get github.com/derekparker/delve/cmd/dlv
    
    FROM alpine:3.11
    
    EXPOSE 8080 2000
    
    # Allow delve to run on Alpine based containers.
    RUN apk add --no-cache libc6-compat
    
    WORKDIR /
    
    COPY --from=build-env /server /
    COPY --from=build-env /go/bin/dlv /
    
    COPY ./config.yml  ./
    
    # 启动服务的配置文件, 按需添加
    COPY cloudcore.yaml  /etc/kubeedge/config/cloudcore.yaml 
    COPY /root/.kube/config /root/.kube/config 
    
    # Run delve
    #CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", "/server"]
    #/dlv --listen=:40000 --headless=true --api-version=2 exec /server
    
    CMD ["/bin/sh"]
    
    

    docker run

     docker run -it --rm --name cloudcore --security-opt="seccomp=unconfined" --cap-add=SYS_PTRACE -p 2000:2000  cloudcore-debugger:latest sh
    

    通过以上配置就能开启调试模式。

    PS: 码字不易,欢迎点赞收藏~`

    相关文章

      网友评论

          本文标题:如何在容器debug?

          本文链接:https://www.haomeiwen.com/subject/aoioultx.html