我的Pod在其名称后附加了一个动态生成的ID,例如my-app-name-7b587cd75b-dscsr
,该ID在每次部署中都不同(下次可能是my-app-name-xcgv83bfsd-4kjsf
)。
这使使用某些命令非常麻烦,因为每次需要查看日志时,我都必须先列出所有Pod,然后将更改后的名称复制粘贴到logs
命令中:kubectl -n [namespace] logs my-app-name-7b587cd75b-dscsr
。 / p>
有没有一种方法可以跳过吊舱名称或名称的一部分,并像kubectl -n [namespace] logs my-pod-name-~
命令那样执行kubectl -n [namespace] logs service/my-pod-name
或port-forward
之类的事情?
我试图将grep
插入logs
命令中以获取吊舱名称,并在单个命令中运行logs
,但是Windows上的Cmder确实如此,似乎支持$()
:kubectl -n [namespace] logs $(kubectl -n my-app-name get pod | grep my-app-name | sed 's/ .*//')
答案 0 :(得分:2)
为部署添加标签,并使用标签选择器从匹配的pod中查找日志。
请参阅以下说明
master $ kubectl run webapp --image=nginx --port=80 --labels="app=web"
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/webapp created
master $
master $ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
webapp 1/1 1 1 2m27s
master $
master $ kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webapp-647c6cd6f4-pxr4g 1/1 Running 0 20s 10.44.0.1 node01 <none> <none>
master $
master $ curl 10.44.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
master $
master $ kubectl logs -l app=web
10.32.0.1 - - [23/Jul/2019:10:07:39 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"
答案 1 :(得分:1)
您可以使用 deployment / $ DEPLOYMENT_NAME 来获取Pod的日志,而不是使用 POD / $ POD_NAME
kubectl logs deployment/$DEPLOY_NAME
# Return snapshot logs from container nginx-1 of a deployment named nginx
kubectl logs deployment/nginx -c nginx-1
kubectl logs --help
将提供更多信息