gli*_*ter 5 logging amazon-ec2 amazon-ebs
我刚刚遇到过这样一种情况,我的ebs应用程序在周末检测到一个不正常的实例,并作为响应将不健康的实例替换为一个新的实例。
很好,我想发生什么,但我已经意识到可以告诉我实例变得不正常的日志文件已被不正常的实例删除。
在终止之前,需要采取什么措施将不正常实例的日志文件保存到s3?是否需要启用一个设置,还是我必须自己编写一些代码来侦听ebs事件并将日志保存到s3(会以为这是很普遍的要求)?
谢谢
好吧,在搜索之后,似乎没有一个可以在 ec2/beanstalk 上启用的快速设置来保存日志(这有点令人失望)。
可以选择使用第三方日志记录服务,例如papertrail或loggly,但我认为这对于我的需要来说太过分了。
因此,经过更多的挖掘后,我终于通过向 ec2 盒子上的 /etc/init.d 添加脚本来实现我所需要的。我将在下面准确地发布我所做的事情,但首先要归功于hudku.com 上的Arun Kumar系列帖子,我从中借用了很多内容,以及aws 论坛上的akhtet6 ,他解释了如何将 init.d 脚本获取到在机器关闭时运行。
基本上,您需要在应用程序的 war 文件的根目录中创建一个名为“.ebextensions”的文件夹,然后将以下所有文件放入该文件夹中。
elastic-beanstalk.config (这是一个 YAML 文件,也是我从Arun Kumar的帖子中借用的文件之一)
# Errors get logged to /var/log/cfn-init.log. See Also /var/log/eb-tools.log
container_commands:
01-command:
command: rm -rf /custom/.ebextensions
02-command:
command: mkdir -p /custom/.ebextensions
03-command:
command: cp -R .ebextensions/* /custom/.ebextensions/
04-command:
command: chmod 700 /custom/.ebextensions/app-setup.sh
05-command:
command: bash /custom/.ebextensions/app-setup.sh
Run Code Online (Sandbox Code Playgroud)
app-setup.sh (这是我从Arun Kumar的帖子中借来的另一个文件,并根据我的目的进行了修改)
#!/bin/bash
# Set DEBUG to 1 to debug this script. 2 for debugging scripts called by this script and so on.
# Execute "export DEBUG=1" to debug this script.
# Set value to 2 to debug this script and the scripts called within this script.
# Set value to 3,4,5 and so on to increase the nesting level of the scripts to be debugged.
[[ $DEBUG -gt 0 ]] && set -x; export DEBUG=$(($DEBUG - 1))
# Check if this is the very first time that this script is running
if ([ ! -f /root/.not-a-new-instance.txt ]) then
newEC2Instance=true
fi
# Get the directory of 'this' script
dirCurScript=$(dirname "${BASH_SOURCE[0]}")
# Redirect stdout and stderr and append it to our file
curDateTime=$(date "+%Y%m%d%H%M%S")
exec &>> /usr/share/tomcat7/logs/customise-setup-log-$curDateTime.txt
echo $(date)
echo "Setting up app"
pwd
# Set permissions
chmod 777 /custom/.ebextensions/*
ls -al /custom/.ebextensions
# Set-up saveLogsToS3 service in appropriate run levels
cp /custom/.ebextensions/saveLogsToS3 /etc/init.d/
touch /var/lock/subsys/saveLogsToS3
/sbin/chkconfig saveLogsToS3 --level 12345 on
/sbin/chkconfig saveLogsToS3 --level 06 off
# If new instance, now it is not new anymore
if ([ $newEC2Instance ]) then
echo -n "" > /root/.not-a-new-instance.txt
fi
# Print the finish time of this script
echo $(date)
# Always successful exit so that beanstalk does not stop creating the environment
exit 0
Run Code Online (Sandbox Code Playgroud)
saveLogsToS3这是 init.d 脚本
#!/bin/sh
#
# chkconfig: 0123456 99 01
# description:
#
RETVAL=0
start () {
touch /custom/.ebextensions/saveLogsToS3_START
touch /var/lock/subsys/saveLogsToS3
}
stop () {
touch /custom/.ebextensions/saveLogsToS3_STOP
/custom/.ebextensions/copylogs.sh > /custom/.ebextensions/saveLogsToS3_STOP.log
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
start
;;
*)
start
;;
esac
exit $RETVAL
Run Code Online (Sandbox Code Playgroud)
copylogs.sh(这是执行复制的文件,将所有尖括号内的变量替换为适当的值)
#!/bin/bash
output=$(/opt/aws/bin/ec2-metadata -i)
instance=${output:13}
s3put --region eu-west-1 -a <Access_Key> -s <Secret_Key> -b <s3_bucket> -p /usr/share/tomcat7/logs/ -k Terminated_logs/$instance put /usr/share/tomcat7/logs/*
Run Code Online (Sandbox Code Playgroud)
部署新的应用程序版本后,您可以通过 ssh 连接到 ec2 盒子并运行“sudo restart”并检查 s3 存储桶中的日志文件来测试它是否正常工作。
| 归档时间: |
|
| 查看次数: |
626 次 |
| 最近记录: |