오늘의 인기 글
최근 글
최근 댓글
Today
Total
04-29 01:41
관리 메뉴

우노

[Prometheus] Dashboard에 Basic Auth 적용하는 방법 본문

DevOps/Prometheus

[Prometheus] Dashboard에 Basic Auth 적용하는 방법

운호(Noah) 2023. 10. 20. 14:09

들어가기 앞서,

  • 해당 포스팅에선, K8S Deployment로 정의한 Prometheus Container를
  • 기본 설정 파일 및 Basic Auth가 적용된 웹 설정 파일을 기반으로 실행하는 방법에 대해서 다뤄보겠습니다.
  • 관련 리소스는 Terraform으로 관리하고 있기 때문에, HCL 코드를 기반으로 설명하겠습니다.

Basic Auth에 사용할 비밀번호 해싱

  • python3-bcrypt를 사용해 Basic Auth에 사용할 비밀번호를 해싱합니다.

      import getpass
      import bcrypt
    
      password = getpass.getpass("password: ")
      hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
      print(hashed_password.decode())
  • 이후, 비밀번호를 test로 입력한다면 아래와 같은 결과값이 출력됩니다.

      password:
      $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

kubernetes_config_map 예제 코드

  • Deployment를 통해 Prometheus Container가 시작될 때,

  • Prometheus가 기본 설정 파일 및 웹 설정 파일을 Container 내부에서 찾아 실행될 수 있도록,

  • Prometheus Container 내부에 마운트할 파일들을 kubernetes_config_map을 통해 정의합니다.

      resource "kubernetes_config_map" "prometheus_config" {
        metadata {
          name = "prometheus-config"
        }
    
        data = {
          "prometheus.yml" = <<EOF
      global:
        scrape_interval: 30s
        evaluation_interval: 30s
        scrape_timeout: 10s
    
      scrape_configs:
        - job_name: 'statsd-exporter'
          static_configs:
            - targets: ['prometheus-statsd-exporter.default.svc.cluster.local:9102']
      EOF
    
          "web.yml" = <<EOF
      basic_auth_users:
        admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
      EOF
        }
      }

kubernetes_deployment 예제 코드

  • kubernetes_config_map이 Prometheus Container 내부로 volume mount 될 수 있도록 설정한 뒤,

  • Prometheus가 기본 설정 파일 및 웹 설정 파일을 Container 내부에서 찾아 실행될 수 있도록,

  • Prometheus Container 실행 인자로 기본 설정 파일 및 웹 설정 파일의 경로를 넣어줍니다.

      resource "kubernetes_deployment" "prometheus" {
        metadata {
          name = "prometheus"
        }
    
        spec {
          replicas = 1
    
          selector {
            match_labels = {
              app = "prometheus"
            }
          }
    
          template {
            metadata {
              labels = {
                app = "prometheus"
              }
            }
    
            spec {
              container {
                image = "prom/prometheus"
                name  = "prometheus"
                args = [
                              "--config.file=/etc/prometheus/prometheus.yml",
                  "--web.config.file=/etc/prometheus/web.yml"
                ]
                port {
                  container_port = 9090
                }
    
                volume_mount {
                  mount_path = "/etc/prometheus/"
                  name       = "prometheus-config-volume"
                }
              }
              volume {
                name = "prometheus-config-volume"
                config_map {
                  name = kubernetes_config_map.prometheus_config.metadata[0].name
                }
              }
            }
          }
        }
      }

참고

Comments