elasticsearch之二快速入門及安裝

個(gè)人專題目錄


快速入門

1. 安裝包下載

  1. Elasticsearch官網(wǎng): https://www.elastic.co/products/elasticsearch
  2. Elastic官網(wǎng):https://www.elastic.co/cn/

2. 安裝Elasticsearch(單節(jié)點(diǎn)Linux環(huán)境)

使用6.5.4,Elasticsearch6.x要求Linux內(nèi)核必須是3.5+版本以上。

在linux操作系統(tǒng)中,查看內(nèi)核版本的命令是: uname -a

Elasticsearch在Linux中安裝部署的時(shí)候,需要系統(tǒng)為其提供若干系統(tǒng)配置。如:應(yīng)用可啟動(dòng)的線程數(shù)、應(yīng)用可以在系統(tǒng)中劃分的虛擬內(nèi)存、應(yīng)用可以最多創(chuàng)建多少文件等。

Elasticsearch是java開發(fā)的應(yīng)用。在6.8.4版本中,要求JDK至少是1.8.0_131版本以上。

出于安全考慮,elasticsearch默認(rèn)不允許以root賬號(hào)運(yùn)行。

  1. 解壓elasticsearch-6.5.4.tar.gz到/opt/module目錄下
$ tar -zxvf elasticsearch-6.5.4.tar.gz -C /opt/module/
  1. 在/opt/module/elasticsearch-6.5.4路徑下創(chuàng)建data和logs文件夾
$ mkdir data
$ mkdir logs
#因?yàn)榘踩珕栴},Elasticsearch 不允許root用戶直接運(yùn)行,所以要?jiǎng)?chuàng)建新用戶,在root用戶中創(chuàng)建新用戶,執(zhí)行如下命令
useradd elasticsearch  # 新增elasticsearch用戶
passwd  elasticsearch  # 為elasticsearch用戶設(shè)置密碼
chown -R elasticsearch:elasticsearch /opt/module/ #文件夾所有者
  1. 修改配置文件/opt/module/elasticsearch-6.5.4/config/elasticsearch.yml
$ pwd
/opt/module/elasticsearch-6.5.4/config
vim jvm.options
-Xms1g
-Xmx1g
$ vi elasticsearch.yml
#配置elasticsearch的集群名稱,默認(rèn)是elasticsearch。建議修改成一個(gè)有意義的名稱
# ---------------------------------- Cluster -----------------------------------
cluster.name: my-application
# ------------------------------------ Node ------------------------------------
#節(jié)點(diǎn)名,elasticsearch會(huì)默認(rèn)隨機(jī)指定一個(gè)名字,建議指定一個(gè)有意義的名稱,方便管理
node.name: node-102
# ----------------------------------- Paths ------------------------------------
path.data: /opt/module/elasticsearch-6.5.4/data
path.logs: /opt/module/elasticsearch-6.5.4/logs
# ----------------------------------- Memory -----------------------------------
# 設(shè)置為true可以鎖住ES使用的內(nèi)存,避免內(nèi)存與swap分區(qū)交換數(shù)據(jù)。
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# ---------------------------------- Network -----------------------------------
#設(shè)置為0.0.0.0允許外網(wǎng)訪問
network.host: 192.168.1.102 
# --------------------------------- Discovery ----------------------------------
discovery.zen.ping.unicast.hosts: ["hadoop102"]
#Elasticsearch的http訪問端口
http.port: 9200
#初始化新的集群時(shí)需要此配置來選舉master
cluster.initial_master_nodes: ["node-1"]

(1)cluster.name

如果要配置集群需要兩個(gè)節(jié)點(diǎn)上的elasticsearch配置的cluster.name相同,都啟動(dòng)可以自動(dòng)組成集群,這里如果不改cluster.name則默認(rèn)是cluster.name=my-application,

(2)nodename隨意取但是集群內(nèi)的各節(jié)點(diǎn)不能相同

