kubernetes之debug

kubernetes之debug

how to debug kubernetes

  • 直接访问pod
  • 部署自定制din的pod
  • kube port forward
  • kube proxy

直接访问pod


kubectl exec -n test -it {pod} -- /bin/bash

kubectl exec -n test -it {pod} --container {container} -- /bin/bash

部署自定制的pod

通常pod对应container的img只包含了运行必要的环境和工具,例如一些测试http的命令行工具curl并不一定在img里。

另外对于一些http接口,pod是cluster内部网络,通过service层暴露访问(eg NodePort, ClusterIP,Ingress, LB)。如果service 使用clusterIP类型,那么service只是cluster 内部可以访问。

新部署一个pod

  • 可以自己灵活安装工具
  • DNS servicename 环境变量会自动添加
  • 和pod,service同属于一个cluster 网络

例如一个curlpod(使用busyboxplus:curl 这个img,busybox基础上添加了curl)

apiVersion: v1
kind: Pod
metadata:
name: curlpod
spec:
containers:
- name: curlpod
image: radial/busyboxplus:curl
command:
- sh
- -c
- while true; do sleep 1; done

通过命令kubectl apply -f -n ??? curlpod.yaml

通过kubectl exec -it curlpod -- sh, 进到pod后可以执行curl命令

Port Forward

类似与ssh port forward

kubectl port-forward pods/{podname} {localport}:{remoteport}

KubeProxy

https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod

kubectl proxy --port=8999 -n ??

访问kubeAPI

curl http://localhost:8999/api/

访问service

// http
http://localhost:8999/api/v1/namespaces/namespace_name/services/service_name[:port_name]/proxy

//https
http://localhost:8999/api/v1/namespaces/namespace_name/services/https:service_name:[port_name]/proxy