1. 程式人生 > 實用技巧 >elasticsearch安裝01

elasticsearch安裝01

環境

角色 主機
elasticsearch 10.0.0.51
elasticsearch 10.0.0.52
elasticsearch 10.0.0.53

條件

1.恢復快照,基礎優化
2.每臺elasticsearch伺服器需要配置2G記憶體

以下以db01為例進行配置

伺服器時間同步

[root@db01 ~]# yum install -y ntpdate
[root@db01 ~]# ntpdate time1.aliyun.com

伺服器統一字符集

伺服器編碼解碼的字符集統一使用utf-8

安裝Java環境

#上傳
[root@db01 ~]# rz jdk-8u181-linux-x64.rpm

#安裝
[root@db01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm

安裝ES

1.上傳或下載包
[root@db01 ~]# rz elasticsearch-6.6.0.rpm

2.安裝
[root@db01 ~]# rpm -ivh elasticsearch-6.6.0.rpm

3.根據提示繼續操作
[root@db01 ~]# systemctl daemon-reload
[root@db01 ~]# systemctl enable elasticsearch.service
[root@db01 ~]# systemctl start elasticsearch.service

4.驗證,es啟動過程有點慢,看情況判斷
[root@db01 ~]# netstat -lntp     
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      20040/java
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      20040/java

[root@db01 ~]# curl 127.0.0.1:9200
{
  "name" : "FIddisT",
  "cluster_name" : "elasticsearch",		#叢集的名字
  "cluster_uuid" : "m8Y9neWHRxat7V1tVijMxA",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

#es服務啟動較慢,可以使用ps或者free -m 判斷服務是否執行

配置ES

#db01配置
[root@db01 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-application
node.name: node-51
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52","10.0.0.53"]
discovery.zen.minimum_master_nodes: 2

#db02配置
[root@db02 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-application
node.name: node-52
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.52,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52","10.0.0.53"]
discovery.zen.minimum_master_nodes: 2

#db03配置
[root@db03 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-application
node.name: node-53
path.data: /service/es/data
path.logs: /service/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.53,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51", "10.0.0.52","10.0.0.53"]
discovery.zen.minimum_master_nodes: 2

根據配置檔案建立目錄

#建立資料目錄和日誌目錄
[root@db01 ~]# mkdir /service/es/{data,logs} -p
#授權
[root@db01 ~]# chown -R elasticsearch.elasticsearch /service/es/

配置啟動檔案中的記憶體鎖

[root@db01 ~]# vim /usr/lib/systemd/system/elasticsearch.service
[Service]
... ...
LimitMEMLOCK=infinity

重啟ES

[root@db01 ~]# systemctl daemon-reload
[root@db01 ~]# systemctl start elasticsearch.service

ES互動

方法一:使用curl命令

#建立索引(庫),?pretty以列格式顯示
[root@db01 ~]# curl -XPUT '10.0.0.51:9200/student?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "student"
}

#新增資料(key value)
[root@db01 ~]# curl -XPUT '10.0.0.51:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'
{
  "_index" : "student",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

#檢視資料,索引 型別 以列顯示
[root@db01 ~]# curl -GET '10.0.0.51:9200/student/user/1?pretty'
{
  "_index" : "student",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "lhd",
    "sex" : "man",
    "age" : "18",
    "about" : "good good study",
    "interests" : [
      "chinese",
      "english"
    ]
  }
}

方法二:使用外掛1

1.在'電腦上'解壓es-head-0.1.4_0.crx.zip,解壓到'一個目錄'(以後使用瀏覽器中的外掛必須依靠該目錄)
2.谷歌瀏覽器,右上角,三個點或者三個槓
3.更多工具--擴充套件程式
4.右上角開啟開發者模式
5.載入已解壓的擴充套件程式,選擇解壓問價你的目錄
6.右上角有個放大鏡或者拼圖,點選進去

方法三:使用外掛2

#安裝npm(只需要在一個節點安裝即可,如果前端還有nginx做反向代理可以每個節點都裝)
[root@elkstack01 ~]# yum install -y npm
#進入下載head外掛程式碼目錄
[root@elkstack01 src]# cd /usr/local/
#從GitHub上克隆程式碼到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
#克隆完成後,進入elasticsearch外掛目錄
[root@elkstack01 local]# cd elasticsearch-head/
#清除快取
[root@elkstack01 elasticsearch-head]# npm cache clean -f
#使用npm安裝n模組(不同的專案js指令碼所需的node版本可能不同,所以就需要node版本管理工具)
[root@elkstack01 elasticsearch-head]# npm install -g n
#安裝最新版本n模組
[root@elkstack01 elasticsearch-head]# n stable
#生成grunt
[root@elkstack01 elasticsearch-head]# npm install grunt -save
#確認生成grunt檔案
[root@elkstack01 elasticsearch-head]# ll node_modules/grunt
#執行安裝grunt
[root@elkstack01 elasticsearch-head]# npm install
#後臺啟動head外掛(切記,必須在外掛目錄下執行啟動命令)
[root@elkstack01 elasticsearch-head]# npm run start &
#驗證埠是否啟動成功
[root@elkstack01 elasticsearch-head]# netstat -lntup
tcp        0      0 0.0.0.0:9100                0.0.0.0:*                   LISTEN      11293/grunt
#啟動成功後,修改elasticsearch配置檔案
[root@elkstack01 elasticsearch-head]# vim /etc/elasticsearch/elasticsearch.yml
#新增如下兩行,開啟跨域訪問支援(新增在配置檔案最後即可)
http.cors.enabled: true
http.cors.allow-origin: "*"
#重啟elasticsearch
[root@elkstack01 elasticsearch-head]# /etc/init.d/elasticsearch restart