簡介:
Elasticsearch 是一個實時的分布式搜索和分析引擎。它可以幫助你用前所未有的速度去處理大規模數據、它可以用於全文搜索,結構化搜索以及分析。
分布式實時文件存儲,並將每一個字段都編入索引,使其可以被搜索。
實時分析的分布式搜索引擎。
可以擴展到上百台服務器,處理PB級別的結構化或非結構化數據。
一、安裝 Elasticsearch
shell > yum -y install java # 需要 java 環境 shell > java -version openjdk version "1.8.0_101" OpenJDK Runtime Environment (build 1.8.0_101-b13) OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode) shell > useradd elast # 用於啟動 elasticsearch shell > cd /usr/local/src shell > wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.0/elasticsearch-2.4.0.tar.gz shell > tar zxf elasticsearch-2.4.0.tar.gz shell > mv elasticsearch-2.4.0 ../elasticsearch-2.4.0 shell > chown -R elast.elast /usr/local/elasticsearch
二、配置 Elasticsearch
shell > vim /usr/local/elasticsearch/conf/elasticsearch.yml cluster.name: es # 集群名稱 node.name: node-1 # 節點名稱,默認第一個啟動的節點為集群 Master path.data: /data/esearch/data # 索引存放路徑 path.logs: /data/esearch/logs # 日志 network.host: 192.168.1.32 # 監聽地址 http.port: 9200 # 端口 shell > mkdir /data/esearch/data shell > mkdir /data/esearch/logs shell > chown -R elast.elast /data/esearch/data shell > chown -R elast.elast /data/esearch/logs shell > vim /usr/local/elasticsearch/bin/elasticsearch.in.sh if [ "x$ES_MIN_MEM" = "x" ]; then ES_MIN_MEM=256m fi if [ "x$ES_MAX_MEM" = "x" ]; then ES_MAX_MEM=2g fi
# 設置一下允許 elasticsearch 使用的最大/最小內存
三、啟動 Elasticsearch
shell > su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch -d" # 以 elast 用戶身份啟動,否則報錯 shell > echo 'su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch -d"' >> /etc/rc.local # 加入開機啟動 shell > killall java # 因為我的服務器上只跑一個 java 程序,就是 elasticsearch ,所以我這樣關閉 shell > netstat -lnpt | grep java tcp 0 0 :::9200 :::* LISTEN 2076/java tcp 0 0 :::9300 :::* LISTEN 2076/java shell > tree /data/esearch/ # 數據(索引)目錄、日志目錄 /data/esearch/ ├── data │ └── my-esearch │ └── nodes │ └── 0 │ ├── node.lock │ └── _state │ └── global-0.st └── logs ├── my-esearch_deprecation.log ├── my-esearch_index_indexing_slowlog.log ├── my-esearch_index_search_slowlog.log └── my-esearch.log shell >ps aux | grep java elast 2107 3.7 6.6 5757256 259152 ? Sl 22:52 0:10 /usr/bin/java -Xms256m -Xmx2g -Djava.awt.headless=true\ -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly\ -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/local/elasticsearch-2.4.0\ -cp /usr/local/elasticsearch-2.4.0/lib/elasticsearch-2.4.0.jar:/usr/local/elasticsearch-2.4.0/lib/* org.elasticsearch.bootstrap.Elasticsearch start -d
# 可以看到啟動參數中有前面內存的設定 -Xms256m -Xmx2g
四、測試 Elasticsearch
shell > curl -X GET localhost:9200 # 返回 elasticsearch 基本信息,節點名稱、版本信息等 { "name" : "node-1", "cluster_name" : "es", "version" : { "number" : "2.4.0", "build_hash" : "ce9f0c7394dee074091dd1bc4e9469251181fc55", "build_timestamp" : "2016-08-29T09:14:17Z", "build_snapshot" : false, "lucene_version" : "5.5.2" }, "tagline" : "You Know, for Search" } shell > curl -X GET localhost:9200/_cat # 支持的各指令 =^.^= /_cat/allocation /_cat/shards /_cat/shards/{index} /_cat/master /_cat/nodes /_cat/indices /_cat/indices/{index} /_cat/segments /_cat/segments/{index} /_cat/count /_cat/count/{index} /_cat/recovery /_cat/recovery/{index} /_cat/health /_cat/pending_tasks /_cat/aliases /_cat/aliases/{alias} /_cat/thread_pool /_cat/plugins /_cat/fielddata /_cat/fielddata/{fields} /_cat/nodeattrs /_cat/repositories /_cat/snapshots/{repository} shell > curl -X GET 127.0.0.1:9200/_cat/master?v # 查看 Master 信息,加 ?v 可以顯示表頭 id host ip node 7EkYmW84S8aELlXvieV5uw 192.168.1.32 192.168.1.32 node-1 shell > curl -X GET localhost:9200/_cat/nodes?v # 集群節點信息 host ip heap.percent ram.percent load node.role master name 192.168.1.32 192.168.1.32 1 20 0.12 d * node-1
五、Elasticsearch 插件安裝
shell > /usr/local/elasticsearch/bin/plugin list # 查看 Elasticsearch 安裝了哪些插件,結果一個也沒有 Installed plugins in /usr/local/elasticsearch/plugins: - No plugin detected
1、head 集群管理插件
shell > /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head # 安裝一個插件,用於集群管理 # 一些插件地址: # https://github.com/mobz/elasticsearch-head # 可直接安裝 # https://github.com/medcl/elasticsearch-analysis-ik # 需要自己打包 mvn package shell > /usr/local/elasticsearch/bin/plugin list # 再次查看 Installed plugins in /usr/local/elasticsearch/plugins: - head ## http://192.168.1.32:9200/_plugin/head 瀏覽器訪問 head 集群管理插件
2、中文分詞插件
# 這里先留空一個知識點:maven 的安裝跟怎么給中文分詞器編譯打包。
shell > cd /usr/local/src/ shell > wget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip # 下載中文分詞插件 shell > unzip elasticsearch-analysis-ik-master.zip shell > cd elasticsearch-analysis-ik-master shell > mvn package # 將分詞器源碼編譯成 jar 包,過程有點長 ( 打包失敗的,可以加我 QQ:25152069 ,我有打好的包 ) shell > cp target/releases/elasticsearch-analysis-ik-1.10.0.zip /usr/local/elasticsearch-2.4.0/plugins/ik/ # ik 目錄不存在就創建一下 shell > cd /usr/local/elasticsearch-2.4.0/plugins/ik shell > unzip elasticsearch-analysis-ik-1.10.0.zip shell > killall java shell > su - elast -c "/usr/local/elasticsearch-2.4.0/bin/elasticsearch" shell > /usr/local/elasticsearch-2.4.0/bin/plugin list # 再次查看插件已經安裝成功 Installed plugins in /usr/local/elasticsearch-2.4.0/plugins: - head - ik
六、配置 Elasticsearch 集群
1、node1 ( 默認的 Master )
shell > grep -vP '^#|^$' /usr/local/elasticsearch-2.4.0/config/elasticsearch.yml cluster.name: es # 集群名稱必須一致,且同一局域網內可以自動組成集群 node.name: node-1 # 節點名稱唯一 path.data: /data/esearch/data path.logs: /data/esearch/logs network.host: 192.168.1.32 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.1.33"] # 指定誰可以成為 Master
2、node2 ( 默認的 node )
shell > grep -vP '^#|^$' /usr/local/elasticsearch-2.4.0/config/elasticsearch.yml cluster.name: es node.name: node-2 # 節點名稱唯一 path.data: /data/esearch/data path.logs: /data/esearch/logs network.host: 192.168.1.33 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.1.32"] # 指定誰可以成為 Master
## 注意:
> 如果不指定誰可以成為 Master ,那么默認兩個節點都是 Master ,無法構成集群。新加入的節點不設置該參數,無法加入到現有集群。
> 如果被指定的節點設置了 node.master: false ,那么該節點無法成為 Master ,如果只有兩個節點,當 Master 掛掉,那么集群失敗。
> 防火牆:開啟 TCP 9200 :為了使用web瀏覽集群狀態等,TCP 9300 :用於節點直接通信。
3、查看集群狀態
shell > curl -X GET 192.168.1.32:9200/_cat/master?v # 查看誰是 Master id host ip node 9RVF5M9lRDuvhzKNF5VjXA 192.168.1.32 192.168.1.32 node-1 shell > curl -X GET 192.168.1.32:9200/_cat/nodes?v # 查看各節點狀態,* 代表該節點是 Master ,m 代表該節點可以成為 Master ,- 代表該節點不可以成為 Master host ip heap.percent ram.percent load node.role master name 192.168.1.33 192.168.1.33 4 17 0.36 d m node-2 192.168.1.32 192.168.1.32 4 25 0.49 d * node-1
# End
