Logging 终止前在弹性beanstalk中保留不健康实例的日志
我刚刚遇到一个情况,我的ebs应用程序在周末检测到一个不健康的实例,作为响应,我将不健康的实例换成了一个新实例 这很好,我希望发生什么,但我已经意识到,可以告诉我为什么实例变得不健康的日志文件已被不健康实例删除 要在终止之前将不健康实例的日志文件保存到s3,需要执行什么操作?我是否需要启用一个设置,或者我是否需要自己编写一些代码来侦听ebs事件并将日志保存到s3(我认为这是一个非常常见的要求)Logging 终止前在弹性beanstalk中保留不健康实例的日志,logging,amazon-ec2,amazon-ebs,Logging,Amazon Ec2,Amazon Ebs,我刚刚遇到一个情况,我的ebs应用程序在周末检测到一个不健康的实例,作为响应,我将不健康的实例换成了一个新实例 这很好,我希望发生什么,但我已经意识到,可以告诉我为什么实例变得不健康的日志文件已被不健康实例删除 要在终止之前将不健康实例的日志文件保存到s3,需要执行什么操作?我是否需要启用一个设置,或者我是否需要自己编写一些代码来侦听ebs事件并将日志保存到s3(我认为这是一个非常常见的要求) 谢谢确定,所以在搜索之后,似乎没有快速设置,您可以在ec2/beanstalk上启用以保存日志(这有点
谢谢确定,所以在搜索之后,似乎没有快速设置,您可以在ec2/beanstalk上启用以保存日志(这有点令人失望) 可以选择使用第三方日志记录服务,如或,但我认为这对于我所需要的来说太过分了 所以在做了更多的挖掘之后,我终于通过在ec2框的/etc/init.d中添加一个脚本实现了我所需要的。我将在下面发布我所做的事情,但首先我想感谢hudku.com上的一系列文章,我从这些文章中借用了很多东西,以及aws论坛上的一系列文章,他们解释了如何在机器关闭时运行init.d脚本 基本上,您需要在应用程序war文件的根目录中创建一个名为“.ebextensions”的文件夹,然后将以下所有文件放在该文件夹中 elastic beanstalk.config(这是一个YAML文件,也是我从的帖子中借用的文件之一) app setup.sh(这是我从的帖子中借来的另一个文件,并为我的目的进行了修改) 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
copylogs.sh(这是执行复制的文件,用适当的值替换所有带角括号的变量)
#/bin/bash
输出=$(/opt/aws/bin/ec2元数据-i)
实例=${output:13}
s3put——地区eu-west-1-a-s-b-p/usr/share/tomcat7/logs/-k终止的_logs/$instance put/usr/share/tomcat7/logs/*
一旦部署了新的应用程序版本,您就可以通过ssh将其连接到ec2框,运行“sudo reboot”并检查s3存储桶中的日志文件来测试它是否有效
#!/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
#!/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
#!/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/*