39
Fluentd with MySQL V1.0 작성일자 : 2015.08 작 성 자 : 이 이 구 1

Fluentd with MySQL

Embed Size (px)

Citation preview

Page 1: Fluentd with MySQL

Fluentd with MySQL V1.0

작성일자 : 2015.08

작 성 자 : 이 이 구

1

Page 2: Fluentd with MySQL

2

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 3: Fluentd with MySQL

3

Intro

Page 4: Fluentd with MySQL

4

Intro

Fluentd is a fully free and fully open-source log collector that instantly enables you to have a ‘Log Everything’

architecture with 125+ types of systems.

http://docs.fluentd.org

Page 6: Fluentd with MySQL

6

Intro

Input plugin

Input plugin 은 외부로부터 이벤트를 받아오거나 외부의 파일을 읽어서 이벤트를 만들어 주는 역할을 한다.

fluentd 이외의 다른 log aggregator들이 가장 취약한 부분으로, 반대로 말하면 fluentd의 최고 장점이 되는 부분

이기도 하다.

fluentd의 경우는 이미 많은 plugin들이 만들어져 있어서 필요한 대부분의 plugin을 찾을 수 있고,

찾지 못하더라도 쉽게 plugin을 만들 수 있다.

Reference : http://blog.seulgi.kim/2014/04/fluentd-pluggable-log-collector.html

Page 7: Fluentd with MySQL

7

Intro

Output plugin

Output plugin 은 읽은 이벤트를 외부 저장소 또는 외부 서비스로 전달하는 역할을 한다

입력은 input plugin 을 통해서 들어와 engine을 거쳐서 buffer plugin을 거치지 않고 output plugin으로 나간다.

buffer는 engine에서 사용되는 것이 아니라 output plugin 내부에서 사용된다.

왜냐하면, output의 종류에 따라서 buffer가 필요하지 않은 경우가 있어, buffer의 사용 여부를 output plugin이 결정해야 하기 때문이다.

buffer plugin 을 사용하지 않는 output plugin을 non-buffered output plugin 이라고 부르며, 대표적인 예가 out_null과 out_stdout

plugin이다. out_null의 경우 들어오는 입력을 전부 버리는 plugin이고, out_stdout은 들어오는 입력을 커맨드 창에 띄워주는 plugin이다.

또 다른 경우는 out_copy 다. 이 plugin은 하나의 fluentd로 들어온 event를 2개 이상의 output으로 보낼 때 쓰인다.

따라서 뒤에 다른 output plugin이 있고, 이 output plugin이 적절한 buffer를 사용하기 때문에 자체적으로 buffer를 이용할 이유가 없다.

평범하게 buffer plugin을 사용하는 plugin들은 buffered output plugin이라고 부르는데 이 중 일부는 time sliced output plugin이라고

불린다. time sliced output plugin은 buffer를 사용하지만, chunk의 key로 tag가 아닌 시간을 사용한다는 것만이 다르다.

Page 8: Fluentd with MySQL

8

Intro

Buffer plugin 1. Output 을 효율적으로 내보내는 기능

log aggregator는 실시간으로 로그를 모아주지만, 모은 로그를 바로 바로 output 으로 보낼 이유는 없다. 그래서 fluentd 를 비롯한 대부분의 aggregator는 서버에서 일정량의 로그를 모았다가 처리하도록 해준다. fluentd에서는 이 단위를 chunk라고 부르며, chunk는 log의 tag 별로 분류되어 저장된다. output plugin은 우선 chunk를 queue에 집어넣지 않고 들어오는 log를 chunk에 적는다. 그러다가 chunk의 크기가 일정 이상 커지거나, chunk가 생긴지 일정 시간 이상 지나면 queue에 들어간다. chunk는 tag를 key로 하므로 buffer에 들어가지 않고 있는 chunk가 한 개 이상일 수도 있다. queue의 크기를 일정 이상 키우지 않기 위해 queue에 chunk를 집어넣을 때, queue에서 chunk를 1개 빼서 output으로 내보낸다.

2. Collector 서버 장애시 log 유실 최소화하는 기능

