最近项目里在调试k8s探针,需要在集群的worker宿主网络上进行调试,但是很多集群往往只有kubeAPI的权限,没有node的SSH权限,调试起来非常不方便。

一种调试方法是启动一个调试工具容器并指定网络参数hostNetwork,这样可以在没有SSH权限的情况下使用node的宿主网络进行调试。之前都是根据需要的工具命令现写一个容器Dockerfile,偶然发现了一个已经集成了大部分常用的容器和网络调试工具的项目,开箱即用,很容易上手,就是今天分享的netshoot。

介绍

项目地址: https://github.com/nicolaka/netshoot

netshoot

使用

kubectl run 直接启动调试容器

kubectl run netshoot --rm -i --tty -n $namespace --overrides='{"spec": {"hostNetwork": true}}' --image nicolaka/netshoot

也可以写一个deployment来构建容器 适合长期调试

apiVersion: apps/v1
kind: Deployment
metadata:
  name: netshoot
  labels:
    app: netshoot
spec:
  replicas: 1
  selector:
    matchLabels:
      app: netshoot
  template:
    metadata:
      labels:
        app: netshoot
    spec:
      hostNetwork: true
      containers:
      - name: netshoot
        image: nicolaka/netshoot
        command: ["/bin/bash"]
        args: ["-c", "while true; do ping localhost; sleep 60;done"]
        resources:
          limits:
            cpu: 100m
            memory: 128Mi

更多

生产环境集群往往会有多个worker node,需要将调试容器指定到对应的node,node数量较少的时候可以增加replicas自动覆盖多个node,node数量较多的时候需要通过label/annotation结合nodeSelector来指定。

netshoot这个工具也提供了kubectl plugin, 也可以通过ephemeral container来附加到容器上进行调试