个人开发的K8S日志查询工具,基本的功能都有,能满足日常使用。
git 项目地址:https://github.com/hxz5215/K8S_logsearch_Tools.git
工具免费无毒,欢迎使用,有好的建议可以留言,找时间慢慢优化!
使用上要注意:
服务名称这里:输入的是POD名称,如果你的K8S集群上的POD是多副本形式,就无法使用该工具了,目前只支持单副本形式的POD进行日志查询!
起始日期:日期筛选条件,是根据你日志里面的时间戳来进行日志截取的,所以如果你日志内容中没有时间戳的话,也用不了该筛选条件!
kubernetes 日志查询
附 K8S API SDK kubernetes.client.CoreV1Api 官方地址
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod_log
以下就是该程序中引用的调用K8S日志接口部分代码:
from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint
# Configure API key authorization: BearerToken
configuration = kubernetes.client.Configuration()
configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
# create an instance of the API class
api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(configuration))
name = 'name_example' # str | name of the Pod
namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
container = 'container_example' # str | The container for which to stream logs. Defaults to only container if there is one container in the pod. (optional)
follow = True # bool | Follow the log stream of the pod. Defaults to false. (optional)
limit_bytes = 56 # int | If set, the number of bytes to read from the server before terminating the log output. This may not display a complete final line of logging, and may return slightly more or slightly less than the specified limit. (optional)
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
previous = True # bool | Return previous terminated container logs. Defaults to false. (optional)
since_seconds = 56 # int | A relative time in seconds before the current time from which to show logs. If this value precedes the time a pod was started, only logs since the pod start will be returned. If this value is in the future, no logs will be returned. Only one of sinceSeconds or sinceTime may be specified. (optional)
tail_lines = 56 # int | If set, the number of lines from the end of the logs to show. If not specified, logs are shown from the creation of the container or sinceSeconds or sinceTime (optional)
timestamps = True # bool | If true, add an RFC3339 or RFC3339Nano timestamp at the beginning of every line of log output. Defaults to false. (optional)
try:
api_response = api_instance.read_namespaced_pod_log(name, namespace, container=container, follow=follow, limit_bytes=limit_bytes, pretty=pretty, previous=previous, since_seconds=since_seconds, tail_lines=tail_lines, timestamps=timestamps)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)