2024.10.30 - 2024.11.07
์ด์ ๊น์ง๋ ๊ธฐ๋ฅ ๊ฐ๋ฐ์ ์ํด ์ญ ๋ฌ๋ ค์๋ค๋ฉด, ๋ ๋ฒจ 5์ ๋ค์ด์๋ถํฐ๋ ๊ฐ์ ์ทจ์ ์ค๋น๋ก ๋ฐ์ ์์ฆ์ด๋ผ ์ ์ ๊ธฐ๋ฅ ๊ฐ๋ฐ์ ๋ฉ์ท๋ค. ์์ง ๋ณธ๊ฒฉ์ ์ผ๋ก ์ทจ์ ์ค๋น๋ฅผ ํ๊ณ ์์ง ์์ ๋๋ ์์์ ํจ๊ป ๊ทธ๋์ ์ ์ฉํด๋ณด์ง ๋ชปํ ๊ธฐ๋ฅ๋ค์ ํ๋์ฉ ์ถ๊ฐํด ํ๋ก์ ํธ์ ์์ฑ๋๋ฅผ ๋์ด๊ณ ์ ํ๋ค. ์ฒซ์์์ ๋ชจ๋ํฐ๋ง ์์คํ ์ฌ๊ตฌ์ถ์ด๋ค ๐

๊ธฐ์กด์๋ ๋ชจ๋ํฐ๋ง์ ์ํด CloudWatch๋ฅผ ์ฌ์ฉํ๋ค. ๊ฑฐ์ ๋ก๊ทธ ํ์ธ์ฉ์ผ๋ก๋ง ์ฌ์ฉํ๊ณ ๊ธฐ๋ณธ์ ์ธ ์๋ฒ ๋ฉํธ๋ฆญ์ ์ ์ธํ๊ณ ๋ ์ ์๋ฏธํ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ด๋ ค์ ๋ค. ๋ฐ๋ผ์ ๋ฐ๋ชจ๋ฐ์ด ์๊ตฌ์ฌํญ ์ค TPS ํ
์คํธ๋ฅผ ์งํํ ๋ ํน์ API์ ์๋๊ฐ ๋๋ ค์ง๋ ๋ฌธ์ ์์ธ์ ํ์
ํ๋๋ฐ์๋ ํ๊ณ๊ฐ ์์๋ค. ๋ก๊ทธ๋ฅผ ๊ฒ์ํ๊ณ ํ์ธํ๋๋ฐ์๋ ๋ถํธํจ์ด ์ปธ๋ค. ๋ค์ํ ๋ฉํธ๋ฆญ ์ ๋ณด๋ฅผ ์์งํ๊ณ ๋ก๊ทธ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ค ํจ์จ์ ์ผ๋ก ํ์ธํ๊ธฐ ์ํด Loki, Prometheus, Node Exporter, Grafana๋ฅผ ํ์ฉํด ๋ชจ๋ํฐ๋ง ์์คํ
์ ๊ตฌ์ถํ๋ค.

๋ฐ์ดํฐ ๋ชจ๋ํฐ๋ง ๋ฐ ๋ฐ์ดํฐ ์๊ฐํ
Dashboard ์๋ฒ์ Grafana ์ค์น
sudo apt install -y apt-transport-https software-properties-common wget
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install grafana
sudo grafana-server -v

