리눅스 환경에서 서비스에 사용되는 여러 프로세스들을 모니터링 하기 위해 supervisor를 적용했다
적용과정을 공유하겠다.
1. supervisor 란?
supervisor는 여러 프로세스를 모니터링-관리하며 데몬 형식으로 지속적으로 동작해야 하는 프로세스가 있을 경우 유용하게 사용할 수 있다
데몬은 서버(시스템)이 살아있는 동안 항상 실행되어야 하는 백그라운드 프로세스이다.
Supervisor 란?
- supervisor가 프로세스에 대해 지속적으로 모니터링 하고, 프로세스가 예기치 않게 종료되어도 재구동하며 지속적으로 동작하도록 돕는 도구이다.
- 또한 여러 프로세스들을 통합해서 관리할 수 있다는 장점도 있다.
사용자에게 지속적으로 제공해야 하는 여러개의 프로세스(서비스)가 있는데, 사람이 직접 프로세스가 구동되고 있는지 지속적으로 확인하는 것은 쉽지 않다. 이 역할을 supervisor가 하게된다.
2. supervisor 설치
supervisor는 파이썬으로 만들어진 제품이라 python 2.7이상 또는 python3.4 이상이 필요하다.
설치 명령어
apt install supervisor # apt
yum install supervisor # yum
pip install supervisor # pip
pip으로도 설치를 할 수 있지만, supervisor는 시스템의 프로세스 관리 매니저로 관리해야 하므로 apt, yum과 같은 os 패키지 관리자로 설치하는 것을 권장한다.
필자는 폐쇄망에서 설치를 진행해야 했다.
https://rpmfind.net/linux/rpm2html/search.php?query=supervisor&submit=Search+...
RPM resource supervisor
rpmfind.net
위 사이트에서 supervisor-3.4.0-1.el7.noarch.rpm 을 설치한 다음 리눅스 서버로 파일 전송하여 서버에 설치를 진행했다.
rpm -Uvh [PATH]/supervisor-3.4.0-1.el7.noarch.rpm # rpm 패키지 업데이트 - 설치
rpm -qi supervisor # 설치 확인
3. supervisor 설정(모니터링 대상 설정)
supervisor 설치가 끝나면 supervisor 관련 설정을 수정하고, 모니터링 할 프로세스를 작성해줘야 한다.
supervisor 설치가 완료되면 /etc/supervisord.conf 파일을 수정하면 된다.
없으면 원하는 경로에 작성해주면 된다.
supervisord.conf
[supervisord]
logfile = [PATH]/supervisord.log ; 로그파일 설정
user = "user 계정"
pidfile = [PATH]/supervisord.pid
[inet_http_server]
port=*:9001
[supervisorctl]
serverurl=http://localhost:9001
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[program: web1] # 프로그램 섹션 정의(supervisor로 관리할 프로세스 섹션 추가)
command=/usr/bin/java -jar /PATH/ndcs.web-1.0.Jar ; 프로세스 실행 명령어(절대경로]
process_name=%(program_name)s ; 프로세스의 이름 지정
numprocs=1 ; 프로세스 시작 개수
directory=[PATH] ;프로세스 실행 전에 작업 디렉토리로 이동하는 경로 지정
autostart=true ; Suervisord가 시작될 때 프로그램을 자동으로 시작하도록 설정
autorestart=true ; 프로그램이 예기치 않게 종료되면 자동으로 재시작하도록 설정
startsecs=10 ; 프로세스 실행 후 최소 정상 작동 시간(초) 설정
startretries=3 ; 프로세스의 시도 횟수 설정
exitcodes=0,2 ; 프로세스 종료 코드 중에서 예상되는종료 코드를 지정. (0: 정상종료, 2: 예상되는 종료 코드)
stopsignal=QUIT ; 프로세스 종료할 때 사용할 시그널 설정
stopwaitsecs=10 ; 프로세스 stop 할 때 기다리는 최대 시간(초) 설
설정과 관련된 내용은 아래 문서에서 확인할 수 있다.
http://supervisord.org/configuration.html
Configuration File — Supervisor 4.2.5 documentation
The command that will be run when this program is started. The command can be either absolute (e.g. /path/to/programname) or relative (e.g. programname). If it is relative, the supervisord’s environment $PATH will be searched for the executable. Programs
supervisord.org
4. supervisor 서비스 구동
supervisor을 system 데몬으로 실행시키기 위해 서비스에 등록하겠다.
system 데몬은 운영체제에서 백그라운드로 실행되는 프로세스를 뜻하며, 서버 부팅시 자동으로 실행된다.
supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /PATH/supervisord.conf # supervisord 실행 스크립트
[Install]
WantedBy=multi-user.target
supervisord.service를 작성하고, 이제 system 데몬으로 등록해줘야 한다.
sudo systemctl enable superviosrd.service # system 데몬 등록
sudo systemctl status superviosrd.service # system 데몬 상태 확인
sudo systemctl start superviosrd.service # system 데몬 시작
sudo systemctl stop superviosrd.service # system 데몬 종료
system 데몬 관련된 내용은 아래 사이트를 참고.
https://www.lesstif.com/system-admin/systemd-system-daemon-systemctl-24445064.html
https://victorydntmd.tistory.com/215
5. 적용 결과
http://hostname:9001 을 입력하면 supervisord.conf에 등록되어 있는 프로세스 목록과 상태들을 브라우저에서 관리할 수 있다.
6. supervisorctl 명령어 정리
shell에서도 supervisor 관련 명령어로 프로세스 상태를 확인하고 모니터링이 가능하다.
supervisorctl -s http://localhost:9001 status # 모든 프로세스 보기
supervisorctl -s http://localhost:9001 start <프로세스명> # 해당 프로세스 시작
supervisorctl -s http://localhost:9001 stop <프로세스명> # 해당 프로세스 중지
supervisorctl -s http://localhost:9001 restart <프로세스명> # 해당 프로세스 재시작
supervisorctl -s http://localhost:9001 reload # supervisord 재시작
supervisorctl -s http://localhost:9001 reread # supervisord.conf 설정 파일 다시 읽기
supervisorctl -s http://localhost:9001 update # 변경된 설정내용 적용(reread 한 설정파일 적용)
'DevOps > Linux' 카테고리의 다른 글
[Linux] yum repository 에러 해결 (1) | 2024.07.26 |
---|---|
[Linux] SFTP로 파일 전송하기 (0) | 2024.01.02 |
[Linux] nohub과 &(앰퍼샌드) 사용법과 이해 (0) | 2023.09.16 |
[Linux] 리눅스에서 JAVA 애플리케이션 실행하기 (0) | 2023.09.16 |
[Linux] crontab과 스케줄링 (0) | 2023.09.15 |