将启动脚本日志推送到gcp中的单独文件 [英] Push startup-script logs to a separate file in gcp
问题描述
在GCP中,对于ubuntu-启动脚本日志会自动推送到/var/log/Syslog
中,如果需要很长时间,由于日志轮换,我们可能会丢失这些日志.有没有办法将这些日志重定向到另一个日志文件?我的启动脚本是一个简单的bash脚本,包含多个命令,并且无法将单个命令的输出重定向到文件.
In GCP, For ubuntu - startup-script logs automatically pushed to /var/log/Syslog
we might miss those logs due to log rotation if required after a long duration. Is there a way to redirect these logs to some another log-file?
My startup-script is a simple bash script with multiple commands and can't redirect the output of individual command to a file.
推荐答案
您可以考虑以下解决方案:
You can consider this solution:
- 将
startup-script
中的输出重定向到专用的/tmp
目录中的startup-script.log
文件 - 安装
stackdriver日志记录
代理 - 为您的
startup-script.log
添加特定配置
- redirect outputs inside your
startup-script
to a dedicatedstartup-script.log
file in/tmp
directory - install
stackdriver logging
agent - add a specific configuration for your
startup-script.log
然后,您将可以通过GCP Stackdriver Logging控制台(或通过 gcloud
命令)浏览日志.
Then you'll be able to browse your logs via GCP Stackdriver Logging console (or via gcloud
command).
GCP日志记录控制台的屏幕截图:
Screenshot of GCP Logging Console :
Stackdriver Logging将日志仅保留30天.在很长的保留期内,您可以轻松创建 sink
以将日志导出到BigQuery表或Cloud Storage存储桶.查看有关导出日志的官方文档:
Stackdriver Logging will keep logs only for 30 days.
For a long retention period, you can easily create a sink
to export logs to a BigQuery table or a Cloud Storage bucket.
Check official docs about exporting logs :
- https://cloud.google.com/logging/docs/basic-concepts#sinks
- https://cloud.google.com/logging/docs/export/configure_export_v2
示例 startup-script.sh
的完整代码:
Full code of a sample startup-script.sh
:
#! /bin/bash
# install gcp logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh
# setup a configuration for startup-script logs only
cat > /etc/google-fluentd/config.d/startup-script-log.conf <<- EOM
<source>
@type tail
# Format 'none' indicates the log is unstructured (text).
format none
# The path of the log file.
path /tmp/startup-script-log.log
# The path of the position file that records where in the log file
# we have processed already. This is useful when the agent
# restarts.
pos_file /var/lib/google-fluentd/pos/startup-script-log.pos
read_from_head true
# The log tag for this log input.
tag startup-script-log
</source>
EOM
# restart logging agent
sudo service google-fluentd restart
# redirect outputs to dedicated startup-script log
exec &>> /tmp/startup-script-log.log
# your startup-script content
# ...
echo "hello the world"
这篇关于将启动脚本日志推送到gcp中的单独文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!