grafana ์ค์น ํ ์ ์์ด ๋์ง ์๋ ๋ฌธ์ ์ํฉ์ด ์์๋ค. ๋ฌด์์ ๋ธ๋ก๊ทธ๋ฅผ ๋ฐ๋ผ ๋ช ๋ น์ด๋ฅผ ์น ๊ฒ์ด ๋ฌธ์ ์๋ค. ๋ง์ ๊ธ์์ grafana๊ฐ ๋ฐฉํ๋ฒฝ์ ์ํด ์ฐจ๋จ๋ ์ ์์ผ๋ฏ๋ก ํฌํธ 3000์ ์ด์ด์ฃผ์๋ค. ์๋์ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ค.
sudo ufw enable
sudo ufw allow ssh
sudo ufw allow 3000/tcp
ํ์ฌ ์๋ฒ๋ 80, 443 ํฌํธ๋ง ์ด๋ ค ์๊ณ nginx๋ฅผ ํตํด 80์ 3000 ํฌํธ๋ก ๋ฆฌ๋ค์ด๋ ํ ํด์ฃผ๊ณ ์์๋ค. ๋ฐฉํ๋ฒฝ์ผ๋ก 3000ํฌํธ๋ฅผ ํ์ฉํด์ฃผ์์ง๋ง ์ด์ธ์ ๋ค๋ฅธ ํฌํธ๋ ํ์ฉํ์ง ์์๋ค. ๋ฐ๋ผ์ 80ํฌํธ๋ก ๋ค์ด์ค๋ ์์ฒญ์ด ์ฐจ๋จ๋๋ฉด์ nginx๋ก ๋๋ฌํ๊ธฐ๋ ์ ์ ์ ๊ทผ์ด ์ฐจ๋จ๋ ๊ฒ์ด๋ค. ufw๋ฅผ disable ํด์ค์ผ๋ก์จ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
Logback์ผ๋ก ๋จ๊ธฐ๋ ๋ก๊ทธ ํ์ผ ๋ชจ๋ํฐ๋ง
Promtail์ ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์ ์ผ๋ก๋ถํฐ ์์ ๋ก๊ทธ๋ฅผ Loki๋ก ์ ์กํ๋ค. Loki๋ Promtail๋ก๋ถํฐ ๋ก๊ทธ๋ฅผ ๋ฐ์ ๋ฐ์ดํฐ ํํ์ ๋ง๊ฒ ์ฒ๋ฆฌํ๋ค.
Monitoring ์๋ฒ์ Loki ์ค์น
wget https://github.com/grafana/loki/releases/download/v3.2.1/loki-linux-arm64.zip
unzip loki-linux-arm64.zip
2024-10-30 ๊ธฐ์ค ์ต์ ๋ฒ์ ์ธ 3.2.1 ๋ฒ์ ์ ๋ฆฌ๋ ์ค arm64 ๋ฒ์ ์ ๋ค์ด๋ฐ์๋ค. Grafana์ ํธํ์ด ๋๋์ง ํ์ธํ๊ณ ๋ค์ด๋ฐ๋ ๊ฒ์ด ์ค์ํ๋ค.
Loki ์ค์ ํ์ผ ๋ค์ด๋ก๋
wget https://raw.githubusercontent.com/grafana/loki/v3.2.1/cmd/loki/loki-local-config.yaml
์์ ์ค์นํ ํ์ผ์ ์คํํ์ผ์ด๊ธฐ ๋๋ฌธ์ ์ค์ ํ์ผ์ ๋ฐ๋ก ๋ค์ด๋ฐ์์ผ ํ๋ค.
PROD ์๋ฒ์ Promtail ์ค์น
wget https://github.com/grafana/loki/releases/download/v2.6.1/promtail-linux-arm64.zip
unzip promtail-linux-arm64.zip
wget https://raw.githubusercontent.com/grafana/loki/v2.6.1/clients/cmd/promtail/promtail-local-config.yaml
๊ธฐ๋ณธ์ ์ผ๋ก ์ค์น๋ Loki์ ๋น์ทํ๋ค.
Promtail ์ค์ ํ์ผ ์์
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://{Loki ์๋ฒ IP}:{Loki ์๋ฒ ํฌํธ}/loki/api/v1/push
scrape_configs:
- job_name: bang-ggood-log
static_configs:
- targets:
- localhost
labels:
jobs: prod-app-log
__path__: /home/ubuntu/actions-runner/server.log
/loki/api/v1/push ๋ promtail์์ loki๋ก ๋ก๊ทธ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ API ์ค๋ํฌ์ธํธ์ด๋ค. ์ด ์ค๋ํฌ์ธํธ๋ loki์ ๋ฐ์ดํฐ ์ ์ฅ์์ ๋ก๊ทธ๋ฅผ ํธ์ํ๋๋ฐ ์ฌ์ฉ๋๋ค. ํ์ฌ PROD ์๋ฒ์ Monitoring ์๋ฒ๋ private IP์ private IP๋ฅผ ์ฌ์ฉํ๊ณ ์์ง๋ง ์๋ก ๊ฐ์ ์๋ธ๋ท์ ์์ผ๋ฏ๋ก private IP๋ก๋ ํต์ ํ ์ ์๋ค.
Grafana ์ฐ๋

