Chuyện của sys

DevOps Blog

Hướng dẫn cài đặt và cấu hình Elasticsearch trên Centos 7 May 27, 2018

Giới thiệu
Elasticsearch là một phần mềm mã nguồn mở và miễn phí  của Elastic, dựa trên Apache Lucene, là platform được sử dụng cho việc phân phối tìm kiếm và phân tích dữ liệu trong thời gian thực, được sử dụng rộng rãi do tính dễ sử dụng, tính năng mạnh mẽ và khả năng mở rộng tốt, bạn có thể sử dụng các phương thức HTTP để giao tiếp qua RESTful để thao tác với dữ liệu, thân thiện với người sử dụng cũng như các nhà phát triển.
Elasticsearch được sử dụng rộng rãi cho các dự án cá nhân cũng như là search engine chính trong các công ty lớn.
Tuy có rất nhiều bài viết, tutorial đề cập tới việc hướng dẫn cài đặt cũng như cấu hình, nhưng hi vọng bài viết này sẽ cung cấp cho bạn 1 cách chi tiết và trọn vẹn nhất để thực hiện trên bản Centos 7 và Elasticsearch phiên bản mới nhất 6.2
Các bước cài đặt
1. Cài đặt Java 8
Do Elasticsearch được viết bằng Java nên bạn cần có 1 JRE để chạy nó, ở đây mình sử dụng JDK 8 bản update 171 mới nhất.
Link download:

wget http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.tar.gz
tar -xvzf jdk-8u171-linux-x64.tar.gz
mv jdk1.8.0_171 /opt/

Cấu hình biến môi trường cho toàn bộ user:

vi /etc/profile
export JAVA_HOME=/opt/jdk1.8.0_171
export PATH=$PATH:$JAVA_HOME/bin
. /etc/profile

Verify:
java -version
java version “1.8.0_171”
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
2. Download và cài đặt Elasticsearch
 
Vào trang chủ https://www.elastic.co/downloads và download bản mới nhất hiện tại là 6.2.4

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
tar -xvzf elasticsearch-6.2.4.tar.gz
mv elasticsearch-6.2.4 /data/elastic/elasticsearch

Tạo user elasticsearch và thư mục /data/elatic/

adduser elasticsearch
chown -R elasticsearch. /data/elastic/
su – elasticsearch
cd /data/elastic/elasticsearch/

Cấu trúc thư mục như sau:

ls
bin lib logs NOTICE.txt README.textile
config LICENSE.txt modules plugins

3. Cấu hình Elasticsearch
Các file cấu hình cần chú ý:

ls config/
elasticsearch.yml jvm.options log4j2.properties

Bạn có thể sửa lại theo ý mình cho các file config này, nếu không cần thiết thì để nguyên và thực hiện sau trong phần tunning cho Elasticsearch.
Khởi chạy Elasticsearch

./bin/elasticsearch