Buffer 를 사용한다고 해도 메모리가 무한한 것이 아니므로 서버에 오랫동안 문제 있으면 버려지는 데이터가 생기게 된다. fluentd 는 재시도를 하고 그래도 안 되면 버리는 것을 정책으로 삼고 있다. 즉, output으로 나가야 하는 data가 나가지 못했을 때 일정 시간이 지난 후 다시 시도하며, 그래도 실패한다면, 기다렸던 시간의 2배만큼 더 기다리고 다시 시도하기를 반복한다. 일정 횟수를 기다려도 보내는 것에 실패하면 이 데이터는 다음으로 보내지지 않고 버려진다. (재시도시간: retry_wait, 재시도횟수: retry_limit로 설정) fluentd 자체가 문제가 생겨서 꺼지는 경우도 있는데, 이것도 buffer의 plugin으로 원하는 종류를 써서 해결할 수 있다. 기본적으로 fluentd가 buffer에 사용하는 것은 buf_memory 라는 plugin으로 chunk를 memory에 기록하는 plugin 이다. 하지만 서버가 죽었다 살아날 때도 보장하고 싶다면 buf_file plugin을 이용하면 된다. buf_file plugin을 사용하면 chunk의 내용을 file에 보관해 주기 때문에 서버가 다시 켜질 때 file을 읽어와 buffer 내용을 복구해준다. file에 쓰는 만큼 속도가 느려지지만, 안정성이 증가하기도 하고, 사용할 수 있는 buffer의 크기도 커진다.

Page 9: Fluentd with MySQL

9

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 11: Fluentd with MySQL

11

Install

"수집대상(Client)" 서버로 접속하여 Fluentd 설치

http://docs.fluentd.org/v0.12/categories/installation

# sudo su -

cd /usr1/program/

curl -L https://td-toolbelt.herokuapp.com/sh/install-redhat-td-agent2.sh | sh

# /etc/init.d/td-agent start

/etc/init.d/td-agent status

/etc/init.d/td-agent restart

/etc/init.d/td-agent stop

# cat /etc/td-agent/td-agent.conf

# curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test

tail -f /var/log/td-agent/td-agent.log

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Page 12: Fluentd with MySQL

12

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 13: Fluentd with MySQL

13

Step.1 수집대상 서버로 접속하여 Fluentd plugin 설치

https://github.com/yuku-t/fluent-plugin-mysqlslowquery

https://github.com/tagomoris/fluent-plugin-mysql

https://github.com/toyama0919/fluent-plugin-mysql-bulk

# yum -y install ruby-rdoc ruby-devel rubygems

find / -name fluent-gem

/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysqlslowquery

/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysql-bulk

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.1 MySQL slowquery logging

Page 14: Fluentd with MySQL

14

Test.1 MySQL slowquery logging

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.2 수집기(Collector) 서버로 접속하여 Log 테이블 생성

$ mysql -u root -p

use test;

drop table if exists test.t_mysql_slow;

create table test.t_mysql_slow (

log_date datetime default current_timestamp

, user varchar(100)

, host varchar(100)

, host_ip varchar(20)

, query_time decimal(20,10)

, lock_time decimal(20,10)

, rows_sent bigint

, rows_examined bigint

, sql_text varchar(10000)

);

Page 15: Fluentd with MySQL

15

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정

$ sudo vi /etc/td-agent/td-agent.conf

<source> type mysql_slow_query path /data/mysql/ADMIN/slowquery.log tag ec-ldb-m2.mysql_slow </source> <match ec-ldb-m2.mysql_slow> type copy <store> type stdout </store> <store> type mysql_bulk host ec-ldb-m2 port 19336 database test username root password testpasswd12#$ column_names user,host,host_ip,query_time,lock_time,rows_sent,rows_examined,sql_text key_names user,host,host_ip,query_time,lock_time,rows_sent,rows_examined,sql_text table t_mysql_slow flush_interval 5s </store> </match>

Test.1 MySQL slowquery logging

Page 16: Fluentd with MySQL

16

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.4 td-agent 재시작 및 부하쿼리 수행

$ sudo /etc/init.d/td-agent stop

sudo /etc/init.d/td-agent start

tail -f /var/log/td-agent/td-agent.log

Test.1 MySQL slowquery logging

$ mysql -u root -p

select sleep(1);

select sleep(1);

Page 17: Fluentd with MySQL

