5.9. 收集主机网络追踪

有时,追踪网络通信并同时捕获多个节点上的数据包简化了与网络相关的问题的故障排除。

您可以使用 oc adm must-gather 命令和 registry.redhat.io/openshift4/network-tools-rhel8 容器镜像的组合来收集来自节点的数据包。分析数据包捕获可帮助您对网络通信问题进行故障排除。

oc adm must-gather 命令用于在特定节点上的 pod 中运行 tcpdump 命令。tcpdump 命令记录 pod 中捕获的数据包。当 tcpdump 命令退出时,oc adm must-gather 命令会用从 pod 捕获的数据包传输到您的客户端机器。

提示

以下流程中的示例命令演示了使用 tcpdump 命令执行数据包捕获。但是,您可以在 --image 参数中指定的容器镜像中运行任何命令,以便同时从多个节点收集故障排除信息。

先决条件

  • 您可以使用具有 cluster-admin 角色的用户访问集群。
  • 已安装 OpenShift CLI(oc)。

流程

  1. 运行以下命令,在某些节点上运行来自主机网络的数据包捕获:

    $ oc adm must-gather \
        --dest-dir /tmp/captures \  <.>
        --source-dir '/tmp/tcpdump/' \  <.>
        --image registry.redhat.io/openshift4/network-tools-rhel8:latest \  <.>
        --node-selector 'node-role.kubernetes.io/worker' \  <.>
        --host-network=true \  <.>
        --timeout 30s \  <.>
        -- \
        tcpdump -i any \  <.>
        -w /tmp/tcpdump/%Y-%m-%dT%H:%M:%S.pcap -W 1 -G 300

    <.> --dest-dir 参数指定 oc adm must-gather 将数据包捕获到相对于客户端机器上 /tmp/captures 的目录中。您可以指定任何可写目录。<.> 当 tcpdumpoc adm must-gather 启动时的 debug pod 中运行时,--source-dir 参数指定数据包捕获的临时存储在 pod 上的 /tmp/tcpdump 目录中。<.> The --image 参数指定包含 tcpdump 命令的容器镜像。<.> --node-selector 参数和示例值指定在 pod 上的 /tmp/tcpdump 目录中执行数据包捕获。作为替代方案,您可以指定 --node-name 参数而不是在单个节点上运行数据包捕获。如果省略 --node-selector--node-name 参数,则数据包捕获将在所有节点上执行。<.> --host-network=true 参数是必需的,以便在节点的网络接口上执行数据包捕获。<.> --timeout 参数和值指定运行 debug pod 达到 30 秒。如果没有指定 --timeout 参数和持续时间,则 debug pod 会运行 10 分钟。<.> -i any 参数用于 tcpdump 命令,指定捕获所有网络接口上的数据包。作为替代方案,您可以指定网络接口名称。

  2. 执行访问 Web 应用等操作,在网络追踪捕获数据包时触发网络通信问题。
  3. 查看 oc adm must-gather 从 pod 传送到客户端机器的数据包捕获文件:

    tmp/captures
    ├── event-filter.html
    ├── ip-10-0-192-217-ec2-internal  1
    │   └── registry-redhat-io-openshift4-network-tools-rhel8-sha256-bca...
    │       └── 2022-01-13T19:31:31.pcap
    ├── ip-10-0-201-178-ec2-internal  2
    │   └── registry-redhat-io-openshift4-network-tools-rhel8-sha256-bca...
    │       └── 2022-01-13T19:31:30.pcap
    ├── ip-...
    └── timestamp
    1 2
    数据包捕获保存在可识别主机名、容器和文件名的目录中。如果您没有指定 --node-selector 参数,则主机名的目录级别不存在。