AWS Elastic Beanstalk 에서 .ebextensions을 활용하여 scouter agent(host) 백그라우드 프로세스 기동하기

이번에 IBK창공기업과 AWS Elastic Beanstalk(이하 EB)을 이용하여 어플리케이션을 개발하고 유지보수 하는 프로젝트를 진행하게 되었습니다. 이 어플리케이션은 Spring Boot 기반에 백앤드 API 이었고 별도의 모니터링이 없는 상태에서 로그만 확인하고 있었습니다. 그래서 저희는 APM 오픈소스인 Scouter를 제안하였고, EB에 Scouter를 적용하는 과정을 설명하려고 합니다.

AWS Elastic Beanstalk

AWS에서는 사용 EC2 인스턴스를 많이 사용해야 돈버는 구조이므로 EB라는 서비스를 출시하였습니다. 단순히 말해 어플리케이션 제작후 손쉽게 서버를 생성하여 배포하는 서비스라고 생각하시면 됩니다.
image
< AWS BeanStalk 워크플로우 >
손쉽게 서버를 생성해야 EC2 인스턴가 자꾸자꾸 늘어나게 되니깐요. 아무튼 이런 구조에서 개발자는 서버 아키텍처에 대한 부담을 줄이면서 손쉽게 백앤드 어플리케이션을 구축 할 수 있습니다.

Scouter

오픈소스 APM인 Scouter는 JVM(WAS, Standalone application)을 사용하는 어플리케이션 및 OS 자원에 대한 모니터링 모니터링 기능을 제공합니다.
  • APM : Application performance montoring / application performance management
  • 모니터링 대상 (현재)
    • Java application - Web application (on Tomcat, JBoss, Resin ...), Standalone java application
    • OS - LInux, Windows, Unix
  • 모니터링 대상 (TOBE)
    • Redis, Apach HTTPD, nginX, Nodejs...
Screen
< Scouter 화면 예시 >
상세내용은 https://github.com/scouter-project/scouter/blob/master/README_kr.md 를 참고하기 바랍니다.

.ebextensions

웹 애플리케이션의 소스 코드에 AWS Elastic Beanstalk 구성 파일(.ebextensions)을 추가하여 환경을 구성하고 환경에 있는 AWS 리소스를 사용자 지정할 수 있습니다. 구성 파일은 .config 파일 확장명을 사용하는 YAML이나 JSON 형식 문서로, .ebextensions 폴더에 놓고 애플리케이션 원본 번들로 배포합니다. 이러면 어플리케이션이 기동시 Scouter를 설치하고 기동할 수 있습니다.

scouter 설치
Scouter 설치 전 반드시 기억해야할 사항
Scouter는 Agent와 Collector Server 그리고 User용 Client 프로그램으로 구성되며, 이들간의 관계를 잘 아는 것이 중요합니다.
  • 각 서버에 설치된 Scouter의 Agent들이 성능 데이터를 Collector로 전송한다.
  • 사용자는 Client 프로그램을 통해 성능 데이터를 본다.
image
scouter 설치하기:  http://gunsdevlog.blogspot.com/2017/07/scouter-apm-1.html 를 참고하기 바랍니다.


.ebextensions 으로 scouter  설치하기

처음에 설치를 위한 압축파일을 다운로드 하여 agent(host)를 기동하는 스크립트 작성하여 기동하였습니다.

sources:
  /usr/share: https://github.com/scouter-project/scouter/releases/download/v1.8.6/scouter-all-1.8.6.tar.gz
commands:
  scouter_auth:
    command: sudo chown -R tomcat:tomcat /usr/share/scouter
    ignoreErrors: true
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
  timezone_setting:
    command: sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime
files:
  /usr/share/scouter/agent.host/conf/scouter.conf:
    owner: tomcat
    group: tomcat
    content: "net_collector_ip=XXX.XXX.XXX.XXX"
  /usr/share/scouter/agent.java/conf/scouter.conf:
    owner: tomcat
    group: tomcat
    content: "net_collector_ip=XXX.XXX.XXX.XXX"
  /usr/share/scouter/agent.host/host.sh:
    mode: "000755"
    owner: tomcat
    group: tomcat
    content: |
      #!/bin/bash
      #PROCESS_NUM='ps -ef | grep "$1" | grep -v "grep" | wc -l'
      cd /usr/share/scouter/agent.host
      nohup java -classpath ./scouter.host.jar scouter.boot.Boot ./lib > nohup.out &

