본문 바로가기

인프라,데이터/Fluentd, Fluent Bit

Fluent Bit에 SQS Output 연결하기(feat. Golang)

Fluent Bit에는 공식 SQS Output 플러그인이 없다.

https://github.com/PayU/fluentBit-sqs-plugin

 

GitHub - PayU/fluentBit-sqs-plugin: FluntBit custom output plugin which allows sending messages to AWS-SQS.

FluntBit custom output plugin which allows sending messages to AWS-SQS. - GitHub - PayU/fluentBit-sqs-plugin: FluntBit custom output plugin which allows sending messages to AWS-SQS.

github.com

하여 위의 플러그인을 이용한다.

"Fluent Bit SQS Output Plugin"을 검색하면 맨 처음으로 뜨는 깃허브지만, Star 수가 무려 4개에 달한다... ⭐🌟

 

위 깃허브에는 Dockerfile로 설치하는 예시를 들었는데, 

나는 Centos 7 환경의 EC2 instance 안에다 설치할거라 Golang 을 설치하는 편을 택했다.

Golang 설치는 https://linuxize.com/post/how-to-install-go-on-centos-7/ 를 참고하시길,, 

 

Golang을 설치해 준 다음에는, 

~/go/src/ 폴더 하위에 hello 라는 디렉토리를 만들고

다음의 코드를 hello.go 라는 이름으로 저장한다.

package main

import "fmt"

func main() {
    fmt.Printf("Hello, World\n")
}

 

go 파일이 있는 디렉토리에서 다음의 build 명령어를 실행하고, 

go build

 

다음 명령어를 실행하면 파일이 실행된다!

./hello
# Hello, World 라는 결과물이 출력된다.

 


 

다시 SQS Ouput 플러그인 설치로 돌아가자. 

SQS Output 플러그인을 설치하려면 git, gcc도 설치해줘야 한다.

sudo yum install git
yum install gcc

 

그리고 플러그인에 필요한 레파지토리를 go get 한다.

go get github.com/aws/aws-sdk-go/aws
go get github.com/fluent/fluent-bit-go/output
go get github.com/jmespath/go-jmespath

 

~/go/src 등 원하는 경로 하위에 디렉토리를 만들고, 

다음의 코드를 out_sqs.go 라는 이름으로 저장한다.

https://github.com/PayU/fluentBit-sqs-plugin/blob/master/out_sqs.go

 

GitHub - PayU/fluentBit-sqs-plugin: FluntBit custom output plugin which allows sending messages to AWS-SQS.

FluntBit custom output plugin which allows sending messages to AWS-SQS. - GitHub - PayU/fluentBit-sqs-plugin: FluntBit custom output plugin which allows sending messages to AWS-SQS.

github.com

go build -buildmode=c-shared -o out_sqs.so out_sqs.go

명령어로 out_sqs.go 파일을 빌드한다.

그리고 fluent-bit 이 설치된 폴더의 plugins.conf 파일에 

[PLUGINS]
    Path /root/go/src/out_sqs/out_sqs.so

와 같이 플러그인의 위치를 기재해준다.

[OUTPUT]
    Name sqs
    Match *
    QueueUrl https://sqs.ap-northeast-2.amazonaws.com/2xxxx/my-queue
    QueueRegion ap-northeast-2

fluent-bit.conf 파일에는 위와 같이 OUTPUT을 적어주면 끝!

 

fluent-bit을 다시 시작하면 정상적으로 sqs로 로그가 전송된다. 

sudo service fluent-bit restart

 

혹시나 잘 안될 경우엔 

journalctl -u fluent-bit.service

로 디버깅하면 된다..!