1. 程式人生 > >Centos7上安裝 elasticsearch-6.2.2及相關外掛

Centos7上安裝 elasticsearch-6.2.2及相關外掛

原文地址

elasticsearch是一個開源的搜尋伺服器,提供了一個分散式多使用者能力的全文搜尋引擎,下面是我的安裝筆記

準備工作

java版本

jdk版本必須是1.8及1.8以上

[[email protected] ~]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

建立es使用者

elasticsearch6 不允許root使用者安裝和使用,需要另外建立使用者

[[email protected] ~]# useradd es &&  echo "es123"  | passwd --stdin es

修改 /etc/security/limits.conf

[[email protected] ~]#  vim /etc/security/limits.conf
# 修改系統最大檔案描述符限制
* soft nofile 262144 
* hard nofile 262144
# 修改系統鎖記憶體限制
es soft memlock unlimited 
es hard memlock unlimited
# 更改使用者可啟用的最大執行緒數
*  hard    nproc   4096
*  soft    nproc   4096

修改 /etc/sysctl.conf

[[email protected] ~]#  vim /etc/sysctl.conf
vm.max_map_count = 262144
vm.swappiness = 1   # 禁用swapping

使修改生效

[[email protected] ~]#  sysctl -p

安裝 elasticsearch-6.2.2

下載解壓

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.2.tar.gz
[
[email protected]
src]# tar xzvf elasticsearch-6.2.2.tar.gz [[email protected] src]# mv elasticsearch-6.2.2 /opt

修改elasticsearch-6.2.2目錄許可權

將該目錄下所有檔案的屬主與屬組均改為es

[[email protected] src]# chown -R es:es /opt/elasticsearch-6.2.2/

建立資料目錄與日誌目錄

注意:後續操作需要切換至es賬戶

[[email protected] src]# su - es
[[email protected] src]$ cd /opt/elasticsearch-6.2.2/
[[email protected] elasticsearch-6.2.2]$ mkdir -p elasticsearchdata/{data,log}

修改配置檔案

[[email protected] elasticsearch-6.2.2]$ cd /opt/elasticsearch-6.2.2/conf
[[email protected] conf]$  vim elasticsearch.yml
# 需要修改 cluster.name,node.name,path.data等引數值
cluster.name: app_es    # 叢集名字
node.name: node-1   # 節點名字
path.data: /opt/elasticsearch-6.2.2/elasticsearchdata/data # 指定資料存放路徑
path.logs: /opt/elasticsearch-6.2.2/elasticsearchdata/log # 指定日誌存放路徑
bootstrap.memory_lock: false
network.host: 0.0.0.0 # Set the bind address to a specific IP
http.port: 9200 # 預設是9200,你也可以通過修改其值自定義埠
transport.tcp.port: 9300    # 預設是9300,可自定義
# 叢集發現
#叢集節點ip或者主機,在這裡新增各節點ip
discovery.zen.ping.unicast.hosts: ["ip1:9300", "ip2:9300","ip3:9300"]
# 設定這個引數來保證叢集中的節點可以知道其它N個有master資格的節點。預設為1,對於大的叢集來說,可以設定大一點的值(2-4)
discovery.zen.minimum_master_nodes: 3

啟動服務

[[email protected] elasticsearch-6.2.2]$ /opt/elasticsearch-6.2.2/bin/elasticsearch    #前臺啟動
[[email protected] elasticsearch-6.2.2]$ nohup /opt/elasticsearch-6.2.2/bin/elasticsearch &    #後臺啟動

測試服務是否啟動成功

看到9200和9300埠就ok了,其中9300是es節點tcp通訊埠,9200是RESTful介面

[[email protected] ~]$ netstat -lntp | grep -E "9200|9300"
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:9200            0.0.0.0:*               LISTEN      1022/java
tcp        0      0 0.0.0.0:9300            0.0.0.0:*               LISTEN      1022/java

在瀏覽器輸入 http://你的ip:9200,可看到如下內容

{
  "name" : "node-1",
  "cluster_name" : "app_es",
 "cluster_uuid" : "...", 
  "version" : {
    "number" : "6.2.2",
    "build_hash" : "10b1edd",
    "build_date" : "2018-02-16T19:01:30.685723Z",
    "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"
}
證明啟動成功

解決啟動報錯

錯誤一:Cannot allocate memory

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000ca660000, 899284992, 0) failed; error='Cannot allocate memory' (errno=12)
There is insufficient memory for the Java Runtime Environment to continue.
Native memory allocation (mmap) failed to map 899284992 bytes for committing reserved memory.
An error report file with more information is saved as:
/opt/elasticsearch-6.2.2/hs_err_pid17955.log

由以上錯誤資訊可知,分配給java的記憶體不足,elasticsearch6.2 預設分配 jvm 空間大小為1g,這個虛機的記憶體大小不足,需要修改 jvm 空間分配,我們可以將1g改成512m

[[email protected] ~]$ vim /opt/elasticsearch-6.2.2/config/jvm.options
-Xms1g  修改為 -Xms512m
-Xmx1g  修改為 -Xmx512m

錯誤二:檔案描述符不足

ERROR: [3] bootstrap checks failed
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2]: memory locking requested for elasticsearch process but memory is not locked
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

如何你按我的教程順序來就不會發生這個錯誤,請參照準備工作這一節,修改相應的核心引數

錯誤三:不能以root啟動

不能以root身份來啟動es服務,需要以相應的es來啟動

外掛下載

下載中文分詞器 elasticsearch-analysis-ik 外掛

[[email protected] ~]$ /opt/elasticsearch-6.2.2/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip

下載 elasticsearch-head 外掛

head外掛是elasticsearch的客戶端工具

# 下載必要元件
[[email protected] ~]# yum -y install nodejs npm git bzip2
[[email protected] ~]# cd /opt/
[[email protected] opt]# git clone https://github.com/mobz/elasticsearch-head.git
[[email protected] opt]# npm install -g grunt-cli
[[email protected] opt]# cd /opt/elasticsearch-head
[[email protected] /opt/elasticsearch-head]# npm install
# 修改Gruntfile.js
[[email protected] opt]# cd /opt/elasticsearch-head
[[email protected] /opt/elasticsearch-head]# vim Gruntfile.js
# 在appcss後新增server塊
appcss: {
                                src: fileSets.srcCss,
                                dest: '_site/app.css'
                        },
                        server: {
                                options: {
                                        hostname: '*',          
                                        port: 9100,
                                        base: '.',      
                                        keepalive: true
                                }
                        }
                },
## 對外開放埠為9100,允許任何主機訪問
# 修改elasticsearch-head預設連線地址
## 修改head/_site/app.js,修改head連線es的地址(修改localhost為本機的IP地址)
[[email protected] /opt/elasticsearch-head]#  cd _site
[[email protected] _site]# vim app.js
## 將localhost修改為es服務的IP地址
修改前:this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
修改後: this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://你的ip:9200";
# 啟動head服務
[[email protected] _site]# cd /opt/elasticsearch-head/node_modules/grunt/bin/
[[email protected] bin]# nohup ./grunt server &
# 修改 elasticsearch-6.2.2 配置檔案
[[email protected] opt]# su - es 
[[email protected] opt]$ vim /opt/elasticsearch-6.2.2/config/elasticsearch.yml
## 在配置檔案最後新增下面兩條
http.cors.enabled: true # 允許跨域訪問,為了配合elasticsearch-head視覺化ES介面
http.cors.allow-origin: "*" # 允許所有地址跨域訪問
## 然後重啟es服務

在瀏覽器輸入:http://你的ip:9100,開始使用es服務吧

原文地址