2015-04-27 28 views
2

我剛剛遇到了一個情況,那就是我的ebs應用在週末檢測到不健康的實例,並作爲響應將新健康實例更換爲新健康實例。在終止之前在彈性beanstalk中保存不健康實例的日誌

這很好,我想要發生,但我已經意識到日誌文件,告訴我爲什麼實例變得不健康已被刪除不健康的實例。

需要說明的是,在終止之前將不健康實例的日誌文件保存到s3中需要說明什麼?有沒有需要啓用的設置,還是我必須自己編寫一些代碼才能收聽ebs事件並將日誌保存到s3(會認爲這是相當常見的要求)?

感謝

回答

1

好了,所以周圍搜索後似乎沒有成爲一個快速設置您可以啓用EC2 /豆莖保存日誌(這是一個有點令人失望)。

可以選擇使用第三方日誌記錄服務,如papertrailloggly,但我認爲這對我所需要的是過度的。

因此,在做了更多的挖掘之後,我終於設法通過在ec2框上添加一個腳本到/etc/init.d來實現我所需要的。我將發佈我正在做的下面的工作,但首先我想給予我在hudku.com上發佈的Arun Kumar系列文章的更多信息,akhtet6在aws論壇上解釋瞭如何讓init.d腳本運行在機器關機。

基本上你需要在應用程序的war文件的根目錄下創建一個名爲'.ebextensions'的文件夾,然後將所有下列文件放在該文件夾中。

彈性beanstalk.config(這是一個YAML文件和一個I從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這是在it.d腳本

#!/bin/sh 
# 
# chkconfig:
# 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'ing到ec2框並運行'sudo reboot'並檢查s3存儲桶查看日誌文件來測試它的工作原理。

相關問題