17

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.5 수집기(Collector) 서버에서 로그 확인

$ mysql -u root –p

select sleep(1);

select sleep(1);

Test.1 MySQL slowquery logging

$ mysql -u root -p

select * from t_mysql_slow;

Page 18: Fluentd with MySQL

18

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 20: Fluentd with MySQL

20

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.2 MySQL process list logging

Step.2 수집기(Collector) 서버로 접속하여 Log 테이블 생성

$ mysql -u root -p

use test;

drop table if exists test.t_mysql_process;

create table test.t_mysql_process (

log_date datetime default current_timestamp

, hostname varchar(100)

, id bigint

, user varchar(100)

, host varchar(100)

, db varchar(64)

, command varchar(50)

, duration_time bigint

, state varchar(4000)

, info varchar(10000)

);

Page 21: Fluentd with MySQL

21

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.2 MySQL process list logging

Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정

$ sudo vi /etc/td-agent/td-agent.conf

<source> type mysql_query host ec-ldb-s2 port 19336 database test username root password 433dlxjsjf12!@! interval 1m tag ec-ldb-s2.processlist query show full processlist; record_hostname yes nest_result no nest_key data #row_count yes #row_count_key row_count </source> <match ec-ldb-s2.processlist> type rename_key remove_tag_prefix ec-ldb-s2. append_tag ec-ldb-s2 rename_rule1 Time duration_time </match>

<match processlist.ec-ldb-s2> type copy <store> type stdout </store> <store> type mysql_bulk host ec-ldb-m2 port 19336 database test username root password testpasswd12#$ column_names hostname,Id,User,Host,db,Command,State,Info,duration_time key_names hostname,Id,User,Host,db,Command,State,Info,duration_time table t_mysql_process flush_interval 5s </store>

</match>

Page 22: Fluentd with MySQL

22

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.2 MySQL process list logging

Step.4 td-agent 재시작

$ sudo /etc/init.d/td-agent stop

sudo /etc/init.d/td-agent start

tail -f /var/log/td-agent/td-agent.log

Page 23: Fluentd with MySQL

23

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.2 MySQL process list logging

Step.5 수집기(Collector) 서버에서 로그 확인

$ mysql -u root –p

use test;

select * from t_mysql_process;

Page 24: Fluentd with MySQL

24

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 25: Fluentd with MySQL

25

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.1 수집기(Collector) 서버로 접속하여 Log 테이블 생성

$ mysql -u root -p

use test;

drop table if exists test.log_game_play;

create table test.log_game_play (

log_date datetime default current_timestamp

, useridx bigint

, play_time bigint

, char_type varchar(1)

, result varchar(1)

);

alter table log_game_play add primary key(log_date,useridx);

Test.3 Game Log Collect

Page 26: Fluentd with MySQL

26

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.2 수집대상 서버로 접속하여 Log 테이블 생성 및 데이터 입력

$ mysql -u root -p

use test; drop table if exists test.log_game_play; create table test.log_game_play ( log_date datetime default current_timestamp , useridx bigint , play_time bigint , char_type varchar(1) , result varchar(1) ); alter table log_game_play add primary key(log_date,useridx);

Test.3 Game Log Collect

set @i=0; insert ignore into test.log_game_play select date_sub(now(), interval @i:=@i+1 minute) as log_date , @i , rand()*10000 , mod(@i,10) , mod(@i,3) from mysql.proc limit 100; select date_format(log_date, '%Y%m%d%h') as dt, count(*) from test.log_game_play group by date_format(log_date, '%Y%m%d%h'); select * from test.log_game_play where log_date between date_sub(now(), interval 10 minute) and now();

Page 27: Fluentd with MySQL

27

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정

$ sudo vi /etc/td-agent/td-agent.conf

<source> type mysql_query host ec-ldb-s2 port 19336 database test username root password 433dlxjsjf12!@! interval 10s tag ec-ldb-s2.log_game_play query select * from test.log_game_play where log_date between date_sub(now(), interval 10 minute) and now(); record_hostname yes nest_result no nest_key data #row_count yes #row_count_key row_count </source>

