如何通过Kubernetes REST API获取多容器Pod的日志?
Kubernetes多容器Pod日志查询的正确API调用方式
当Pod包含多个容器时,Kubernetes REST API要求通过查询参数指定目标容器,而非修改URL路径结构,这就是你之前调用出错的原因。
正确调用格式
在原请求基础上,添加container查询参数指定容器名即可:
- 查询nginx容器日志:
curl -k -H "Authorization: Bearer my-super-secret-token" https://kubernetes/api/v1/namespaces/default/pods/my-app-1/log?container=nginx
- 查询php-fpm容器日志:
curl -k -H "Authorization: Bearer my-super-secret-token" https://kubernetes/api/v1/namespaces/default/pods/my-app-1/log?container=php-fpm
实用扩展参数
如果需要实时跟踪日志(对应kubectl logs -f的效果),可以追加follow=true参数:
curl -k -H "Authorization: Bearer my-super-secret-token" https://kubernetes/api/v1/namespaces/default/pods/my-app-1/log?container=nginx&follow=true
内容的提问来源于stack exchange,提问作者Joel
相关产品推荐
相关产品推荐

