Spring

도커 네트워크에서 프로메테우스, 스프링 부트 및 스프링 시큐리티 마스터하기: 종합 가이드

kimc 2024. 1. 10. 22:36
반응형

Mastering Prometheus, Spring Boot, and Spring Security in Docker Networks: A Comprehensive Guide by codemasterkimc


 

이번 글을 통해 배워 갈 내용

  1. 프로메테우스 보안 설정(포트 기반/ kotlin)

 

먼저 스프링 부트 프로메테우스 기본 설정은 아래 블로그를 참조해 주시면 됩니다

https://codemasterkimc.tistory.com/691

 

프로메테우스, 그라파나, 집킨으로 스프링 부트 모니터링 하는 한가지 방법

Monitoring Spring Boot 3.15 with Prometheus, Grafana, and Zipkin with docker compose 스프링 부트를 위해서 초간단 모니터링 시스템 구축하는 방법입니다 도커 컴포즈를 활용해서 빠르게 구축했습니다 이번 글을 통

codemasterkimc.tistory.com

 

 

먼저 Spring Security 내부에 코드를 수정합니다

 

 

Security Config 에

SecurityFilterChain에

몇 가지 추가하였습니다

 

 

먼저

requestMatcher에 커스텀 함수 forPort를 만들어서 사용하였습니다

.requestMatchers(forPort(managementServerPort)).permitAll()

 

 

아래와 같이 forPortAndPath도 만들었으며 다음과 같이 사용도 가능합니다

.requestMatchers(forPortAndPath(managementServerPort, "/actuator/prometheus")).permitAll()

 

 

managementServerPort 는 9999와 같이 쓰시고자 하는 포트 번호를 인트 값으로 넣어주면 됩니다

/**
 * Constructs a RequestMatcher for requests matching a specific port and path.
 * Author: codemasterkimc
 *
 * @param port        Target port for matching.
 * @param pathPattern Ant-style path pattern for matching.
 * @return Combined RequestMatcher for port and path.
 */
private fun forPortAndPath(port: Int, pathPattern: String): RequestMatcher {
    return AndRequestMatcher(forPort(port), AntPathRequestMatcher(pathPattern))
}

 

/**
 * Creates a RequestMatcher for requests on a specific local port.
 * Author: codemasterkimc
 *
 * @param port Target port for matching.
 * @return RequestMatcher for the specified port.
 */
private fun forPort(port: Int): RequestMatcher {
    println("port")
    return RequestMatcher { request: HttpServletRequest -> port == request.localPort }
}

 

 

그다음 docker compose에 포트를 열어줍니다

컨테이너명칭:
 ... 다른 값들
  expose:
    - "9999"

 

 

prometheus.yml 파일도 수정해 줍니다

global:
 ...기타설정

scrape_configs:
 ...기타설정
    static_configs:
      - targets: ['컨테이너명:9999']

 

 

그다음 스프링 프로젝트를 빌드하고

도커 컴포즈 컨테이너들을 내렸다가 올리면

docker compose down 스프링컨테이너명칭

docker compose down 프로메테우스컨테이너명칭

docker compose up -d 프로메테우스컨테이너명칭

docker-compose up -d --build 스프링컨테이너명칭

 

보안 세팅이 적용되어서

외부에서는 접근이 안되고

도커 네트워크 내부에서만 접근이 되는 것을 확인할 수 있습니다

 

 

 

필요시 Nginx 세팅을 해서

외부에서 특정 IP만 접속가능하게 변경을 해줍니다

 

읽어주셔서 감사합니다

 

결과물 확인

 


참조 및 인용

https://stackoverflow.com/questions/50009932/environment-variables-in-prometheus-values

 

Environment variables in prometheus values

i would like to set environment specific values based on environment qa/prod in prometheus values file ## Additional alertmanager container environment variable ## For instance to add a http_pr...

stackoverflow.com

 

https://stackoverflow.com/questions/60272588/securing-spring-boot-2-actuator-prometheus-end-points

 

Securing Spring Boot 2 Actuator Prometheus End Points

I have a Spring Boot 2 with Spring Security microservice that I have configured with Micometer/Spring Actuator. All is well when I permitAll() on the antMatcher("/actuator/**") end points. I am a...

stackoverflow.com

 


블로그 추천 포스트

https://codemasterkimc.tistory.com/50

 

300년차 개발자의 좋은 코드 5계명 (Clean Code)

이번 글을 통해 배워갈 내용  좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋

codemasterkimc.tistory.com

 

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

반응형