Quantcast
Channel: Charsyam's Blog
Viewing all articles
Browse latest Browse all 122

[입 개발] 스타트업을 위한 AWS 로그 시스템 Part #2

$
0
0

1부에서는 CloudWatch Log Stream 에 데이터를 저장하는 것 까지 살펴보았습니다. 그런데 그렇게 로그를 보낸다고 하면 고민해야 할 부분들이 있습니다.

  • 로그 파일의 사이즈
    • /var/log/myservice/myservice.log 에 파일을 저장한다고 가정을 하겠습니다. 그러면 해당 파일에 계속 로그가 쌓이다 보면 어떤 문제가 발생하게 될까요?
    • 계속 데이터가 쌓이다보면, 결국 해당 디스크의 디스크 용량을 서비스 로그가 전부 디스크를 사용하게 되면, 해당 인스턴스에 문제가 발생하게 될 것입니다.

그렇다면 위의 문제를 기존에 beanstalk 에서는 어떻게 사용하고 있을까요? beanstalk의 기본 설정으로는 logrotate 를 사용하고 있습니다. logrotate 에 대한 기본 설명 자체는 아래 블로그가 잘 설명하고 있어서 이것으로 대체하도록 하겠습니다.

https://velog.io/@gillog/logrotate

해당 설정은 beanstalk를 사용하면 /etc/logrotate.elasticbeanstalk.hourly 를 보면 확인할 수 있습니다.

[ec2-user@ ~]$ cd /etc/logrotate.elasticbeanstalk.hourly/
[ec2-user@ logrotate.elasticbeanstalk.hourly]$ ll
total 32
-rw-r--r-- 1 root root 157 Feb 11 00:54 logrotate.elasticbeanstalk.eb-engine.conf
-rw-r--r-- 1 root root 156 Feb 11 00:54 logrotate.elasticbeanstalk.eb-hooks.conf
-rw-r--r-- 1 root root 171 Feb 15 11:28 logrotate.elasticbeanstalk.healthd.conf
-rw-r--r-- 1 root root 157 Feb 15 11:28 logrotate.elasticbeanstalk.nginx.conf
-rw-r--r-- 1 root root 156 Feb 11 00:54 logrotate.elasticbeanstalk.web-stderr.conf
-rw-r--r-- 1 root root 156 Feb 11 00:54 logrotate.elasticbeanstalk.web-stdout.conf

여기에 logrotate 필요한 로그에 대한 설정을 다음과 같이 추가해주면 됩니다.

