logging - 在终止前将不健康实例的日志保留在 Elastic beanstalk 中

标签 logging amazon-ec2 amazon-ebs

我刚刚遇到过一种情况,我的 ebs 应用程序在周末检测到一个不健康的实例,并作为响应将不健康的实例交换为新实例。

这很好,也是我想要发生的事情,但我已经意识到,可以告诉我为什么实例变得不健康的日志文件已随不健康的实例一起删除。

需要采取什么措施才能在终止之前将不健康实例的日志文件保存到 s3?是否需要启用某个设置,或者我是否必须自己编写一些代码来监听 ebs 事件并将日志保存到 s3(本以为这是一个很常见的要求)?

谢谢

最佳答案

好吧,在搜索之后似乎没有一个可以在 ec2/beanstalk 上启用来保存日志的快速设置(这有点令人失望)。

可以选择使用第三方日志记录服务,例如 papertrailloggly,但我认为这对于我的需要来说太过分了。

经过更多的挖掘,我终于通过向 ec2 盒子上的/etc/init.d 添加一个脚本来实现我所需要的。我将在下面准确地发布我所做的事情,但首先只想感谢 hudku.com 上的 Arun Kumar 系列帖子,我从中借用了很多内容,以及 aws 论坛上的 akhtet6,他解释了如何运行 init.d 脚 native 器关闭时。

基本上,您需要在应用程序的 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

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

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

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/*

部署新应用程序版本后,您可以通过 ssh 连接到 ec2 盒子并运行“sudo restart”并检查 s3 存储桶中的日志文件来测试它是否正常工作。

关于logging - 在终止前将不健康实例的日志保留在 Elastic beanstalk 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891735/

相关文章:

logging - 如何更改 IntelliJ IDEA 日志记录级别?

python - 从 Celery 任务登录到 Sentry

Linux AWS 立即被 rm -rf/* 关闭

amazon-s3 - EC2 - 拍摄 EBS 快照,保存到 S3,然后从 S3 启动实例

python : How can I test that my error has been logged on sentry?

grails - Jboss 7 Blocks日志上的Grails项目

angularjs - ExpressJS/NodeJS - 反向代理部署

amazon-web-services - 如何在 Elastic Beanstalk 服务器上模拟发送周期性任务?

amazon-ec2 - 我应该如何处理我的 EC2 应急计划?

amazon-web-services - 拍摄 EBS 卷的快照会提高可靠性吗?