container_commands:
  scouter_host_start:
    command: sudo -u tomcat ./host.sh
    cwd: /usr/share/scouter/agent.host
    leader_only: true
    ignoreErrors: true

그리고 어플리케이션 다시 기동해보니 계속 Time-Out 발생되면서 어플리케이션 작동 되지 않았습니다. 그런 와중에 검색해보니 아래의 링클 발견
https://theholyjava.wordpress.com/2015/07/29/fixing-a-mysterious-ebextensions-command-time-out-aws-elastic-beanstalk/
stdout and/or stderr 를 /dev/null 로 리다이렉션 하면 된다고 해서 변경하였습니다.

....

  /usr/share/scouter/agent.host/host.sh:
    mode: "000755"
    owner: tomcat
    group: tomcat
    content: |
      #!/bin/bash
      #PROCESS_NUM='ps -ef | grep "$1" | grep -v "grep" | wc -l'
      cd /usr/share/scouter/agent.host
      nohup java -classpath ./scouter.host.jar scouter.boot.Boot ./lib > /dev/null 2>&1 & 

...
하지만, 그래도 계속 타임아웃이 발생 하였고, 좀더 검색을 해보니  아래의 링크를 발견하였습니다.
https://serverfault.com/questions/628518/job-control-background-process-using-ampersand-in-ebextensions-config-command
백그라우드 프로세스를 EB 기동단계에서 어플리케이션 기동후에 실행하면 된다고….
그래서 다음과 같이 수정하였습니다.

sources:
  /usr/share: https://github.com/scouter-project/scouter/releases/download/v1.8.6/scouter-all-1.8.6.tar.gz
commands:
  scouter_auth:
    command: sudo chown -R tomcat:tomcat /usr/share/scouter
    ignoreErrors: true
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
  timezone_setting:
    command: sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime
files:
  /usr/share/scouter/agent.host/conf/scouter.conf:
    owner: tomcat
    group: tomcat
    content: "net_collector_ip=XXX.XXX.XXX.XXX"
  /usr/share/scouter/agent.java/conf/scouter.conf:
    owner: tomcat
    group: tomcat
    content: "net_collector_ip=XXX.XXX.XXX.XXX"
  /opt/elasticbeanstalk/hooks/appdeploy/post/99_start_scouter_host.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash
      #PROCESS_NUM='ps -ef | grep "$1" | grep -v "grep" | wc -l'
      chown -R tomcat:tomcat /usr/share/scouter
      cd /usr/share/scouter/agent.host
      sudo -u tomcat nohup java -classpath ./scouter.host.jar scouter.boot.Boot ./lib >/dev/null 2>&1 &

이렇게 EB 어플리케이션 단계에서 실행파일을 추가로 작성하여 appdeploy 이후 실행하는 것으로 하니 정상적으로 기동되었습니다.

결론

AWS EB에서 .ebextensions으로 백그라우드 프로세스를  기동하려면,  첫번째로 stdout and/or stderr 를 /dev/null 리다이렉션하고 두번째로 appdeploy에서 post 디렉토리에 스크립를 추가하여 실행하면 기동할 수 있습니다. 끝.


* 참고자료
https://github.com/scouter-project/scouter/blob/master/README_kr.md
https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/Welcome.html
https://docs.aws.amazon.com/ko_kr/elasticbeanstalk/latest/dg/ebextensions.html
https://theholyjava.wordpress.com/2015/07/29/fixing-a-mysterious-ebextensions-command-time-out-aws-elastic-beanstalk/
https://serverfault.com/questions/628518/job-control-background-process-using-ampersand-in-ebextensions-config-command



댓글

주간 인기글

카드뉴스 마케팅 팁

[ubuntu] 신규 계정에 sudo 권한 추가하기

SPA(Sigle Page Applications) 란 무엇인가?

[AWS] WinSCP 를 이용해 Linux 인스턴스로 파일 전송하기

[Vue] 전화번호 입력/조회시 '-' 자동으로 넣어주기