通过crictl运行一个容器sidecar进行debug

我在排查 coredns “context deadline exceeded” 监控采集异常时,想要进入容器内部验证 Metrics 是否已经启用。但是我发现有一个棘手的问题, CoreDNS 容器没有提供任何排查工具,没有提供 Shell Atlas ,就无法通过 kubectl exec 或者 nsenter 进入容器内部排查。

有一个思路是采用 crictlcrictl运行pod sandbox 功能,借鉴 How to get into CoreDNS pod kuberrnetes? (原文使用 Docker Atlas ):

  • 创建 container-config.json :

container-config.json 配置运行容器镜像
{
  "metadata": {
    "name": "alpine"
  },
  "image":{
    "image": "alpine"
  },
  "command": [
    "sh"
  ],
  "log_path":"alpine.log",
  "linux": {
  }
}
通过 crictl ps 获取coredns的容器ID
crictl ps

在输出中找出coredns对应 pod ID 是 47933c32ce14d :

通过 crictl ps 获取coredns的容器ID输出信息
CONTAINER           IMAGE               CREATED             STATE               NAME                      ATTEMPT             POD ID              POD
e06473dd8f0d1       5e3a0e9ab91a1       9 hours ago         Running             speaker                   37                  ed5621760e601       speaker-vlhld
7f4fb84d66dce       0c16e5f81be4b       9 hours ago         Running             master                    11                  b6ce34d7999e5       gpu-operator-1673526262-node-feature-discovery-master-6594glnlk
592bc16348f70       5185b96f0becf       9 hours ago         Running             coredns                   37                  23d008f4aaa1f       coredns-d669857b7-x6wlt
65b28faa3741e       526bd4754c9cd       9 hours ago         Running             cilium-agent              162                 aa9e0b0642dff       cilium-2x6gn
fa4c96d33f275       0da6a335fe135       9 hours ago         Running             node-exporter             33                  7e041042fae43       kube-prometheus-stack-1680871060-prometheus-node-exporter-dkqqs
0d517d735bddc       6d23ec0e8b87e       9 hours ago         Running             kube-scheduler            17                  5290816dcc423       kube-scheduler-z-k8s-m-2
56b89f3c82bf9       6039992312758       9 hours ago         Running             kube-controller-manager   17                  1db70ddeb87be       kube-controller-manager-z-k8s-m-2
04465ed654902       0346dbd74bcb9       9 hours ago         Running             kube-apiserver            39                  f6178f5243ea2       kube-apiserver-z-k8s-m-2
  • 我们需要在coredns的pod 47933c32ce14d 再运行一个用于调试的sidecar,所以构建一个 pod-config.json 来对应:

pod-config.json 配置运行coredns的pod
{
  "metadata": {
    "name": "coredns",
    "namespace": "kube-system",
    "attempt": 1,
    "uid": "47933c32ce14d"
  },
  "log_directory": "/tmp",
  "linux": {
  }
}
  • 运行起容器加入到现有的 coredns pods中:

通过 crictl run 在sandbox中运行新容器
crictl run container-config.json pod-config.json

警告

我这个思路还没实现成功,还需要进一步学习实践

我忽然想到,另一种方式是通过 kubectl edit 为pods增加一个 sidecar 或许是更为优雅的方法

参考