-
Notifications
You must be signed in to change notification settings - Fork 5
BE 모니터링
sunghyun-0 edited this page Oct 15, 2024
·
7 revisions

현재 서버 로그를 확인하기 위해서는 ssh 접속을 통해 ec2 내부의 로그 파일에 접근해야 한다.
그리고 우테코 외부에서는 보안 그룹 설정으로 인해 ec2에 접속할 수 없기 때문에 외부에서도 원활하게 확인할 수 있는 방안을 구축해야 했다.
따라서 저희는 로그 확인을 비롯한 각종 서버의 문제점들을 확인할 수 있는 모니터링 도구를 도입하였다.
방끗 팀이 사용하기 전 도입을 고려한 것은 cloudWatch와 프로메테우스, Loki, 그라파나 등이 있다.
최종적으로 CloudWatch를 도입하기로 결정하였고 이유는 다음과 같다.
- 기존의 모든 리소스가 aws에 있기 때문에 설정의 연동성이 좋다.
- 러닝 커브가 낮은 기술을 택하였다.
- 가장 기본적인 수준의 모니터링을 하기로 결정했기 때문에 부가적인 서버 구축 혹은 기술이 필요하지 않았다.
- 기본적으로 CloudWatch가 제공하는 것을 사용했다.
- CPU 사용량
- Network I/O
- 추가적으로 필요한 정보의 경우, EC2 내부의 CloudWatch agent를 설치하여 연동하였다.
- 메모리 사용량
- 디스트 사용량
- 로그
- CloudWatch agent 설치 과정
// agent 설치
wget https://amazoncloudwatch-agent.s3.amazonaws.com/ubuntu/arm64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
// config wizard 실행
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
-
CloudWatch agent 설정 과정
================================================================ = Welcome to the Amazon CloudWatch Agent Configuration Manager = = = = CloudWatch Agent allows you to collect metrics and logs from = = your host and send them to CloudWatch. Additional CloudWatch = = charges may apply. = ================================================================ // 우리의 서버가 linux 환경에서 실행되기 때문에 agent 또한 linux 환경으로 맞춰 주었다 On which OS are you planning to use the agent? 1. linux 2. windows 3. darwin default choice: [1]: Trying to fetch the default region based on ec2 metadata... I! imds retry client will retry 1 times // 서버가 위치하는 곳에 대한 질문이다 // 우리의 경우 EC2 서버를 사용하고 있다 Are you using EC2 or On-Premises hosts? 1. EC2 2. On-Premises default choice: [1]: 1 // agent의 권한에 관련된 질문이다 cwagent의 경우, cloudwatch에 관련된 권한만 받는 것이다 // 우리는 cwagent를 선택하였지만 일부 권한이 부족하여 root 권한으로 변경하였다 Which user are you planning to run the agent? 1. cwagent 2. root 3. others default choice: [1]: 2 // StatsD 프로토콜과 함께 CloudWatch 에이전트를 사용하여 애플리케이션 // 또는 서비스에서 추가 사용자 지정 지표를 검색할 수 있다 // 유용한 기능이라 판단하여 도입하였다 Do you want to turn on StatsD daemon? 1. yes 2. no default choice: [1]: 1 // StatsD 프로토콜이 사용할 포트 번호를 지정하는 부분이다 // 디폴드로 제안한 것을 사용하였다 Which port do you want StatsD daemon to listen to? default choice: [8125] // 사용자 지정 지표를 수집하는 주기를 선택한다 // 우리는 제안해 주는 10초를 사용하였다 What is the collect interval for StatsD daemon? 1. 10s 2. 30s 3. 60s default choice: [1]: 1 // 메트릭 데이터를 집계하여 백엔드로 보내는 주기를 ****선택한다 **//** 제안하는 60초를 사용하였다 What is the aggregation interval for metrics collected by StatsD daemon? 1. Do not aggregate 2. 10s 3. 30s 4. 60s default choice: [4]: // CollectD 프로토콜을 사용해 시스템 레벨의 메트릭을 모니터링할지 여부를 묻는 것이다 // 우리는 StasD를 이미 사용하기로 결정하였고 추가적인 다운로드를 하지 않기 위해 No를 선택하였다 Do you want to monitor metrics from CollectD? WARNING: CollectD must be installed or the Agent will fail to start 1. yes 2. no default choice: [1]: 2 // 해당 메모리, 디스크 부분은 필수적으로 모니터링 하기 위해 yes를 선택하였다 Do you want to monitor any host metrics? e.g. CPU, memory, etc. 1. yes 2. no default choice: [1]: 1 // 코어 단위로 Cpu metric을 보는 것에 대한 부분인데 Cpu는 중요한 부분이니 선택했다 Do you want to monitor cpu metrics per core? 1. yes 2. no default choice: [1]: 1 //EC2 인스턴스에 대한 메트릭을 상세하게 모니터링하기 위해 ec2 차원을 추가했다 Do you want to add ec2 dimensions (ImageId, InstanceId, InstanceType, AutoScalingGroupName) into all of your metrics if the info is available? 1. yes 2. no default choice: [1]: 1 //인스턴스 id를 기준으로 각 인스턴스의 개별적으로 집계하여 조회하기 위해 선택했다 Do you want to aggregate ec2 dimensions (InstanceId)? 1. yes 2. no default choice: [1]: 1 // 메트릭 데이터를 얼마나 자주 기록할지에 대한 질문이다 // 메트릭 수집을 너무 짧은 단위로 할 필요는 없을 것 같아 60초로 설정했다 Would you like to collect your metrics at high resolution (sub-minute resolution)? This enables sub-minute resolution for all metrics, but you can customize for specific metrics in the output json file. 1. 1s 2. 10s 3. 30s 4. 60s default choice: [4]: // 시스템의 기본 메트릭 정보 이외에 디스크 사용량 등의 정보를 포함하기 위해 Standard를 선택했다 Which default metrics config do you want? 1. Basic 2. Standard 3. Advanced 4. None default choice: [1]: 2 Current config as follows: { "agent": { "metrics_collection_interval": 60, "run_as_user": "cwagent" }, "metrics": { "aggregation_dimensions": [ [ "InstanceId" ] ], "append_dimensions": { "AutoScalingGroupName": "${aws:AutoScalingGroupName}", "ImageId": "${aws:ImageId}", "InstanceId": "${aws:InstanceId}", "InstanceType": "${aws:InstanceType}" }, "metrics_collected": { "cpu": { "measurement": [ "cpu_usage_idle", "cpu_usage_iowait", "cpu_usage_user", "cpu_usage_system" ], "metrics_collection_interval": 60, "resources": [ "*" ], "totalcpu": false }, "disk": { "measurement": [ "used_percent", "inodes_free" ], "metrics_collection_interval": 60, "resources": [ "*" ] }, "diskio": { "measurement": [ "io_time" ], "metrics_collection_interval": 60, "resources": [ "*" ] }, "mem": { "measurement": [ "mem_used_percent" ], "metrics_collection_interval": 60 }, "statsd": { "metrics_aggregation_interval": 60, "metrics_collection_interval": 10, "service_address": ":8125" }, "swap": { "measurement": [ "swap_used_percent" ], "metrics_collection_interval": 60 } } } } Are you satisfied with the above config? Note: it can be manually customized after the wizard completes to add additional items. 1. yes 2. no default choice: [1]: 1 Do you have any existing CloudWatch Log Agent (http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html) configuration file to import for migration? 1. yes 2. no default choice: [2]: 2 //모니터링할 로그 파일이 있는지 설정하는 질문이다 // 기존의 서버 로그를 기록하는 파일이 있기 때문에 선택했다 Do you want to monitor any log files? 1. yes 2. no default choice: [1]: 1 //우선 default로 설정해주었다 //추후에 server.log 파일을 모니터링 하도록 수정하였다 Log file path: /home/ubuntu/actions-runner/server.log Log group name: default choice: [cloudwatch.log] 2024-bang-ggood-prod1 // 로그 그룹에서 기본 저장 옵션과 덜 자주 조회되는 로그 데이터를 저장하기 위한 옵션을 선택한다 // 기간을 밑에서 비교적 짧게 설정할 예정이기 때문에 // 저장 공간 절약이 필요가 없어 기본 저장 옵션으로 선택하였다 Log group class: 1. STANDARD 2. INFREQUENT_ACCESS default choice: [1]: // 로그 이벤트의 연속적인 스트림 이것을 그룹으로 묶은 것이 로그 그룹이다 Log stream name: default choice: [{instance_id}] server-log //로그를 몇일동안 저장하고 삭제할건지에 대해 90일로 설정했다 // -1의 경우, 영구 저장이다 Log Group Retention in days 1. -1 2. 1 3. 3 4. 5 5. 7 6. 14 7. 30 8. 60 9. 90 10. 120 11. 150 12. 180 13. 365 14. 400 15. 545 16. 731 17. 1096 18. 1827 19. 2192 20. 2557 21. 2922 22. 3288 23. 3653 default choice: [1]: 9 Do you want to specify any additional log files to monitor? 1. yes 2. no default choice: [1]: 2 //CloudWatch Agent에 X-ray 추적 데이터를 추가할 건지에 대한 질문이다 // 기본적인 간단한 모니터링만 진행할 것이기 때문에 사용하지 않았다 Do you want the CloudWatch agent to also retrieve X-ray traces? 1. yes 2. no default choice: [1]: 2 Existing config JSON identified and copied to: /opt/aws/amazon-cloudwatch-agent/etc/backup-configs Saved config file to /opt/aws/amazon-cloudwatch-agent/bin/config.json successfully. // 최종 만들어진 설정 파일다 Current config as follows: { "agent": { "metrics_collection_interval": 60, "run_as_user": "root" }, "logs": { "logs_collected": { "files": { "collect_list": [ { "file_path": "/home/ubuntu/actions-runner/server.log", "log_group_class": "STANDARD", "log_group_name": "2024-bang-ggood-prod1", "log_stream_name": "server-log", "retention_in_days": 90 } ] } } }, "metrics": { "aggregation_dimensions": [ [ "InstanceId" ] ], "append_dimensions": { "AutoScalingGroupName": "${aws:AutoScalingGroupName}", "ImageId": "${aws:ImageId}", "InstanceId": "${aws:InstanceId}", "InstanceType": "${aws:InstanceType}" }, "metrics_collected": { "cpu": { "measurement": [ "cpu_usage_idle", "cpu_usage_iowait", "cpu_usage_user", "cpu_usage_system" ], "metrics_collection_interval": 60, "resources": [ "*" ], "totalcpu": false }, "disk": { "measurement": [ "used_percent", "inodes_free" ], "metrics_collection_interval": 60, "resources": [ "*" ] }, "diskio": { "measurement": [ "io_time" ], "metrics_collection_interval": 60, "resources": [ "*" ] }, "mem": { "measurement": [ "mem_used_percent" ], "metrics_collection_interval": 60 }, "statsd": { "metrics_aggregation_interval": 60, "metrics_collection_interval": 10, "service_address": ":8125" }, "swap": { "measurement": [ "swap_used_percent" ], "metrics_collection_interval": 60 } } } Please check the above content of the config. The config file is also located at /opt/aws/amazon-cloudwatch-agent/bin/config.json. Edit it manually if needed. Do you want to store the config in the SSM parameter store? 1. yes 2. no default choice: [1]: 2 Program exits now.
-
완료 후 CloudWatchAgent를 실행한다.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config \
-m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
- 실행 상태를 확인한다.
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
- 해당 EC2에 IAM 권한을 추가하여 CloudWatch로 기록할 수 있도록 한다.