์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ค์ํ ๋ฉํธ๋ฆญ ์ ๋ณด ์์ง
Prometheus๋ pull ๋ชจ๋ธ์ ์ฌ์ฉํด ์ ํ๋ฆฌ์ผ์ด์
๊ณผ ์๋น์ค์์ ๋ฉํธ๋ฆญ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ์์งํ๋ค. Spring Actuator๋ ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์
์ ๊ด๋ฆฌ ๋ฐ ๋ชจ๋ํฐ๋ง์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. Spring Boot Actuator๋ /actuator/prometheus ์๋ํฌ์ธํธ๋ฅผ ์ ๊ณตํ์ฌ Prometheus๊ฐ ์์งํ ์ ์๋ ์ ํ๋ฆฌ์ผ์ด์
๋ฉํธ๋ฆญ์ ๋
ธ์ถํ๋ค.
PROD ์๋ฒ์ Spring Actuator์ Prometheus ์์กด์ฑ ์ถ๊ฐ
// actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// prometheus
implementation 'io.micrometer:micrometer-registry-prometheus'
YML ์ถ๊ฐ
management:
endpoints:
web:
exposure:
include: httpexchanges, metrics, prometheus
httpexchanges:
recording:
enabled: true
prometheus:
metrics:
export:
enabled: true
/healthย ์ย /infoย 2๊ฐ์ง endpoint๋ง deafult๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ์๋์ ๋ ๊ฐ์ง ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ค.Monitoring ์๋ฒ์ Prometheus ์ค์น
wget https://github.com/prometheus/prometheus/releases/download/v2.53.3/prometheus-2.53.3.linux-arm64
tar -xzf prometheus-2.53.3.linux-arm64
cd prometheus-2.53.3.linux-arm64
// ์คํ
sudo nohup ./prometheus --config.file=prometheus.yml > prometheus.log 2>&1 &
์์ถํด์ ํ ํด๋ ์์ prometheus.yml ์ค์ ํ์ผ์ ์์ ํด์ ๋ชจ๋ํฐ๋งํ ํ๊ฒ ๋ฉํธ๋ฆญ์ ์์งํ๋ ๋ฐฉ๋ฒ์ ์ ์ํ๋ค.
...
scrape_configs:
- job_name: "spring-actuator"
metrics_path: /actuator/prometheus
scrape_interval: 10s
static_configs:
- targets: [{PROD ์๋ฒ IP}]
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
/actuator/prometheus ๋ฅผ ํตํด JVM (Java Virtual Machine) ๋ฉํธ๋ฆญ, CPU ๋ฐ ์์คํ
๋ฉํธ๋ฆญ, HTTP ์์ฒญ ๋ฉํธ๋ฆญ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฉํธ๋ฆญ (DataSource) ๋ฑ๋ฑ ๋ค์ํ ๋ฉํธ๋ฆญ์ ํ์ธํ ์ ์๋ค.
์ค์ ํ์ผ ๋ณ๊ฒฝ ์๋ฃ ํ ๋ชจ๋ํฐ๋ง ์๋ฒ์์ PROD ์๋ฒ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋์ง ํ์ธํ๊ธฐ ์ํด curl ์์ฒญ์ ๋ณด๋์ง๋ง ๋ค์๊ณผ ๊ฐ์ด 404 ํ์ด์ง๋ฅผ ์๋ตํ๋ ๋ฌธ์ ์ํฉ์ด ์์๋ค.
curl http://{PROD ์๋ฒ IP}/actuator/prometheus
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0 (Ubuntu)</center>
</body>
</html>
2์๊ฐ ๋๊ฒ ๋๋ฒ๊น ํด๋ณธ ๊ฒฐ๊ณผ Nginx ์ค์ ํ์ผ์ ๋ฌธ์ ๊ฐ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋๋ค. curl ์์ฒญ์ 404๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ ์ฐ๊ฒฐ ๋ฌธ์ ๋ ์๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค. ๋ค๋ง 404๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ ์ ํ๋ฆฌ์ผ์ด์ ์ผ๋ก ์์ฒญ์ด ๋๋ฌํ๊ธฐ ์ ์ Nginx๋จ์์ ๋ฐ๋ก ์๋ต๊ฐ์ ๋ด๋ ค์ค ๊ฒ์ ์๋ฏธํ๋ค.
๋ค์๊ณผ ๊ฐ์ด 80์ ๋ํ ์์ฒญ์ ์๋ตํ ์ ์๋๋ก ์ค์ ์ ์ถ๊ฐํด์ฃผ์๊ณ ์๋ต๊ฐ์ด ์ ๋๋ก ์ค๋ ๊ฒ์ ํ์ธํ๋ค.
server {
listen 80;
server_name _;
...
}
# HELP application_ready_time_seconds Time taken for the application to be ready to service requests
# TYPE application_ready_time_seconds gauge
application_ready_time_seconds{main_application_class="com.bang_ggood.Application"} 15.385
...
Grafana ์ฐ๋

