Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Kubernetes structured Log

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use Kubernetes structured logs". In daily operation, I believe many people have doubts about how to use Kubernetes structured logs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how to use Kubernetes structured logs. Next, please follow the editor to study!

Logging is a basic aspect of observability and a key tool for debugging. But Kubernetes logs are traditionally unstructured strings, which makes any automatic parsing difficult, and any downstream processing, analysis, or query difficult to complete reliably.

In Kubernetes 1.19, we added support for structured logging, which natively supports (key, value) pairs and object references. We have also updated a number of log calls so that more than 99% of the log volume in a typical deployment is now migrated to a structured format.

To maintain backward compatibility, the structured log is still output as a string that contains a representation of the "key" = "value" pair. Starting with alpha of1.19, logs can also be output in JSON format using the-- logging-format=json flag.

Use structured logs

We added two new methods to the klog library: InfoS and ErrorS. For example, a call to the following information:

Klog.InfoS ("Pod status updated", "pod", klog.KObj (pod), "status", status)

Will result in the following logs:

I1025 00 kube-system/kubedns 15 controller_utils.go:116 15.525108 1 controller_utils.go:116] "Pod status updated" pod= "status=" ready "

Or, if the-- logging-format=json flag is set, the output is as follows:

{"ts": 1580306777.04728, "msg": "Pod status updated", "pod": {"name": "coredns", "namespace": "kube-system"}, "status": "ready"}

This means that downstream logging tools can easily get structured log data instead of using regular expressions (regex) to parse unstructured strings. This also makes it easier to process logs, more robust to query logs, and faster to analyze logs.

With structured logging, all references to Kubernetes objects are structured in the same way, so you can filter output and log entries that reference a particular pod. You can also find logs that tell the scheduler how to schedule pod, how to create pod, health probes for pod, and all other changes in the pod lifecycle.

Suppose you are debugging a problem with pod. With structured logs, you can filter out log entries that refer only to the pod you are interested in, without scanning possibly thousands of log lines to find related logs.

Structured logs are not only more useful when debugging problems manually, but also support richer features, such as automatic pattern recognition in logs or closer correlation between logs and trace data.

Finally, structured logs can help reduce log storage costs, because most storage systems are more efficient in compressing structured key=value data than unstructured strings.

At this point, the study on "how to use Kubernetes structured logs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report