(3)修改后的每行前面不能有空格,修改后的“:”后面必須有一個(gè)空格

  1. 配置linux系統(tǒng)環(huán)境(參考:http://blog.csdn.net/satiling/article/details/59697916

? (1)切換到root用戶,編輯limits.conf 添加類似如下內(nèi)容,是修改系統(tǒng)中允許應(yīng)用最多創(chuàng)建多少文件等的限制權(quán)限。Linux默認(rèn)來說,一般限制應(yīng)用最多創(chuàng)建的文件是65535個(gè)。但是Elasticsearch至少需要65536的文件創(chuàng)建權(quán)限。

# vi /etc/security/limits.conf

添加如下內(nèi)容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096

*代表任意用戶,soft表示內(nèi)存中虛擬文件(軟文件),hard表示落地到磁盤的具體文件(硬文件), nofile表示權(quán)限,65536表示個(gè)數(shù)。

(2)切換到root用戶,進(jìn)入limits.d目錄下修改配置文件。

在CentOS6.5版本中編輯下述的配置文件

vim /etc/security/limits.d/90-nproc.conf

在CentOS7+版本中編輯配置文件是:

vim /etc/security/limits.conf

是修改系統(tǒng)中允許用戶啟動(dòng)的進(jìn)程開啟多少個(gè)線程。默認(rèn)的Linux限制root用戶開啟的進(jìn)程可以開啟任意數(shù)量的線程,其他用戶開啟的進(jìn)程可以開啟1024個(gè)線程。必須修改限制數(shù)為4096+。因?yàn)镋lasticsearch至少需要4096的線程池預(yù)備。Elasticsearch在5.x版本之后,強(qiáng)制要求在linux中不能使用root用戶啟動(dòng)Elasticsearch進(jìn)程。所以必須使用其他用戶啟動(dòng)Elasticsearch進(jìn)程才可以。

# vim /etc/security/limits.d/90-nproc.conf
修改如下內(nèi)容:
* soft nproc 1024
#修改為
* soft nproc 4096

*任何用戶 nproc創(chuàng)建線程 數(shù)量4096

Linux低版本內(nèi)核為線程分配的內(nèi)存是128K。4.x版本的內(nèi)核分配的內(nèi)存更大。如果虛擬機(jī)的內(nèi)存是1G,最多只能開啟3000+個(gè)線程數(shù)。至少為虛擬機(jī)分配1.5G以上的內(nèi)存,保險(xiǎn)起見建議2G以上。

(3)切換到root用戶修改配置sysctl.conf

系統(tǒng)控制文件是管理系統(tǒng)中的各種資源控制的配置文件。Elasticsearch需要開辟一個(gè)65536字節(jié)以上空間的虛擬內(nèi)存。Linux默認(rèn)不允許任何用戶和應(yīng)用直接開辟虛擬內(nèi)存。

# vim /etc/sysctl.conf 
添加下面配置:最大虛擬內(nèi)存太小,限制一個(gè)進(jìn)程可以擁有的VMA(虛擬內(nèi)存區(qū)域)的數(shù)量
vm.max_map_count=655360
并執(zhí)行命令:
# sysctl -p

然后,重新啟動(dòng)elasticsearch,即可啟動(dòng)成功。

  1. 啟動(dòng)集群
$ bin/elasticsearch
  1. 測(cè)試集群