/actuator/prometheus ๋ก ์ธ๋ถ์์๋ ๋ฉํธ๋ฆญ ์ ๋ณด๊ฐ ๋
ธ์ถ๋๋ ๊ฒ์ ํ์ธํ๋ค. private ์๋ธ๋ท์ ์กด์ฌํ๋ Monitoring ์๋ฒ์์๋ง ๋ฉํธ๋ฆญ ์ ๋ณด๋ฅผ ์์งํ ์ ์๋๋ก Nginx๋ฅผ ํ์ฉํด ์ธ๋ถ ์ ๊ทผ์ ์ฐจ๋จํ๋ค.
CPU ์ฌ์ฉ๋, ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋, ๋์คํฌ ์ฌ์ฉ๋ ๋ฑ๊ณผ ๊ฐ์ ์์คํ ๋ฉํธ๋ฆญ ์ ๋ณด ์์ง
PROD ์๋ฒ์ Node Exporter ์ค์น
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xzvf node_exporter-1.8.2.linux-amd64.tar.gz
cd node_exporter-1.8.2.linux-amd64/
// ์คํ
sudo nohup ./node_exporter > node_exporter.log 2>&1 &
Monitoring ์๋ฒ๋ด prometheus.yml ์ค์ ํ์ผ์ ๋ด์ฉ ์ถ๊ฐ
...
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: [{PROD ์๋ฒ IP}]
Grafana ์ฐ๋

์ฐ๋ฆฌ ํ์ Docker๋ฅผ ์ฌ์ฉํ๊ณ ์์ง ์์ ์ง์ ์ค์นํ๊ณ ์ค์ ์ ๋ณด๋ฅผ ๋ณ๊ฒฝํ๋ ๊ณผ์ ์์ ๋ง์ ํธ๋ฌ๋ธ ์ํ ์ด ์์๋ค. ์ด์ ์ ๋ชจ๋ํฐ๋ง ์์คํ ์ ๊ตฌ์ถํ๋ ํ์๊ฒ ๋ฌผ์ด๋ณด๋ฉฐ ํ๋์ฉ ํด๊ฒฐํ๋๋ฐ, ์ ๋ง ์ฝ์ง ์์๋ค. ๋๋ถ์ ๋ฆฌ๋ ์ค ๋ช ๋ น์ด์ ํ์ธต ๊ฐ๊น์์ก์ง๋ง ๊ทธ๋๋ ๋ค์๋ถํฐ๋ Docker๋ฅผ ์ฌ์ฉํด์ผ๊ฒ ๋ค๊ณ ๋๊ผ๋ค..ใ ใ ์์ฑํ๊ณ ๋ณด๋ ๋ ๋นจ๋ฆฌ ๊ตฌ์ถํ์ผ๋ฉด ์ด๋ ์๊น ์ถ๋ค. ์๋ฃ๊น์ง ์ผ๋ง๋จ์ง ์์์ง๋ง ๋จ์ ๊ธฐ๊ฐ ์ ํ์ฉํด๋ณผ ์ ์์์ ์ข๊ฒ ๋ค :)
์ฐธ๊ณ
Prometheus & Grafana ๋ชจ๋ํฐ๋ง ์์คํ ๊ตฌ์ถํ๊ธฐ
Eden's Devpedia ๐
[ubuntu] ๊ทธ๋ผํํ ์ค์น ๋ฐฉ๋ฒ
[Prometheus] prometheus ์์ํ๊ธฐ (์ค์น, ๊ตฌ์ฑ, ์คํ)
Actuator - Prometheus - Grafana๋ฅผ ์ฌ์ฉํ ๋ชจ๋ํฐ๋ง ๊ตฌ์ถํ๊ธฐ