/var/log/myservice/*.log {
 su root root
 size 1M
 rotate 5
 missingok
 compress
 notifempty
 copytruncate
 dateext
 dateformat %s
 olddir /var/log/rotated
}

이 때 해당 설정 파일의 umask 는 444 나 644 가 되어야 합니다. 위의 설정도 .ebextention의 files 섹션에 내용을 추가함으로써 생성할 수 있습니다.

  "/etc/logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.reqlog.conf" :
    mode: "000444"
    owner: root
    group: root
    content: |
      /var/log/lemontree-admin/*.log {
       su root root
       size 1M
       rotate 5
       missingok
       compress
       notifempty
       copytruncate
       dateext
       dateformat %s
       olddir /var/log/rotated 
      }

그럼 위의 설정만으로 과연 해당 로그가 logrotate가 되게 될까요? 위의 설정중에 olddir 을 보면 logrotate가 된 파일이 저장되게 됩니다. 위의 rotate 5 설정에 의해서 최고 5개만 남게 되죠.

/var/log/rotated 로 가보면 다음과 같이 파일 목록이 존재하게 됩니다. 파일 뒤에 붙는 이름의 형식은 위의 설정의 dateformat %s 를 사용하면 timestamp 로, 아니면 YYYYMMDD나 순서등으로 이름이 생성됩니다.

[ec2-user@ip-]$ logrotate.elasticbeanstalk.hourly]$ cd /var/log/rotated/
[ec2-user@ip rotated]$ ll
total 72
-rw-r--r-- 1 root   root   5444 Feb 11 02:42 eb-engine.log1644516061.gz
-rw-r--r-- 1 root   root   5678 Feb 12 01:36 eb-engine.log1644598861.gz
-rw-r--r-- 1 root   root   3888 Feb 12 13:46 eb-engine.log1644642061.gz
-rw-r--r-- 1 root   root   5664 Feb 12 14:25 eb-engine.log1644645661.gz
-rw-r--r-- 1 root   root   3899 Feb 15 11:28 eb-engine.log1644894061.gz
-rw------- 1 root   root   1851 Feb 16 20:31 web.stdout.log1645012861.gz
-rw------- 1 root   root   1843 Feb 16 21:57 web.stdout.log1645016462.gz
-rw------- 1 root   root   1693 Feb 16 22:44 web.stdout.log1645020061.gz
-rw------- 1 root   root   3905 Feb 16 23:43 web.stdout.log1645023661.gz
-rw------- 1 root   root   2076 Feb 17 00:55 web.stdout.log1645027261.gz

logrotate 를 사용할 경우, 만약에 해당 log 사이즈가 많고 지속적으로 데이터가 저장되고 있으면 logrotate가 발생하는 순간에 많은 부하가 발생할 수 있습니다. 이것은 logrotate의 동작 특성을 살펴보면 알 수 있는데 위의 설정에서 copytruncate 옵션을 주면 다음과 같이 처리되게 됩니다.

  • 먼저 logrotate 는 임시 파일을 생성합니다.
  • tmpfile의 이름을 필요한 형태로 변경합니다.
  • Myserver.log 의 내용을 Myserver.log.YYYYMMDD 로 복사합니다. 이 때 데이터의 양이 많으면 복사 작업 때문에 시스템 IO가 많이 발생하고 이때 서비스 timeout 과 로그 유실이 발생할 수 있습니다.
  • 최종적으로 Myserver.log 의 내용을 지우고 다시 해당 Myserver.log 에 로그를 쓰게 됩니다.

여기서 발생할 수 있는 부분에 대해서는 아래 블로그를 참고하면 될듯합니다.

https://brunch.co.kr/@alden/27 – 참고하고 보니, 갓앨든(강진우) 님의 브런치네요. 사랑해요 앨든!!!

그런데 이렇게 logrotate 설정만으로는 logrotate가 되지 않습니다. 실제로 해당 logrotate를 매 시간마다 실행시켜 주는 것은 cron 에 의해서 동작하고 있습니다. 그래서 실제로 cron 작업을 추가하는 마지막 작업이 있어야 합니다.

이것은 /etc/cron.hourly/ 를 살펴보면 쉽게 찾을 수 있습니다.

[ec2-user@ip- rotated]$ cd /etc/cron.hourly/
[ec2-user@ip- cron.hourly]$ ll
total 40
-rwxr-xr-x 1 root root 392 Jan 16  2020 0anacron
-rwxr-xr-x 1 root root 121 Feb 15 11:28 cron.logcleanup.elasticbeanstalk.healthd-proxy.conf
-rwxr-xr-x 1 root root 152 Feb 15 11:28 cron.logrotate.elasticbeanstalk.eb-engine.conf
-rwxr-xr-x 1 root root 151 Feb 15 11:28 cron.logrotate.elasticbeanstalk.eb-hooks.conf
-rwxr-xr-x 1 root root 150 Feb 15 11:28 cron.logrotate.elasticbeanstalk.healthd.conf
-rwxr-xr-x 1 root root 148 Feb 15 11:28 cron.logrotate.elasticbeanstalk.nginx.conf
-rwxr-xr-x 1 root root 149 Feb 12 18:07 cron.logrotate.elasticbeanstalk.reqlog.conf.bak
-rwxr-xr-x 1 root root 153 Feb 15 11:28 cron.logrotate.elasticbeanstalk.web-stderr.conf
-rwxr-xr-x 1 root root 153 Feb 15 11:28 cron.logrotate.elasticbeanstalk.web-stdout.conf

cron 설정을 보시면 다음과 같이 내용이 들어가 있습니다.

#!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate -f /etc/logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.web-stdout.conf

이 해당 설정도 이제 files 섹션에 추가해주면 logrotate 설정이 완료하게 됩니다.

  "/etc/cron.hourly/cron.logrotate.elasticbeanstalk.myservice.conf" :
    mode: "000444"
    owner: root
    group: root
    content: |
      #!/bin/sh
      test -x /usr/sbin/logrotate || exit 0
      /usr/sbin/logrotate -f /etc/logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.myservice.conf

이제 이것으로 기본적으로 서버의 설정은 끝났습니다.

  • 1부에서는 로그의 수집
  • 2부에서는 서버 안정성을 위한 logrotate 설정을 살펴보았습니다.

그런데 과연 이것만으로 다 된것일까요?

log의 retention 설정이 중요한데, cloudwatch 에 있는 log group의 retention 은 저희가 설정하기 나름입니다. 1일에서 영구적으로도 가능합니다. 해당 로그는 logs insight를 통해서 살펴 볼 수 있습니다. 다만 이 retention 기간이 지나면 없어지기 때문에 영구적으로 설정하거나, 잘 백업을 해둬야 합니다.

사실 로그는 이렇게 확인을 하기 위해서도 저장하지만, 분석한 필요한 정보들을 포함하고 있기 때문에, 분석을 위해서 이를 저장할 필요가 있습니다. AWS에서 가장 쉽게 사용하는 분석툴은 athena 나 EMR을 이용해서 분석을 해야 할텐데요. 결국 이를 위해서는 cloudwatch log stream에 있는 로그들을 s3에 적절한 형태로 저장할 필요가 있습니다.

3부에서는 이 부분에 대해서 다뤄보도록 하겠습니다.


Viewing all articles
Browse latest Browse all 122

Trending Articles