Sample output:
[2018-05-28T02:56:03,739][INFO ][o.e.n.Node ] [] initializing …
[2018-05-28T02:56:03,846][INFO ][o.e.e.NodeEnvironment ] [zqsXYde] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [10.4gb], net total_space [17.4gb], types [rootfs]
[2018-05-28T02:56:03,846][INFO ][o.e.e.NodeEnvironment ] [zqsXYde] heap size [1007.3mb], compressed ordinary object pointers [true]
[2018-05-28T02:56:03,850][INFO ][o.e.n.Node ] node name [zqsXYde] derived from node ID [zqsXYdeEQ8ifax0sYq72gg]; set [node.name] to override
[2018-05-28T02:56:03,850][INFO ][o.e.n.Node ] version[6.2.4], pid[1624], build[ccec39f/2018-04-12T20:37:28.497551Z], OS[Linux/3.10.0-693.5.2.el7.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_171/25.171-b11]
[2018-05-28T02:56:03,851][INFO ][o.e.n.Node ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch.BOVwKawi, -XX:+HeapDumpOnOutOfMemoryError, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, -XX:+PrintTenuringDistribution, -XX:+PrintGCApplicationStoppedTime, -Xloggc:logs/gc.log, -XX:+UseGCLogFileRotation, -XX:NumberOfGCLogFiles=32, -XX:GCLogFileSize=64m, -Des.path.home=/data/elastic/elasticsearch, -Des.path.conf=/data/elastic/elasticsearch/config]
….
[2018-05-28T02:56:09,185][INFO ][o.e.t.TransportService ] [zqsXYde] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2018-05-28T02:56:09,200][WARN ][o.e.b.BootstrapChecks ] [zqsXYde] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2018-05-28T02:56:09,201][WARN ][o.e.b.BootstrapChecks ] [zqsXYde] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2018-05-28T02:56:12,290][INFO ][o.e.c.s.MasterService ] [zqsXYde] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {zqsXYde}{zqsXYdeEQ8ifax0sYq72gg}{3MVumgkjQGK04HXp82FPCA}{127.0.0.1}{127.0.0.1:9300}
[2018-05-28T02:56:12,311][INFO ][o.e.c.s.ClusterApplierService] [zqsXYde] new_master {zqsXYde}{zqsXYdeEQ8ifax0sYq72gg}{3MVumgkjQGK04HXp82FPCA}{127.0.0.1}{127.0.0.1:9300}, reason: apply cluster state (from master [master {zqsXYde}{zqsXYdeEQ8ifax0sYq72gg}{3MVumgkjQGK04HXp82FPCA}{127.0.0.1}{127.0.0.1:9300} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
[2018-05-28T02:56:12,361][INFO ][o.e.g.GatewayService ] [zqsXYde] recovered [0] indices into cluster_state
[2018-05-28T02:56:12,368][INFO ][o.e.h.n.Netty4HttpServerTransport] [zqsXYde] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2018-05-28T02:56:12,368][INFO ][o.e.n.Node ] [zqsXYde] started
Trong output trên có warning những thứ cần phải tunning cho phù hợp, mình sẽ đề cập ở bài viết sau.
4. Kiểm tra hoạt động
Ctrl + Z để thoát và bg để chạy backgroud

^Z
[1]+ Stopped ./bin/elasticsearch
bg
[1]+ ./bin/elasticsearch &

Verify process đang chạy:

ps aux |grep elastic |grep java
elastic+ 1624 11.3 66.0 3626528 1233032 pts/0 Sl 02:55 0:20 /opt/jdk1.8.0_171/bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch.BOVwKawi -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Des.path.home=/data/elastic/elasticsearch -Des.path.conf=/data/elastic/elasticsearch/config -cp /data/elastic/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch

Verify port đang lắng nghe

netstat -aonp |grep java |grep 9*00
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 1624/java off (0.00/0/0)
tcp6 0 0 ::1:9200 :::* LISTEN 1624/java off (0.00/0/0)
tcp6 0 0 127.0.0.1:9300 :::* LISTEN 1624/java off (0.00/0/0)
tcp6 0 0 ::1:9300 :::* LISTEN 1624/java off (0.00/0/0)

Bằng HTTP

curl -X GET ‘http://localhost:9200’
{
“name” : “zqsXYde”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “jvbM2A4XRhmYoDgD_tmzSg”,
“version” : {
“number” : “6.2.4”,
“build_hash” : “ccec39f”,
“build_date” : “2018-04-12T20:37:28.497551Z”,
“build_snapshot” : false,
“lucene_version” : “7.2.1”,
“minimum_wire_compatibility_version” : “5.6.0”,
“minimum_index_compatibility_version” : “5.0.0”
},
“tagline” : “You Know, for Search”
}

Testing:
Thử POST 1 document
curl -X POST -H ‘Content-Type: application/json’ ‘http://localhost:9200/chuyencuasys/elastic_note/1’ -d ‘{ “message”: “Hello World!” }’
[2018-05-28T03:04:24,379][INFO ][o.e.c.m.MetaDataCreateIndexService] [zqsXYde] [chuyencuasys] creating index, cause [auto(bulk api)], templates [], shards [5]/[1], mappings []
[2018-05-28T03:04:24,814][INFO ][o.e.c.m.MetaDataMappingService] [zqsXYde] [chuyencuasys/tpb7MoIpRSqLobimaUFVZg] create_mapping [elastic_note]
{“_index”:”chuyencuasys”,”_type”:”elastic_note”,”_id”:”1″,”_version”:1,”result”:”created”,”_shards”:{“total”:2,”successful”:1,”failed”:0},”_seq_no”:0,”_primary_term”:1}
Sau đó GET:
curl -X GET ‘http://localhost:9200/chuyencuasys/elastic_note/1’
{“_index”:”chuyencuasys”,”_type”:”elastic_note”,”_id”:”1″,”_version”:1,”found”:true,”_source”:{ “message”: “Hello World!” }}
View cho dễ nhìn hơn với pretty
curl -X GET ‘http://localhost:9200/chuyencuasys/elastic_note/1?pretty’
{
“_index” : “chuyencuasys”,
“_type” : “elastic_note”,
“_id” : “1”,
“_version” : 1,
“found” : true,
“_source” : {
“message” : “Hello World!”
}
}
Như vậy là chúng ta đã xong phần cơ bản với cài đặt và cấu hình Elasticsearch, rất đơn giản và dễ hiểu phải không nào? Phần tiếp theo mình sẽ viết thêm về tunning và cấu hình nâng cao, cũng như một số thành phần có liên quan để xây dựng 1 hệ sản phẩm Elastic.

Leave a Reply

Your email address will not be published. Required fields are marked *