티스토리 뷰

안녕하세요 강정호입니다. 오늘은 엘라스틱 서치를 사용하여 서울 지하철 대쉬보드를 만들어보겠습니다.


동영상 가이드 : 

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



1-1 단계 : ElasticSearch 설치하기




ElasticSearch를 설치한 이후 설정파일에서 메모리 사이즈를 늘려줍니다.

$ cd elasticsearch 2.4.1/bin


elasticsearch.in.sh 파일에서 다음과 같이 추가합니다.(동영상 : 7분 54초)





Plugin 설치

위와 같이 엘라스틱 서치 플러그인을 설치해야 아래와 같은 화면이 나타나게됩니다.




1-2 단계 : Kibana 설치하기(동영상 : 9분 5초)


영상보다는 강의노트를 기준으로 Kibana 설치하는 것을 추천드립니다.





1-3 단계 : Kibana 플러그인 Sense 설치 ( 동영상 : 10분 27초)


Sense 설치 공식 가이드 : https://www.elastic.co/guide/en/sense/current/installing.html


1-2단계에서 설치한 Kibana의 폴더에 들어가서 다음의 명령어를 입력하면 됩니다.

$./bin/kibana plugin --install elastic/sense



명령어를 입력하면 위와 같이 Sense를 다운받습니다.




1-4 단계 : 클러스터명 설정 및 Shard Allocation 설정


1. 클러스터명 설정

$ cd elasticsearch-2.4.1 : 엘라스틱 서치 엔진 폴더로 이동

$ vi config/elasticsearch.yml : yml 파일 오픈


클러스터명 설정

cluster.name: es_test



elasticsearch.yml 파일은 위와 같이 clustername, node.name, network.host 3가지가 위와 같이 설정되어야 합니다. yml 파일은 들여쓰기가 잘못되면 파싱이 안되니 꼭 주의 하세요.



2. Shards Allocation

Shard Allocation 명령문

curl -XPUT localhost:9200/_cluster/settings -d '{
    "transient" : {
        "cluster.routing.allocation.disk.threshold_enabled" : false
    }
}'


ElasticSearch 엔진을 실행시키고 위의 쿼리문을 쳐서 엔터 누르면 다음과 같이 샤드가 할당이 됩니다.





1-5 단계 : ElasticSearch, Kibana  실행


1) ElasticSearch 실행 : $ bin/elastic &

백그라운드에서 엘라스틱 서치를 실행합니다.


2) Kibana 실행 : $ nohup bin/kibana &

nohup을 이용해서 실행하여야 no connection warning이 뜨지 않습니다.


3) Kibana Sense에 접속 : http://개별 서버 IP 주소:5601/app/sense






2-1단계 : seoul-metro-2014 인덱스 생성하기(동영상 : 12분 8초)


설명서 링크 : https://github.com/kimjmin/elastic-demo/blob/master/demos/seoul-metro-2014/doc/20151025_EMOCON.md


아래 코드는 인덱스 seoul-metro-2014를 생성하는 것입니다. 아래와 같이 Sense에 입력하여 인덱스를 생성하세요.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PUT seoul-metro-2014
{
    "mappings" : {
      "seoul-metro" : {
        "properties" : {
          "time_slot" : { "type" : "date" },
          "line_num" : { "type" : "string""index" : "not_analyzed" },
          "line_num_en" : { "type" : "string""index" : "not_analyzed" },
          "station_name" : { "type" : "string""index" : "not_analyzed" },
          "station_name_kr" : { "type" : "string""index" : "not_analyzed" },
          "station_name_en" : { "type" : "string""index" : "not_analyzed" },
          "station_name_chc" : { "type" : "string""index" : "not_analyzed" },
          "station_name_ch" : { "type" : "string""index" : "not_analyzed" },
          "station_name_jp" : { "type" : "string""index" : "not_analyzed" },
          "station_geo" : { "type" : "geo_point" },
          "people_in" : { "type" : "integer" },
          "people_out" : { "type" : "integer" }
        }
      }
    }
  }
 
cs

위와 같이 입력하고 인덱스를 생성합니다.



2-2 단계 : logstash 설치하기


$ wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.1.tar.gz

: 이 명령어를 입력하면 logstash 압축파일을 다운 받습니다.


$ tar xvf logstash-6.5.1.tar.gz

: logstash 압축파일을 압축 풀기




2-3 단계 : Filebeat 설치하기(동영상 : 14분)


$ wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.1-linux-x86_64.tar.gz

: Filebeat 압축파일 다운로드 받기


$ tar xvf filebeat-6.5.1-linux-x86_64.tar.gz 

: File beat 압축 풀기



2-4 단계 : 서울 메트로 데이터 수작업으로 정리(동영상 : 16분~30분)