$ curl http://localhost:9200
{
  "name" : "L6WdN7y",
  "cluster_name" : "Elasticsearch",
  "cluster_uuid" : "s7_GSd9YQnaH10VQBKCQ5w",
  "version" : {
    "number" : "6.3.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "eb782d0",
    "build_date" : "2018-06-29T21:59:26.107521Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

curl http://localhost:9200/_cat/nodes 獲取節(jié)點(diǎn)信息
curl http://localhost:9200/_cat/shards 獲取分片信息
curl http://localhost:9200/_cat/indices 獲取索引信息
curl http://localhost:9200/_cat/health?v 獲取健康狀態(tài)信息

$ curl http://localhost:9200/_cluster/health
查詢集群狀態(tài)
{
    "cluster_name": "elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100
}

Status:集群狀態(tài)。Green 所有分片可用。Yellow所有主分片可用。Red主分片不可用,集群不可用。
  1. 停止集群

? kill -9 進(jìn)程號(hào)

3. 安裝Elasticsearch(多節(jié)點(diǎn)集群Linux環(huán)境)

1554688271544.png
1554688294804.png
1554688390747.png

4. Elasticsearch head插件安裝

  1. 下載插件

https://github.com/mobz/elasticsearch-head

elasticsearch-head-master.zip

  1. nodejs官網(wǎng)下載安裝包

https://nodejs.org/dist/

node-v6.9.2-linux-x64.tar.xz

  1. 將elasticsearch-head-master.zip和node-v6.9.2-linux-x64.tar.xz都導(dǎo)入到linux的/opt/software目錄。

  2. 安裝nodejs

$ tar -zxvf node-v6.9.2-linux-x64.tar.gz -C /opt/module/
  1. 配置nodejs環(huán)境變量
# vi /etc/profile
export NODE_HOME=/opt/module/node-v6.9.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin
# source /etc/profile 
  1. 查看node和npm版本
# node -v
v6.9.2 
# npm -v
3.10.9 
  1. 解壓head插件到/opt/module目錄下
$ unzip elasticsearch-head-master.zip -d /opt/module/
  1. 查看當(dāng)前head插件目錄下有無node_modules/grunt目錄:

沒有:執(zhí)行命令創(chuàng)建:

$ npm install grunt --save
  1. 安裝head插件:
]$ npm install -g cnpm --registry=https://registry.npm.taobao.org
  1. 安裝grunt:
$ npm install -g grunt-cli
  1. 編輯Gruntfile.js
$ vim Gruntfile.js
文件93行添加hostname:'0.0.0.0'

options: {
        hostname:'0.0.0.0',
        port: 9100,
        base: '.',
        keepalive: true
}
  1. 檢查head根目錄下是否存在base文件夾

沒有:將 _site下的base文件夾及其內(nèi)容復(fù)制到head根目錄下

$ mkdir base
$ cp base/* ../base/
  1. 啟動(dòng)grunt server:
$ grunt server -d
Running "connect:server" (connect) task
[D] Task source: /opt/module/elasticsearch-head-master/node_modules/grunt-contrib-connect/tasks/connect.js
Waiting forever...
Started connect web server on http://localhost:9100 

如果提示grunt的模塊沒有安裝:
Local Npm module “grunt-contrib-clean” not found. Is it installed? 
Local Npm module “grunt-contrib-concat” not found. Is it installed? 
Local Npm module “grunt-contrib-watch” not found. Is it installed? 
Local Npm module “grunt-contrib-connect” not found. Is it installed? 
Local Npm module “grunt-contrib-copy” not found. Is it installed? 
Local Npm module “grunt-contrib-jasmine” not found. Is it installed? 
Warning: Task “connect:server” not found. Use –force to continue. 
執(zhí)行以下命令: 
npm install grunt-contrib-clean -registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat -registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-connect -registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-jasmine -registry=https://registry.npm.taobao.org
最后一個(gè)模塊可能安裝不成功,但是不影響使用。
  1. 瀏覽器訪問head插件:
http://hadoop102:9100
  1. 啟動(dòng)集群插件后發(fā)現(xiàn)集群未連接

在/opt/module/elasticsearch-5.2.2/config路徑下修改配置文件elasticsearch.yml,在文件末尾增加

$ pwd
/opt/module/elasticsearch-5.2.2/config
$ vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
再重新啟動(dòng)elasticsearch。
  1. 關(guān)閉插件服務(wù)
ctrl+c
$ netstat -lntp | grep 9100
tcp        0      0 192.168.1.102:9100          0.0.0.0:*                   LISTEN 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 3,893評(píng)論 0 5
  • 1 準(zhǔn)備工作 服務(wù)器信息: 192.168.91.221 (elasticsearch-node1) 192.16...
    淺色的嗥嘯閱讀 2,854評(píng)論 2 9
  • ElasticSearch 一.全文檢索技術(shù)簡(jiǎn)介 1.什么是搜索? 搜索,就是在任何場(chǎng)景下,找尋你想要的信息,這個(gè)...
    Movle閱讀 264評(píng)論 0 2
  • 基本概念 集群:一個(gè)或者多個(gè)elasticsearch節(jié)點(diǎn)組成的集合 索引:含有相同屬性的文檔集合,例如一個(gè)索引代...
    彭槐閱讀 26,774評(píng)論 0 6
  • 今天老大考完試放假了,小的還上學(xué),起來做好飯吃完飯送小的走了大的在家看門吧,電動(dòng)車沒修好繼續(xù)騎自行車去上班,雖然累...
    王飛媽媽閱讀 87評(píng)論 0 0