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 view k8s log through shell script

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to view the K8s log through the shell script. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Check the log: kubectl logs-f podName-- tail 100

For example, if we want to check the specified pod, the specified number of lines, the specified content

You need to enter kubectl logs-f xxx-- tail yyy each time | grep zzz

For convenience, you can customize the script by typing sh .sh xxx yyy zzz, and xxx supports RE

Placeholder mode #! / bin/bash# kubectl get pods#notificationx= "kubectl logs-f" y = "--tail" g = "| grep" name= `kubectl get pods | grep ^ $1 | awk'{print $1}'`x = "eval $x $name $y $2$ g $3" ${x} # sh log.sh podName 20 content# finally: kubectl logs-f podName-- tail 20 | grep content specifies the parameter getoptsafter an option with a colon (":") It means that this option can be followed by the parameter x = "kubectl logs-f" y = "- tail" g = "| grep" while getopts ": n:f:c:" optdo case $opt in n) name= `kubectl get pods | grep ^ $OPTARG | awk'{print $1}'`x = "$x $name" ; f) x = "$x $y $OPTARG";; c) x = "$x $g $OPTARG";;?) Echo "unknown parameters" exit 1; esacdonex= "eval $x" ${x} # sh log.sh-n podName-f 20-c content# finally: kubectl logs-f podName-- tail 20 | grep content question 1. Execute shell script\ r problem

The script is edited under window and uploaded to linux for execution. The newline under win is carriage return + newline, that is,\ r\ n, while under unix is newline\ n. \ r is not recognized as a carriage return character under linux, so there is an extra\ r configuration for each line, so it is a script coding problem.

two。 Grep in the command

You can see that the resulting concatenated string is the correct command, but the variable error is reported through ${CMD}.

Reason:

If you define a command in shell with pipes, such as

CMD= "ls-l | grep xx"

If you execute $CMD directly, the following error will occur

Ls: cannot access |: No such file or directory

Ls: cannot access grep: No such file or directory

Pipe characters are interpreted as ordinary characters

Plus eval

CMD= "eval ls-l | grep xx"

The above is the editor for you to share how to view the k8s log through the shell script, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report