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

Centos7上安裝 elasticsearch-6.2.2及相關插件

als 測試 內存大小 server 安裝和使用 earch sport ava map

原文地址

elasticsearch是一個開源的搜索服務器,提供了一個分布式多用戶能力的全文搜索引擎,下面是我的安裝筆記

準備工作

java版本

jdk版本必須是1.8及1.8以上

[root@localhost ~]# 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用戶安裝和使用,需要另外創建用戶

[root@localhost ~]# useradd es &&  echo "es123"  | passwd --stdin es

修改 /etc/security/limits.conf

[root@localhost ~]#  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

[root@localhost ~]#  vim /etc/sysctl.conf
vm.max_map_count = 262144
vm.swappiness = 1   # 禁用swapping

使修改生效

[root@localhost ~]#  sysctl -p

安裝 elasticsearch-6.2.2

下載解壓

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

修改elasticsearch-6.2.2目錄權限

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

[root@localhost src]# chown -R es:es /opt/elasticsearch-6.2.2/

創建數據目錄與日誌目錄

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

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

修改配置文件

[es@localhost elasticsearch-6.2.2]$ cd /opt/elasticsearch-6.2.2/conf
[es@localhost 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

啟動服務

[es@localhost elasticsearch-6.2.2]$ /opt/elasticsearch-6.2.2/bin/elasticsearch    #前臺啟動
[es@localhost elasticsearch-6.2.2]$ nohup /opt/elasticsearch-6.2.2/bin/elasticsearch &    #後臺啟動

測試服務是否啟動成功

看到9200和9300端口就ok了,其中9300是es節點tcp通訊端口,9200是RESTful接口

[es@localhost ~]$ 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

[es@localhost ~]$ 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 插件

[es@localhost ~]$ /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的客戶端工具

# 下載必要組件
[root@localhost ~]# yum -y install nodejs npm git bzip2
[root@localhost ~]# cd /opt/
[root@localhost opt]# git clone https://github.com/mobz/elasticsearch-head.git
[root@localhost opt]# npm install -g grunt-cli
[root@localhost opt]# cd /opt/elasticsearch-head
[root@localhost /opt/elasticsearch-head]# npm install
# 修改Gruntfile.js
[root@localhost opt]# cd /opt/elasticsearch-head
[root@localhost /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地址)
[root@localhost /opt/elasticsearch-head]#  cd _site
[root@localhost _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服務
[root@localhost _site]# cd /opt/elasticsearch-head/node_modules/grunt/bin/
[root@localhost bin]# nohup ./grunt server &
# 修改 elasticsearch-6.2.2 配置文件
[root@localhost opt]# su - es 
[es@localhost 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服務吧

原文地址

Centos7上安裝 elasticsearch-6.2.2及相關插件