갈루아의 반서재

우분투에 엘라스틱서치 설치하기

How To Install and Configure Elasticsearch on Ubuntu 14.04




Prerequisites


본 예제를 위해서는 아래의 2가지 사항이 준비되어 있어야 한다.


    A Ubuntu 14.04 Droplet
    A non-root sudo user


1
2
root@localhost:~# cut -d: -f1 /etc/passwd
root@localhost:~# adduser elastic
cs




Installing JAVA



1. Installing OpenJDK

1) update the list of available packages
2) install OpenJDK

3) To verify your JRE is installed

1
2
3
4
5
6
root@localhost:~# sudo apt-get update
root@localhost:~# sudo apt-get install openjdk-7-jre
root@localhost:~# java -version
java version "1.7.0_121"
OpenJDK Runtime Environment (IcedTea 2.6.8) (7u121-2.6.8-1ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.121-b00, mixed mode)
cs



2. Installing Java 8
더욱 향상된 자바 퍼포먼스와 호환성을 위해서 Oracle's proprietary Java (Oracle JDK 8) 를 선택할 수 있다.
1) Add the Oracle Java PPA to apt:
2) Update your apt package database:
3) Install the latest stable version of Oracle Java 8 with this command (and accept the license agreement that pops up):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@localhost:~# sudo add-apt-repository -y ppa:webupd8team/java
gpg: keyring `/tmp/tmpmxjd7rh5/secring.gpg' created
gpg: keyring `/tmp/tmpmxjd7rh5/pubring.gpg' created
gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpmxjd7rh5/trustdb.gpg: trustdb created
gpg: key EEA14886: public key "Launchpad VLC" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK
root@localhost:~# sudo apt-get update
root@localhost:~# sudo apt-get -y install oracle-java8-installer
 
#####Important########
To set Oracle JDK8 as default, install the "oracle-java8-set-default" package.
E.g.: sudo apt install oracle-java8-set-default.
W: Operation was interrupted before it could finish
 
root@localhost:~# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
cs




Downloading and Installing Elasticsearch


Elasticsearch 는 elastic.co 에서 다양한 방식으로 다운로드 가능하다. 하지만 우분투에서는 Debian package 를 사용하는 것이 가장 효과적이다. 아래와 같이 다운로드할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
# wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
--2016-12-08 12:28:08--  https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
Resolving download.elastic.co (download.elastic.co)... 107.22.240.13423.21.105.204204.236.217.108, ...
접속 download.elastic.co (download.elastic.co)|107.22.240.134|:443... 접속됨.
HTTP request sent, awaiting response... 200 OK
Length: 27302886 (26M) [application/x-debian-package]
Saving to: ‘elasticsearch-1.7.2.deb’
 
100%[===================================================================================================================================================================================================>27,302,886  6.57MB/s   in 4.1s
 
2016-12-08 12:28:14 (6.28 MB/s) - ‘elasticsearch-1.7.2.deb’ saved [27302886/27302886]
cs


   

다음과 같이 설치한다.

1
2
3
4
5
6
7
8
9
# sudo dpkg -i elasticsearch-1.7.2.deb
Selecting previously unselected package elasticsearch.
(데이터베이스 읽는중 ...현재 110265개의 파일과 디렉터리가 설치되어 있습니다.)
Preparing to unpack elasticsearch-1.7.2.deb ...
Creating elasticsearch group... OK
Creating elasticsearch user... OK
Unpacking elasticsearch (1.7.2) ...
elasticsearch (1.7.2) 설정하는 중입니다 ...
Processing triggers for ureadahead (0.100.0-16) ...
cs


설치경로                   

 /usr/share/elasticsearch

 환경설정파일 위치 

 /etc/elasticsearch

 초기 스크립트 위치

 /etc/init.d/elasticsearch.


1
2
# dpkg -s elasticsearch | grep '^Version:'
Version: 1.7.2
cs


아래와 같이 자동실행 환경을 구성한다.

1
2
3
4
5
6
7
8
9
# sudo update-rc.d elasticsearch defaults
 Adding system startup for /etc/init.d/elasticsearch ...
   /etc/rc0.d/K20elasticsearch -> ../init.d/elasticsearch
   /etc/rc1.d/K20elasticsearch -> ../init.d/elasticsearch
   /etc/rc6.d/K20elasticsearch -> ../init.d/elasticsearch
   /etc/rc2.d/S20elasticsearch -> ../init.d/elasticsearch
   /etc/rc3.d/S20elasticsearch -> ../init.d/elasticsearch
   /etc/rc4.d/S20elasticsearch -> ../init.d/elasticsearch
   /etc/rc5.d/S20elasticsearch -> ../init.d/elasticsearch
cs



Configuring Elastic


환경설정 파일은 /etc/elasticsearch 디렉토리에 위치하고 있다.


elasticsearch.ym

 서버 설정 파일. 로깅을 제외한 모든 옵션이 저장된다.

 logging.yml

 로깅에 관한 설정파일로 처음에는 손댈 필요가 없다. 일단 기본값으로 둔다. 기본적으로 로그는 /var/log/elasticsearch 에 저장된다.



1) node.name 과 cluster.name 설정

