Elasticsearch安裝部署
Elasticsearch是個開源分布式搜索引擎,它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。要負責數據存儲與搜索。
最近在學習ELK方面的知識,在安裝的過程中也遇到了很多坑,網上找了很多Elasticsearch的安裝文檔,但是或多或少都有些沒有說清楚的地方,於是結合自己的部署過程,整理成自己的文檔,以便后續查閱。
1、Elasticsearch安裝所需環境
Elasticsearch對於JAVA JDK環境有要求,需要JDK1.8或以上的支持。操作系統官網上都有各個系統的安裝文件。我本機的測試環境是CentOS 7.3,JDK版本是1.8.0_131
1 [root@localhost /]# more /etc/redhat-release 2 CentOS Linux release 7.3.1611 (Core) 3 [root@localhost /]# java -version 4 java version "1.8.0_131" 5 Java(TM) SE Runtime Environment (build 1.8.0_131-b11) 6 Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
JDK建議使用Oracle的,不要使用CentOS自帶的OpenJDK,如果使用java -version查看是OpenJDK的,可以先卸載,然后再安裝Oracle JDK。
1 先查看 rpm -qa | grep java,如果openjdk,則可使用yum remove來刪除 2 [root@localhost /]# rpm -qa | grep java 3 java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5 4 [root@localhost /]#yum -y remove java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
刪除完OpenJDK之后,則可以到Oracle官網下載JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html
下載JDK之后,解壓文件,並設置PATH、JAVA_HOME既可以配置完成。
1 [root@localhost /]# tar -zxvf jdk-8u151-linux-x64.tar.gz 2 將JDK路徑加入環境變量中 3 [root@localhost /]vim /etc/profile 4 將下面內容復制到文件最后一行: 5 export JAVA_HOME=/usr/local/jdk1.7.0_67 #這里換成你的JDK解壓路徑 6 export PATH=$PATH:$JAVA_HOME/bin 7 編輯完后,刷新配置文件 8 [root@localhost /]source /etc/profile 9 完成,查看JDK版本 10 [root@localhost /]java -version
2、Elasticsearch下載安裝及配置
- Elasticsearch下載解壓
從ELK官網下載Elasticsearch:https://www.elastic.co/cn/downloads/elasticsearch
下載elasticsearch-6.1.0.tar.gz的tar包后,在Centos中解壓
1 [root@localhost local]# tar -zxvf elasticsearch-6.1.0.tar.gz 2 [root@localhost elasticsearch-6.1.0]# pwd 3 /usr/local/elasticsearch-6.1.0 4 [root@localhost elasticsearch-6.1.0]# ls 5 bin config data lib LICENSE.txt logs modules NOTICE.txt plugins README.textile
Elasticsearch的tar包是已經編譯好的,下載后直接使用即可
- Elasticsearch配置文件
現在我們來配置 config/elasticsearch.yml文件,Elasticsearch的所有配置信息都在此文件中。
1 [root@localhost config]# more elasticsearch.yml 2 # ======================== Elasticsearch Configuration ========================= 3 # 4 # NOTE: Elasticsearch comes with reasonable defaults for most settings. 5 # Before you set out to tweak and tune the configuration, make sure you 6 # understand what are you trying to accomplish and the consequences. 7 # 8 # The primary way of configuring a node is via this file. This template lists 9 # the most important settings you may want to configure for a production cluster. 10 # 11 # Please consult the documentation for further information on configuration options: 12 # https://www.elastic.co/guide/en/elasticsearch/reference/index.html 13 # 14 # ---------------------------------- Cluster ----------------------------------- 15 # 16 # Use a descriptive name for your cluster: 17 # 18 cluster.name: jun-application 19 # 20 # ------------------------------------ Node ------------------------------------ 21 # 22 # Use a descriptive name for the node: 23 # 24 node.name: node-1 25 # 26 # Add custom attributes to the node: 27 # 28 node.attr.rack: r1 29 # 30 # ----------------------------------- Paths ------------------------------------ 31 # 32 # Path to directory where to store the data (separate multiple locations by comma): 33 # 34 #path.data: /path/to/data 35 # 36 # Path to log files: 37 # 38 #path.logs: /path/to/logs 39 # 40 # ----------------------------------- Memory ----------------------------------- 41 # 42 # Lock the memory on startup: 43 # 44 #bootstrap.memory_lock: true 45 # 46 # Make sure that the heap size is set to about half the memory available 47 # on the system and that the owner of the process is allowed to use this 48 # limit. 49 # 50 # Elasticsearch performs poorly when the system is swapping the memory. 51 # 52 # ---------------------------------- Network ----------------------------------- 53 # 54 # Set the bind address to a specific IP (IPv4 or IPv6): 55 # 56 network.host: 10.1.129.101 57 # 58 # Set a custom port for HTTP: 59 # 60 http.port: 9200 61 # 62 # For more information, consult the network module documentation. 63 # 64 # --------------------------------- Discovery ---------------------------------- 65 # 66 # Pass an initial list of hosts to perform discovery when new node is started: 67 # The default list of hosts is ["127.0.0.1", "[::1]"] 68 # 69 #discovery.zen.ping.unicast.hosts: ["host1", "host2"] 70 # 71 # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1): 72 # 73 #discovery.zen.minimum_master_nodes: 74 # 75 # For more information, consult the zen discovery module documentation. 76 # 77 # ---------------------------------- Gateway ----------------------------------- 78 # 79 # Block initial recovery after a full cluster restart until N nodes are started: 80 # 81 #gateway.recover_after_nodes: 3 82 # 83 # For more information, consult the gateway module documentation. 84 # 85 # ---------------------------------- Various ----------------------------------- 86 # 87 # Require explicit names when deleting indices: 88 # 89 #action.destructive_requires_name: true 90 91 http.cors.enabled: true 92 http.cors.allow-origin: "*"
這里ES配置就結束了,對沒有錯,就這么簡單。當然以上的配置只是基本的配置,還有更多的參數設置可以到官網上了解更多。
- Elasticsearch創建普通用戶
配置完成后,即可以啟動Elasticsearch,但是在啟動之前需要先創建一個用戶,並將此用戶的權限賦予Elasticsearch的目錄。(主要是因為Elasticsearch不能用root用戶來啟動,必須用非root用戶)
1 [root@localhost /]# useradd elkuser 2 #elasticsearch 只能用非 root 啟動 3 [root@localhost /]# chown -R elkuser.elkuser elasticsearch-6.1.0
用戶創建后,進入到elasticsearch-6.1.0目錄啟動ES:
1 [root@localhost elasticsearch-6.1.0]# pwd 2 /usr/local/elasticsearch-6.1.0 3 [root@localhost elasticsearch-6.1.0]# cd bin 4 [root@localhost bin]# ls 5 elasticsearch elasticsearch-env.bat elasticsearch-plugin elasticsearch-service-mgr.exe elasticsearch-translog.bat 6 elasticsearch.bat elasticsearch-keystore elasticsearch-plugin.bat elasticsearch-service-x64.exe 7 elasticsearch-env elasticsearch-keystore.bat elasticsearch-service.bat elasticsearch-translog 8 [root@localhost bin]# ./elasticsearch
- Elasticsearch文件打開數及堆大小檢測
在ES啟動過程中可能會報如下的錯誤:
1 ERROR: [2] bootstrap checks failed 2 [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] 3 [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
錯誤主要說明,linux中elasticsearch最大文件打開數太小,需要我們修改到對應的數值:
1 1.max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] 2 3 修改/etc/security/limits.conf文件,添加或修改如下行: 4 * hard nofile 65536 5 * soft nofile 65536 6 7 2.max virtual memory areas vm.max…… 8 9 修改 /etc/sysctl.conf 文件,添加如下行: 10 11 vm.max_map_count=262144 12 修改好了以后,運行/sbin/sysctl -p 13 14 重啟以后,再啟動es即可,就可以通過主機ip訪問。
如果是內存不足就需要調整內存大小了或者調整config/jvm.options的參數
1 在elasticsearch-6.1.0/config目錄中有jvm.options文件,可以設置JVM大小 2 [root@localhost config]# pwd 3 /usr/local/elasticsearch-6.1.0/config 4 [root@localhost config]# ls 5 elasticsearch.yml jvm.options log4j2.properties 6 [root@localhost config]# vi jvm.options 7 -Xms2g 8 -Xmx2g
最大堆內存和最小堆內存兩者值設定為一至,同時盡可能大,同時不要超過32G,最大堆內存和最小堆內存如果不一致,在啟動中的時候會進行內存大小自動調整,可能會出現中斷的情況,為了避免此情況的產生,所以heap_check中要求最大內存最小內存相當,本例中設置為2G。
- 啟動Elasticsearch
前序工作全部准備完成后,即可在elasticsearch-6.1.0目錄中的bin目錄下執行elasticsearch-6.1.0文件:
1 [root@localhost elasticsearch-6.1.0]# pwd 2 /usr/local/elasticsearch-6.1.0 3 [root@localhost elasticsearch-6.1.0]# cd bin 4 [root@localhost bin]# ls 5 elasticsearch elasticsearch-env.bat elasticsearch-plugin elasticsearch-service-mgr.exe elasticsearch-translog.bat 6 elasticsearch.bat elasticsearch-keystore elasticsearch-plugin.bat elasticsearch-service-x64.exe 7 elasticsearch-env elasticsearch-keystore.bat elasticsearch-service.bat elasticsearch-translog 8 [root@localhost bin]# ./elasticsearch 9 [root@localhost bin]# ./elasticsearch -d 可以使其在后台運行
瀏覽器訪問 http://localhost:9200 ,可以查看到對應的節點信息,如下顯示則說明啟動正常:
3、Elasticsearch安裝Head插件
Elasticsearch-head是一個界面化的集群操作和管理工具,可以對集群進行傻瓜式操作。你可以通過插件把它集成到es(首選方式),也可以安裝成一個獨立webapp。
ES-head主要有四個方面的操作:
- 顯示集群的拓撲,並且能夠執行索引和節點級別操作
- 搜索接口能夠查詢集群中原始json或表格格式的檢索數據
- 能夠快速訪問並顯示集群的狀態
- 有一個輸入窗口,允許任意調用RESTful API。這個接口包含幾個選項,可以組合在一起以產生有趣的結果;
- 請求方法(get、put、post、delete),查詢json數據,節點和路徑
- 支持JSON驗證器
- 支持重復請求計時器
- 支持使用javascript表達式變換結果
- 收集結果的能力隨着時間的推移(使用定時器),或比較的結果
- 能力圖表轉換后的結果在一個簡單的條形圖(包括時間序列)
Elasticsearch-head的官方文檔:https://github.com/mobz/elasticsearch-head
安裝Elasticsearch的Head插件,首先需要在Centos中安裝Git、Nodejs、grunt。三個軟件全部安裝配置完成后,才可安裝Head插件
- Git安裝
從Git官網中下載Linux版本安裝包:https://git-scm.com/downloads
下載之后進行編譯安裝:
1 $ tar -zxf git-1.7.2.2.tar.gz 2 $ cd git-1.7.2.2 3 $ make prefix=/usr/local all 4 $ sudo make prefix=/usr/local install 5 以上命令執行完成后,即可使用Git
在編譯安裝過程中,可能會出現如下錯誤:
Can't locate ExtUtils/MakeMaker.pm in @INC…………
解決方法如下:
yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
- Nodejs安裝
從Nodejs官網中下載Nodejs安裝包:http://nodejs.cn/download/
Nodejs 官網提供了編譯好的Linux二進制包,你也可以下載下來直接應用。下載二進制的包,直接解壓到目錄即可:
1 [root@localhost local]# tar -xvf node-v8.9.0-linux-x64.tar.xz 2 [root@localhost node-v8.9.0]# pwd 3 /usr/local/node-v8.9.0 4 [root@localhost node-v8.9.0]# ls 5 bin CHANGELOG.md etc include lib LICENSE README.md share
配置NODE_HOME,進入profile編輯環境變量
1 vim /etc/profile
設置nodejs環境變量
1 #set for nodejs 2 export NODE_HOME=/usr/local/node-v8.9.0 3 export PATH=$NODE_HOME/bin:$PATH
:wq保存並退出,編譯/etc/profile 使配置生效
1 source /etc/profile
驗證是否安裝配置成功
1 node -v
輸出node-v8.9.0表示配置成功
- Grunt安裝
安裝還Nodejs后,直接在CentsOS中運行如下命令即可安裝Grunt
1 npm install -g grunt-cli 2 grunt -version -- 安裝后 ,查看 grunt版本。
-g代表全局安裝,並且自動加入PATH變量。安裝完成后檢查一下。
grunt是一個很方便的構建工具,可以進行打包壓縮、測試、執行等等的工作,Elasticsearch里的head插件就是通過grunt啟動的,因此需要安裝grunt。
- 下載 head 插件的源碼並安裝
1 git clone git://github.com/mobz/elasticsearch-head.git
下載之后會在目錄中生成elasticsearch-head文件夾
1 [root@localhost elasticsearch-head]# pwd 2 /usr/local/elasticsearch-head 3 [root@localhost elasticsearch-head]# ls 4 Dockerfile elasticsearch-head.sublime-project grunt_fileSets.js LICENCE package.json proxy _site test 5 Dockerfile-alpine Gruntfile.js index.html node_modules plugin-descriptor.properties README.textile src 6 [root@localhost elasticsearch-head]#
下載之后,需要修改head源碼。因為直接執行有很多限制,比如無法跨機器訪問。因此需要用戶修改兩個地方。
elasticsearch-head/Gruntfile.js,增加hostname屬性
1 connect: { 2 server: { 3 options: { 4 port: 9100, 5 hostname: '*', 6 base: '.', 7 keepalive: true 8 } 9 } 10 }
elasticsearch-head/_site/app.js。修改head的連接地址
1 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200"; 2 把localhost修改成你es的服務器地址,如: 3 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://10.1.129.101:9200";
然后在elasticsearch-head源碼目錄中,執行npm install:
1 npm install
在運行npm install時,可能會存在Head插件phantomjs權限問題:
1 [root@localhost elasticsearch-head]# npm install 2 phantomjs-prebuilt@2.1.16 install /usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt 3 node install.js 4 PhantomJS not found on PATH 5 Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 6 Verified checksum of previously downloaded file 7 Extracting tar contents (via spawned process) 8 Removing /usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt/lib/phantom 9 Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1513568757772/phantomjs-2.1.1-linux-x86_64 -> /usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt/lib/phantom 10 Phantom installation failed { Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1513568757772/phantomjs-2.1.1-linux-x86_64' -> '/usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt/lib/phantom' 11 errno: -13, 12 code: 'EACCES', 13 syscall: 'link', 14 path: '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1513568757772/phantomjs-2.1.1-linux-x86_64', 15 dest: '/usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1513568757772/phantomjs-2.1.1-linux-x86_64' -> '/usr/local/elasticsearch-head/node_modules/phantomjs-prebuilt/lib/phantom' 16 npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression 17 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules/fsevents): 18 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) 19 20 npm ERR! code ELIFECYCLE 21 npm ERR! errno 1 22 npm ERR! phantomjs-prebuilt@2.1.16 install: `node install.js` 23 npm ERR! Exit status 1 24 npm ERR! 25 npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script. 26 npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 27 28 npm ERR! A complete log of this run can be found in: 29 npm ERR! /root/.npm/_logs/2017-12-18T03_46_03_878Z-debug.log
解決方法,在npm install命令后加 -g 參數:
1 npm install -g
最后,在elasticsearch-head源代碼目錄下啟動nodejs,運行 grunt server。
運行成功后,訪問 http://localhost:9100 網站,即可看到elasticsearch的相關信息: