Aws

Linux Server에서 OpenSearch로 로그 보내기

wngnl05 2024. 12. 27. 15:03

OpenSerch 생성하기 참고

2024.04.17 - [Aws] - EKS Fluent-Bit to OpenSearch

OpenSearch 설정

Security 선택하기

Create new role

Ec2에 연결된 역활 이름

역활 넣어주기

역활 넣어주기

Mapped users를 누르고

EC2 역활의 ARN 작성하고 생성

Python app을 Ec2에서 nohup으로 실행

Install Fluentbit

curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh
sudo systemctl enable --now fluent-bit
sudo ln -s /opt/fluent-bit/bin/fluent-bit /bin/

Fluentbit 설정 파일

sudo vi /etc/fluent-bit/fluent-bit.conf

밑에 있는 [INPUT], [OUTPUT] 삭제하고 아래 코드 넣기

더보기
[INPUT]
    Name              tail
    Path              <로그 파일 경로>
    Read_From_Head    true
    Parser            test
[OUTPUT]
    Name              es
    Match             *
    Host              <OpenSearch 앤드포인트 https:// 빼고>
    Port              443
    TLS               On
    AWS_Auth          On
    AWS_Region        ap-northeast-2
    Index             app-log
    Replace_Dots      On
    Suppress_Type_Name On

Fluentbit Parser 설정하기

sudo vi  /etc/fluent-bit/parsers.conf

가장 위에 넣어주기

[PARSER]
    Name test
    Format regex
    Regex  ^(?<clientip>[^*]*) - \[(?<time>[^\]]*)\] "(?<method>[^ ]*) (?<path>[^ ]*) (?<protocol>[^ ]*)" (?<responsecode>[^ ]*) "(?<useragent>[^ ]*)"

Parser.conf 는 수정하면 다시 재시작 해주어야 함

sudo systemctl restart fluent-bit

테스트 해보고 백그라운드에서 실행

fluent-bit -c /etc/fluent-bit/fluent-bit.conf

Run Fluentbit in BackGround

fluent-bit -c /etc/fluent-bit/fluent-bit.conf &

 

 

 

green open app-log dwxHtcnyQcGsMwhGB0FMYw 5 1 2 0 34kb 17kb

OPENSEARCH_ENDPOINT=$(aws opensearch describe-domain --domain-name wsi-opensearch | jq -r '.DomainStatus.Endpoint')
   curl -s -u admin:Password01! "https://$OPENSEARCH_ENDPOINT/_cat/indices?index=app-log"

 

"clientip", "method", "path", "protocol", "responsecode", "time", "useragent"

OPENSEARCH_ENDPOINT=$(aws opensearch describe-domain --domain-name wsi-opensearch | jq -r '.DomainStatus.Endpoint')
curl -s -u admin:Password01! https://$OPENSEARCH_ENDPOINT/app-log | jq '.["app-log"].mappings.properties | keys[]'