모니터링

[ stackdriver-export ] 도커를 이용하여 컨테이너 빌드하기

블루빔 2023. 7. 26. 11:30

해당 글에서는 Dockerfile로 stackdriver-export 커스텀 도커이미지를 생성하고 compose.yml 파일에 컨테이너 서비스 설정을 기재하여 애플리케이션 배포를 실행해 보려고 합니다.

목차
▶ 전체적인 파일 구조
▶ 도커 이미지 파일 작성
▶ 도커 이미지 빌드 및 확인
▶ compose.yml 파일 작성
▶ 컨테이너 빌드 & 확인

 

전체적인 파일 구조

── mon  
   ├── compose.yml   
   ├── Dockerfile   
   └── keyfile      
       └── service_account.json

 


도커 이미지 파일 작성 

먼저 mon 디렉터리를 생성 후 하위에 Dockerfile을 작성합니다. 

FROM prometheuscommunity/stackdriver-exporter

USER       nobody
EXPOSE     9255
ENTRYPOINT ["/bin/stackdriver_exporter"]

 

도커 이미지 빌드 및 확인 

docker build . -t 도커이미지네임
docker images

예시 :

 

 

compose.yml 파일 작성

compose.yml 파일에 stackdriver-export 설치에 필요한 서비스 설정들을 기재합니다. 

version: '3.8'

services:
    stackdriver-export:
        container_name: 생성할 컨테이너 네임
        image: 사용할 도커이미지네임
        restart: unless-stopped
        
        ####호스트 경로:컨테이너내부 경로 ####
        volumes:
            - ./keyfile:/keyfile:ro
        environment:
            GOOGLE_APPLICATION_CREDENTIALS: "/keyfile/service_account.json"
        
        #####호스트 OS에서 공개할 포트:컨테이너내부 포트#####    
        ports:
            - "9255:9255"

        user: root
        command:
             "--google.project-id=프로젝트id
              --monitoring.metrics-type-prefixes='compute.googleapis.com/instance/cpu/utilization,agent.googleapis.com/disk/percent_used,agent.googleapis.com/memory/percent_used'"

주의할 사항

  • 생성할 컨테이너 네임은 구동 중인 컨테이너 네임과 일치하지 않아야 합니다.
  • 사용할 도커 이미지 네임이 정확하여야 합니다.
  • 호스트 볼륨을 사용할 경우 호스트 디렉터리 경로를 정확하게 기재하여야 합니다. 
  • 컨테이너 내부 포트는 동일해도 호스트 OS에서 사용할 포트는 동일하지 않아야 합니다.
  • 프로젝트 id가 정확하여야 합니다.
  • 수집할 GCP 모니터링 메트릭 타입 prefixes 가 정확하여야 합니다. 
      🔗더 자세한 내용은 해당 링크를 참고 :  https://cloud.google.com/monitoring/api/metrics_opsagent#agent-cpu 
 

Ops Agent metrics  |  Cloud Monitoring  |  Google Cloud

When you install the Ops Agent on your VM instances, the agent transmits data for the metric types listed on this page to Monitoring. For information about choosing the right agent, see Ops Agent overview. Important: You must install the Ops Agent to colle

cloud.google.com

 

 

컨테이너 빌드

docker compose up

🚨 에러 발생 : environment variable: open /keyfile/service_account.json: no such file or directory”
      원인 : volumes 항목 작성 시 호스트 디렉터리 경로를 정확하게 입력하지 않아서 발생하는 이슈입니다. 

🚨 에러 발생 : 'docker login': denied: requested access to the resource is denied
      원인 : compose.yml 파일에 작성한 도커 이미지 네임이 현재 사용가능한 도커 이미지 네임과 일치하지 않아서 발생한 이슈입니다. 

 

확인 

curl http://localhost:9255/metrics
-------------------------------------------------------
stackdriver_monitoring_last_scrape_error{project_id="프로젝트id"} 0  => 수치가 "0"이면 정상 수집 되고 있습니다