/etc/elasticsearch/elasticsearch.yml 파일을 열어 다음과 같은 식으로 설정한다.

1
2
3
4
...
cluster.name: elasticluster
node.name: "blackbriar"
...
cs


2) node.master : 서버의 역할 지정. 하나의 노드만 사용하는 경우 기본값이 true 로 둔다.

1
2
3
...
node.master: true
...
cs


3) node.data : 노드에 데이터 저장 여부. 기본값은 true 이지만 저장하지 않는 경우가 2가지 있다. 하나는 해당 노드가 dedicated master 인 경우이고, 다른 하나는 노드가 데이터를 불러오고 결과를 합산하는데만 쓰이는 경우이다. 하나의 노드만 사용하기 원한다면 기본값으로 둔다.

1
2
3
...
node.data: true
...
cs


4) index.number_of_shards 와 index.number_of_replicas

1
2
3
4
5
6
7
8
9
 
# Set the number of shards (splits) of an index (5 by default):
#
index.number_of_shards: 1
 
# Set the number of replicas (additional copies) of an index (1 by default):
#
index.number_of_replicas: 0
 
cs


5) path.data : 데이터 저장 위치. 기본값은 /var/lib/elasticsearch 이다. 설정이 끝나면 아래와 같이 시작한다.

1
2
3
# sudo service elasticsearch start
 * Starting Elasticsearch Server                                                                                                                                                                                                             (envalicia)root@localhost:~/vikander#
 
cs





Securing Elastic



1. public access 제한
1
2
3
# Set the bind address specifically (IPv4 or IPv6):
#
network.bind_host: localhost
cs


2.  disable dynamic scripts

1
script.disable_dynamic: true
cs




Testing


엘라스틱서치는 포트 9200 에서 작동한다. 아래와 같이 테스트 가능하다. 만약 아래와 같은 결과를 볼 수 있다면 엘라스틱서치가 제대로 작동하고 있는 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
r# curl -X GET 'http://localhost:9200'
{
  "status" : 200,
  "name" : "blackbriar",
  "cluster_name" : "elasticluster",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}
cs




Using Elasticsearch


엘라스틱서치를 사용하기 위해 몇 가지 데이터를 추가하자. 이미 언급했듯이 엘라스틱서치는 RESTful API 를 사용한다. 아래와 같이 첫번째 엔트리를 추가해보자.
1
2
# curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"created":true}
cs


curl 을 통해서 엘라스틱서치 서버로 HTTP POST 요청을 전송했다. 요청의 URI 는 /tutorial/helloworld/1 이다.

- tutorial 은 엘라스틱서치 데이터의 인덱스이다.

- helloworld 는 타입이다.
- 1 은 위의  인덱스와 타입을 가진 엔트리의 id 이다.


HTTP GET 요청을 통해 첫 번째 엔트리를 받을 수 있다.

1
2
# curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"found":true,"_source":{ "message""Hello World!" }}
cs


HTTP PUT 요청을 통해 현재의 엔트리를 수정할 수 있다. 예제에서는 첫번째 엔트리의 메시지를 "Hello People!"로 수정했다. 따라서 버전 번호가 2로 증가했다.

1
2
3
4
5
6
7
8
# curl -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d ' { "message": "Hello People!"  }'
{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 2,
  "created" : false
}
cs


"prettify"를 사용하여 읽기 좋게 출력해보자. 

1
2
3
4
5
6
7
8
9
# curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'
{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source": { "message""Hello People!"  }
}
cs


더욱 자세한 내용은 Elasticsearch Reference [5.0] » Document APIs 를 참고하시 바란다.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html