본문 바로가기

파이프라인

fluentd-pubsub 예제

httpd의 access 로그를 fluentd로 수집하여 Google Pubsub으로 전송하는 예제

실습환경

  • windows 11에서 wsl 설정하여 docker 실행

Google 클라우드 Pubsub 설정

주제 만들기. 명시된 주제이름은 fluentd.conf에 명시 필요
구독 만들기

 

Google 클라우드의 서비스 계정 발급과 권한 설정

  • https://console.cloud.google.com/iam-admin/iam 으로 이동하여 "서비스 계정" 클릭
    •  "서비스 계정 만들" 클릭 후 서비스 계정 이름 입력 후 완료. 다른 선택사항은 권한 관련으로 추후 설정 가능
    • IAM 메뉴로 이동하여 "액세스 권한 부여" 클릭하여 위에서 생성한 서비스 계정이름을 새 주 구성원에 입력 후 역할은 게시/구독 게시자 선택 후 저장

서비스 계정 생성
서비스 계정에 pubsub 권한 설정

 

Google Cloud 서비스 계정의 서비스 키 다운로드

  • IAM 메뉴의 "서비스 계정" 클릭
  • 생성한 서비스 계정 클릭 후 "키" 탭으로 이동
  • "키 추가" 클릭 후 "새 키 만들기" 클릭 후 JSON 선택한 후 만들기 클릭하면 로컬에 json 키 다운로드 완료

 

 

docker-compose 를 이용해 도커 컨테이너 생성

  • 구성
    • docker-compose.yml
    • fluentd/conf/fluentd.conf
    • fluentd/conf/구글서비스키.json
#docker-compose.yml
version: "3"
services:
  web:
    image: httpd   
    container_name: web
    ports:
      - "80:80"
    links:
      - fluentd
    logging:
      driver: "fluentd"
      options:
        fluentd-address: localhost:24224
        tag: httpd.access
        fluentd-async: 'true'

  fluentd:
    image: fluent/fluentd:v1.9.1-debian-1.0
    container_name: fluentd
    volumes:
      - ./fluentd/conf:/fluentd/etc
    ports:
      - "24224:24224"
      - "24224:24224/udp"

 

 

# fluentd/conf/fluent.conf

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>


<match httpd.access>
  @type stdout
</match>

 

wsl에서 docker-compose 실행

  • cmd 실행
  • docker-compose.yml 파일 디렉토리로 이동
  • docker-compose up -d 명령어 실행
  • docker ps로 web, fluentd 컨테이너 실행 확인

 

pubsub 플러그인 설치와 fluentd conf 수정 후 재시작

  • fluentd 컨테이너에 root 접속: docker exec -it -u root fluentd /bin/bash
  • pubsub 플러그인 설치: gem install fluent-plugin-gcloud-pubsub-custom
  • fluentd.conf 에 pubsub 플러그인 내용 추가
  • fluentd 컨테이너 재시작
    • docker stop fluentd
    • docker start fluentd
  • 도커 로그에 에러가 없는지 확인
    • docker logs -f fluentd
# fluentd/conf/fluent.conf

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>


<match httpd.access>
  @type copy

  <store>
    @type gcloud_pubsub
    project 프로젝트명
    topic projects/프로젝트명/topics/주제이름
    key /fluentd/etc/서비스키.json
    flush_interval 10
    autocreate_topic false
  </store>

  <store>
    @type stdout
  </store>
</match>

 

httpd 접속 로그 생성과 pubsub 확인

  • curl http://localhost:80
  • fluentd 로그에 httpd 액세스 로깅 확인: docker logs fluentd
  • pubsub 메세지 수신 확인
    • 주제 > 구독 > 메시지 > 가져오

fluentd 로그가 pubsub에 수신

'파이프라인' 카테고리의 다른 글

nginx 로그 - fluent - pubsub - apache-beam 샘플 코드  (0) 2023.06.06
fluentd example(docker-compose)  (1) 2022.12.25
fluentd example(docker logging driver)  (0) 2022.12.16
td-agent 설정  (0) 2022.12.11
카프카 기본 개념 정리  (0) 2021.05.31