오늘은 EC2 애플리케이션의 로그를 Fluent-Bit로 수집하여 CloudWatch Log, Kinesis Data Stream으로 보내봅시다.
pip3 install flask
nohup python3 app.py &
[2023-09-09 10:15:58,913] 127.0.0.1 - - GET /v1/color/red HTTP/1.1 200
[시간] 클라이언트_IP주소 - - HTTP_메서드 URL HTTP_버전 HTTP_상태_코드
curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh
sudo systemctl start fluent-bit
sudo systemctl enable fluent-bit
sudo ln -s /opt/fluent-bit/bin/fluent-bit /usr/local/bin/fluent-bit
active (running) 이라고 뜨면 성공적으로 설치된 것입니다.
cd /etc/fluent-bit
sudo vim fluent-bit.conf
[INPUT]
Name tail
Path /home/ec2-user/app/app.log
Tag i-0dda95ee7ee5db532
Parser logNoDate
[INPUT]
Name tail
Path /home/ec2-user/app/app.log
Tag kinesis
Parser logParser
[OUTPUT]
Name cloudwatch_logs
Match i-*
region ap-northeast-2
log_group_name ec2/app/accesslog
log_stream_prefix ec2_
auto_create_group On
[OUTPUT]
Name kinesis_streams
Match kinesis
region ap-northeast-2
stream ec2-logStream
time_key time
time_key_format %Y-%m-%d %H:%M:%S
[PARSER]
Name logNoDate
Format regex
Regex ^\[(?<time>[^\]]*)\] (?<host>[^ ]*) - - (?<method>[^ ]*) (?<path>[^ ]*) (?<HTTP>[^ ]*) (?<code>[^ ]*)
[PARSER]
Name logParser
Format regex
Regex ^\[(?<time>[^\]]*)\] (?<host>[^ ]*) - - (?<method>[^ ]*) (?<path>[^ ]*) (?<HTTP>[^ ]*) (?<code>[^ ]*)
Time_Key time
Time_Format %Y-%m-%d %H:%M:%S,%L
Time_Keep Off
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource": "*"
}]
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"kinesis:PutRecords"
],
"Resource": "*"
}]
}
sudo systemctl restart fluent-bit
curl localhost:8080/v1/color/red
curl localhost:8080/v1/color/orange
curl localhost:8080/v1/color/melon
로그도 잘 기록됐습니다.
오늘의 글은 여기까지입니다. 감사합니다!