이미 정리되어 있는 파일이 강의 노트에 올려져 있다. 그래서 이 단계는 스킵하고 바로 2-4 단계로 넘어가면 됩니다.

http://54.214.227.60/elasticsearch/sample/seoul-metro-2014-source.zip




2-5 단계 : 서울메트로 데이터 zip 파일 압축해제


1) $ mkdir source : 데이터 source 폴더를 생성한다. 데이터가 많기 때문에 따로 source 폴더를 생성해서 거기에 데이터를 보관합니다.

2) $ cd source : source 폴더로 이동

3) $ wget http://54.214.227.60/elasticsearch/sample/seoul-metro-2014-source.zip

: 지하철 데이터를 받는다.

4) $ unzip seoul-metro-2014-source.zip

: 이 명령어를 치면 압축이 다음과 같이 쫙 풀립니다~





3-1 단계 : FileBeat과 Logstash 설정(동영상 : 30분 ~ 


1. Logstash 설정

1) $ cd logstash-6.5.1 : logstash 폴더로 이동

2) $ touch logstash.conf : logstash config 파일 생성

3) $ vi logstash.conf



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
input {
  beats {
    codec => json
    port => 5044
  }
}
 
output{
 elasticsearch{
    hosts => ["127.0.0.1"]
    index => "seoul-metro-2014"
    document_type => "seoul-metro"
  }
}
cs
cs

[코드 설명]

- input codec => json : .log 파일에 있는 데이터가 json 형식의 데이터라는 것을 명시하는 코드

- port => 5044 : logstash의 포트 번호

- filter : 데이터의 불필요한 부분을 제거하고 필요한 부분만 넣기 위한 설정


fileter가 없는 것으로 해도 무방. 만약에 오류가 생긴다면 이것으로 해보기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
input {
  beats {
    codec => json
    port => 5044
  }
}
 
 
output{
 elasticsearch{
    hosts => ["127.0.0.1"]
    index => "seoul-metro-2014"
    document_type => "seoul-metro"
  }
}
 
cs



2. Filebeat 설정

1) $ cd filebeat-6.5.1-linux-x86_64

2) $ vi filebeat.yml

3) $ pwd : pwd 명령어를 사용해서 이전에 압축을 해제한 지하철 데이터 소스의 폴더 위치를 찾아서 filebeat.yml에 입력해줘야 한다.

4) vi filebeat.yml에 위에서 나온 data 폴더 경로를 다음과 같이 입력한다



4) elasticsearch를 주석처리하고 logstash를 활성화 시키는 설정(동영상 34분 45초)

: elasticsearch로 데이터를 바로 넣는 것이 아니고 logstash를 이용해서 넣을 것이기 때문에

filebeat.yml에서 다음과 같이 변경한다.



위와 같이 elasticsearch에는 주석처리를 해주고, logstash에는 주석처리를 해제한다.



3. logstash, filebeat 실행 명령어
1):logstash-2.4.1 $ bin/logstash -f logstash.conf
- 로그스태쉬 컨피그 파일을 실행시킨다.

2) :filebeat-1.2.1 $ ./filebeat -c filebeat.yml 


자 이제 모든 준비는 끝났습니다. 이제 다음의 명령어를 순서대로 입력하여 데이터를 넣으면 됩니다.

1. $ bin/elasticsearch &
2. $ bin/kibana &
3. $ bin/logstash -f logstash.conf &
4. $ ./filebeat -c filebeat.yml &






[이슈사항]



1. 로그스태시 실행하니 에러가 발생. 메모리가 없다

리눅스 스왑으로 해결 : https://blog.lael.be/post/6810


메모리관련 이슈는 https://blog.lael.be/post/6810 이 포스팅에서 리눅스 스왑을 이용해서 해결


명령어 순서

1) $ sudo -i : root 계정으로 변경

2) #dd if=/dev/zero of=/root/swapfile bs=1024 count=2000000

3) # chmod 600 /root/swapfile

4) # mkswap /root/swapfile 

5) # swapon /root/swapfile

6) # free -m : 메모리 확인



2. logstash conf 파일 실행하고 멈췄다

이것을 백그라운드로 실행시켜야 다른 작업이 가능하다. 그러니까 logstash 실행할 때 명령어 끝에 &를 붙여서 실행하기.


3. filebeat.yml을 실행해도 log 파일이 들어가지 않는이유?

내가 바꾼 설정을 활성화 시키는 것이 해결방법이었다.


원래는 enabled=false 로 디폴트 설정이 되어있다. 하지만 내가 /home/ec2-user/source/data/*.log 파일을 읽어들이기 위해 설정을 바꿨기 때문에 그 설정을 활성화 시키기 위해서는 enabled=true 로 변경해야 한다.







댓글