<match ec-ldb-s2.log_game_play> type copy <store> type stdout </store> <store> type mysql_bulk host ec-ldb-m2 port 19336 database test username root password 433dlxjsjf12!@! column_names log_date,useridx,play_time,char_type,result key_names log_date,useridx,play_time,char_type,result table log_game_play on_duplicate_key_update true on_duplicate_update_keys log_date,useridx flush_interval 15s </store> </match>

Test.3 Game Log Collect

Page 28: Fluentd with MySQL

28

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.4 td-agent 재시작

$ sudo /etc/init.d/td-agent stop

sudo /etc/init.d/td-agent start

tail -f /var/log/td-agent/td-agent.log

Test.3 Game Log Collect

Page 29: Fluentd with MySQL

29

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.5 수집기(Collector) 서버에서 로그 확인

$ mysql -u root –p

use test;

select * from test.log_game_play;

Test.3 Game Log Collect

Page 30: Fluentd with MySQL

30

Index

1. Intro

2. Install

3. Test

MySQL slowquery logging

MySQL process list logging

Game Log Data Collect

Log Server 구축

4. QnA

Page 31: Fluentd with MySQL

31

Step.1 수집대상 서버로 접속하여 Fluentd plugin 설치

https://github.com/tagomoris/fluent-plugin-mysql

# yum -y install ruby-rdoc ruby-devel rubygems

find / -name fluent-gem

/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysql

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Test.4 Log Server 구축

Client

Client

Client

Server

Server

HA P

roxy (

L4)

Page 32: Fluentd with MySQL

32

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.2 수집기(Collector) 서버로 접속하여 Log 테이블 생성

$ mysql -u root -p

use test;

drop table if exists test.t_log_connect;

create table test.t_log_connect (

log_date datetime default current_timestamp

, jsondata text

);

drop table if exists test.t_log_money;

create table test.t_log_money (

log_date datetime default current_timestamp

, jsondata text

);

Test.4 Log Server 구축

Page 33: Fluentd with MySQL

33

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정

$ sudo vi /etc/td-agent/td-agent.conf

<source> type http port 8888 body_size_limit 1mb keepalive_timeout 10s </source> <match ec-ldb-s2.t_log_connect> type copy <store> type stdout </store> <store> type mysql host ec-ldb-m2 port 19336 database test username root password testpasswd12#$ table t_log_connect columns jsondata format json flush_interval 5s </store> </match>

<match ec-ldb-s2.t_log_money> type copy <store> type stdout </store> <store> type mysql host ec-ldb-m2 port 19336 database test username root password testpasswd12#$ table t_log_money columns jsondata format json flush_interval 5s </store> </match>

Test.4 Log Server 구축

Page 34: Fluentd with MySQL

34

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.4 td-agent 재시작

$ sudo /etc/init.d/td-agent stop

sudo /etc/init.d/td-agent start

-- 로그 발생

curl -X POST -d 'json={"ver":"1.0","action":"login","user":1}' http://localhost:8888/ec-ldb-s2.t_log_connect

curl -X POST -d 'json={"ver":"1.0","action":"login","user":1}' http://localhost:8888/ec-ldb-s2.t_log_money

tail -f /var/log/td-agent/td-agent.log

Test.4 Log Server 구축

Page 35: Fluentd with MySQL

35

Collector [ec-ldb-m2]

Service DB [ec-ldb-s2]

Step.5 수집기(Collector) 서버에서 로그 확인

$ mysql -u root –p

use test;

select count(*) from test.t_log_money;

select count(*) from test.t_log_connect;

Test.4 Log Server 구축

Page 36: Fluentd with MySQL

36

Test.0 활용방안

Page 37: Fluentd with MySQL

37

Test.0 활용방안

BackupDB

Service.1

Service.2

Service.3

1. federated engine or Export/Import 을 이용하여

매월 3개월 이전 Log Data 를 백업받은 후 삭제함

1. 특정기간 이전의 과거 데이터를 10분단위로 백업

받음으로서 불필요한 월 배치작업 제거

2. 주요 지표 데이터에 대해서는 ETL 도 대체할 수

있지 않을까?

Fluentd Agent Pool

Page 38: Fluentd with MySQL

38

Reference

RubyConf 2014 - Build the Unified Logging Layer with Fluentd and Ruby by Kiyoto Tamura

https://www.youtube.com/watch?v=sIVGsQgMHIo

Page 39: Fluentd with MySQL

39