Actuator 기본 설정
- build.gradle
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
- application.properties 엔드포인트 설정
spring.application.name=sample
server.port=8080
#모든 엔드포인트 노출 설정
management.endpoints.web.exposure.include=*
#헬스 체크 엔드포인트 상세 정보 표시 설정
#이 설정은 /actuator/health 엔드포인트에서 헬스 체크 정보를 항상 상세히 보여주도록 설정합니다.
#기본적으로, 헬스 체크 엔드포인트는 요약된 상태 정보만 제공하며, 상세 정보는 노출되지 않습니다.
management.endpoint.health.show-details=always
- 선택사항
# Actuator 엔드포인트를 19090 포트에서 서비스하도록 설정
management.server.port=19090
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/**").authenticated()
.and()
.httpBasic();
}
}
Prometheus 기본 설정
- build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
- application.properties
spring.application.name=sample
server.port=8080
#모든 엔드포인트 노출 설정
management.endpoints.web.exposure.include=*
#헬스 체크 엔드포인트 상세 정보 표시 설정
#이 설정은 /actuator/health 엔드포인트에서 헬스 체크 정보를 항상 상세히 보여주도록 설정합니다.
#기본적으로, 헬스 체크 엔드포인트는 요약된 상태 정보만 제공하며, 상세 정보는 노출되지 않습니다.
management.endpoint.health.show-details=always
management.endpoint.prometheus.enabled=true
- prometheus.yml 설정 파일 생성
- Prometheus가 모니터링할 타겟과 기타 설정을 정의하는 설정 파일(prometheus.yml)을 생성합니다.
- host.docker.internal 은 Docker에서 제공하는 특수한 DNS 이름으로, Docker 컨테이너가 호스트 머신(즉, Docker를 실행하는 컴퓨터)의 네트워크 서비스에 접근할 수 있도록 합니다. 이를 통해 컨테이너 내부에서 호스트 머신의 네트워크 주소를 참조할 수 있습니다.
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'spring-boot'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['host.docker.internal:8080']
- Prometheus 실행
- Docker 명령어를 사용해 prometheus.yml 속한 폴더 경로에서 컨테이너를 실행합니다.
- -v 옵션의 앞부분은 방금전 생성한 prometheus.yml의 경로를 포함하여 작성합니다.
docker run -d --name=prometheus -p 9090:9090 -v {prometheus.yml 속한 폴더 경로}/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
- localhost:9090 에 접속해봅니다.프로메테우스 서버에 접속할 수 있습니다.
- 상단 메뉴에서 Status > Targets에 접속하여 스프링 애플리케이션의 매트릭스를 수집하고 있는것을 확인 할 수 있습니다 (나오지 않는다면 스프링 애플리케이션을 Run하고 있는지 확인합니다.)
Grafans 기본 설정
- Docker 사용하여 Container 실행
- localhost:3000에 접속하여 로그인을 진행합니다. 기본계정의 아이디와 비밀번호는 admin/admin 입니다.
docker run -d --name=grafana -p 3000:3000 grafana/grafana
- 대시보드에서 DATA SOURCE 를 클릭한 후, Prometheus를 선택합니다.
- Name을 입력한후, Contact에 Prometheus Server URL 을 입력합니다. 여기서는 host.docker.internal을 사용했습니다(스프링 애플리케이션이 호스트 PC에서 실행되어 접근하기 위함)
- 그후, Save & test를 클릭하여 저장합니다.
- 대시보드 메뉴에 접속하여 Create Dashboard 버튼을 클릭합니다. 그후 import dashboard 를 클릭합니다.
- grafana.com/dashboards 접속하여 원하는 대시보드 ID 복사하여 Load 클릭
- 임포트 창에서 19004을 입력하고 Load 버튼을 클릭합니다.19004는 그라파나에서 제공하는 spring boot 3용 대시보드입니다.
- prometheus 를 선택하고 Import 를 클릭하면 대시보드가 생성됩니다.
그라파나에서 Slack으로 Alert 보내기
- 슬랙 앱 생성
- 먼저 슬랙에 가입해서 워크스페이스를 생성해주세요~!
- https://api.slack.com/apps 에 접속해서 Create an App을 클릭합니다.
- 팝업이 뜨면 From scratch 를 클릭합니다.
- 앱이름을 입력하고 생성한 워크스페이스를 선택합니다.
- OAuth & Permissions 메뉴에 접속합니다.
- 스크롤 하여 Scopes 항목으로 갑니다. Bot Token Scopes에 “chat:write”를 추가합니다.
- 그후 스크롤을 올라와서 Install to Workspace 를 클릭합니다. 그리고 허용을 클릭합니다.
- Incoming Webhooks 메뉴에 접속합니다. Activate Incoming Webhooks 를 활성화 한 후, 하단에 Add New Webhook to Workspace를 클릭합니다. 다음 화면에서 전달 받고 싶은 슬랙 채널을 선택합니다. 그리고 생성된 Webhook URL을 복사해 둡니다.
- 슬랙 앱에서 해당 채널에 가서 생성한 App을 “@”를 사용하여 추가해줍니다.
그라파나 Alert 설정
- 그라파나에서 사이드메뉴에 Alerting > Contact points 에 접속하여 Add contact point 버튼을 클릭합니다.
- Name 을 입력한 후, Integration 을 Slack을 선택합니다. 그후 Webhook URL에 아까 앱에서 복사한 URL을 입력합니다. 테스트 버튼을 클릭하면 테스트 메시지가 슬랙 채널로 오는 것을 확인할 수 있습니다.
- 그라파나 사이드 메뉴에서 Alerting > Notification policies 로 들어갑니다. 그 후 Default policy의 edit 버튼을 클릭합니다. 이후 나온 Edit창에서 Default contact point 를 이전에 생성한 contact point로 선택합니다.
- Alerting > Alert rules 를 클릭하여 “New alert rule”을 클릭합니다.
- 알림 이름을 입력합니다.
- Define query and alert condition 에서 matric을 UP을 선택하고 Label filter 에서 Job , spring-boot 를 선택합니다.
- Expression 의 Threshold에서 IS BLOW를 선택 숫자는 1을 입력합니다. 이를 통해 만약 애플리케이션이 정지되면 알람이 발송되게 됩니다.
- 스크롤 하여 내려가면 Set evaluation behavior 섹션을 볼 수 있습니다.
- Folder 및 Evaluation group을 선택또는 새로 생성합니다.
- pending period, Evaluation internal 은 빠른 확인을 위해 1m으로 설정합니다.
- 좀더 스크롤해 내려가면 Confifure labels and notifications 메뉴가 있습니다. 해당 메뉴의 contact point 을 이전에 설정한 slack으로 설정합니다.
그라파나 Alert 설정 확인
- Alert rules에 생성한 Alert이 노출 됩니다. 상태는 Normal인 것을 확인할 수 있습니다.
- 스플링 애플리케이션을 정지 시키면 Normal 이었던 상태가 Pending > Firing으로 변경됩니다.
- 그후 잠시 기다려면 슬랙 채널로 Firing 알람이 오는것을 확인할 수 있습니다.
- 다시 스프링 애플리케이션을 실행한후 기다리면 슬랙 채널로 Resolved 알람이 오는것을 확인할수 있습니다.
- 해당 알람은 도착하는데 시간이 걸립니다!
'모니터링 시스템' 카테고리의 다른 글
애플리케이션 로그 모니터링(Loki) (1) | 2024.08.16 |
---|---|
Actuator, Prometheus, Grafana 기본 이론 (0) | 2024.08.16 |
모니터링이란? (0) | 2024.08.16 |