1. 程式人生 > >搜尋推薦一——Centos搭建Elasticsearch單機實戰

搜尋推薦一——Centos搭建Elasticsearch單機實戰

環境

  1. jdk: 1.8
  2. centos: 7
  3. elasticsearch: 5.3.0

一、JDK安裝

$: tar -zxvf jdk-8u181-linux-x64.tar.gz

$: mkdir /usr/local/java
$: cp jdk1.8.0_181 /usr/local/java/

##配置環境變數.bash_profile 或者 /etc/profile
$: vi ~/.bash_profile 
## 輸入下列配置
JAVA_HOME=/usr/local/java/jdk1.8.0_181 
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME
/lib/tools.jar:$JRE_HOME/lib PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export JAVA_HOME JRE_HOME CLASS_PATH PATH $: java -version | javac -version > java version "1.8.0_181" > Java(TM) SE Runtime Environment (build 1.8.0_181-b13) > Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode) >
javac 1.8.0_181

二、ES安裝

Elasticsearch的安裝過程非常容易,下面介紹在不同的作業系統如何安裝 -

  1. Windows作業系統 − 解壓縮zip包,並安裝Elasticsearch。
  2. UNIX作業系統 - 在任何位置提取tar檔案,並安裝Elasticsearch。
$ tar –xvf elasticsearch-5.3.0.tar.gz
$ mv elasticsearch-5.3.0 /home/es/
$ cd /home/es

在Windows中

> cd elasticsearch-5.3.0/bin
> elasticsearch

在Linux中

$ cd
elasticsearch-5.3.0/bin $ ./elasticsearch

安裝啟動單機部署

修改訪問es的ip及埠如下:

$ vi /home/es/elasticsearch-5.3.0/config/elasticsearch.yml

> #Set the bind address to a specific IP (IPv4 or IPv6):
> network.host: 172.16.119.154 (本機ip地址)
> #Set a custom port for HTTP:
> http.port: 9200
  • 啟動es
$ cd /home/es/elasticsearch-5.3.0/
前臺啟動:
$ ./bin/elasticsearch

後臺啟動:
$: ./elasticsearch-5.3.0/bin/elasticsearch -d 

啟動成功後,在web瀏覽器中輸入172.16.119.154:9200,如果返回以下頁面,則說明成功。

{
    name: "YoXmVYH",
    cluster_name: "elasticsearch",
    cluster_uuid: "Yw8f033SSb6_oc5o2v1r7w",
    version: {
        number: "5.3.0",
        build_hash: "3adb13b",
        build_date: "2017-03-23T03:31:50.652Z",
        build_snapshot: false,
        lucene_version: "6.4.1"
    },
    tagline: "You Know, for Search"
}

啟動後,如果只有本地可以訪問,嘗試修改配置檔案 elasticsearch.yml
中network.host (注意配置檔案格式不是以 # 開頭的要空一格, : 後要空一格)
為 network.host: 0.0.0.0
預設埠是 9200

注意:關閉防火牆 或者開放9200埠
elasticsearch.yml配置檔案格式不是以 # 開頭的要空一格, : 後要空一格

注意事項

(1)JVM記憶體問題 如果遇到:

圖片

說明jvm啟動記憶體太大,機器記憶體不足(如果按照之前虛擬機器配置中的1G來配置,則會出現此問題,因為es預設jvm啟動記憶體是2g,所以我們需要配置一下es的啟動記憶體) 編輯es/config/jvmoptions檔案,修改以下兩個引數:

#-Xms2g
#-Xmx2g
-Xms512m
-Xmx512m

(2)根許可權執行問題 如果遇到: Alt text 根據提示,無法以根許可權啟動es程式,所以我們建立一個非根使用者,並給它賦予目錄該使用者許可權。es 無法以root使用者啟動

# groupadd es
# useradd es -g es -p es
# chown -R es:es /home/es
# chmod -R 770 /home/es/
# sudo su es

用root使用者給es使用者賦權時沒有完全將es目錄的所有檔案許可權給es,重新用root使用者身份執行:(-R引數不要掉,表示遞迴將es目錄下所有檔案許可權給es使用者)

$ chown -R yehao:yehao /home/es/elasticsearch-5.3.0/

(3)記憶體問題: 有[1][2][3]三個問題,逐一解決:

[1]: initial heap size [16777216] not equal to maximum heap size [257949696]; this can cause resize pauses and prevents mlockall from locking the entire heap 第一個問題還是jvm記憶體問題,請重新檢查可能碰到的問題(1)JVM記憶體問題,按照步驟重新檢查一遍。

[2]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] Es程序設定的最大檔案描述符太小,需要增加。 編輯/etc/security/limits.conf,最後加上:

es soft nofile 65536 
es hard nofile 65536

然後重新連線shell,切換到es使用者,再嘗試啟動es。

[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 是因為作業系統的vm.max_map_count引數設定太小導致的,請使用root使用者登入系統,執行以下命令:

sysctl -w vm.max_map_count=262144 

並用以下命令檢視是否修改成功

sysctl -a | grep "vm.max_map_count"

如果能正常輸出262144,則說明修改成功

(4) 啟動異常:ERROR: bootstrap checks failed system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk 問題原因:因為Centos6不支援SecComp,而ES5.2.1預設bootstrap.system_call_filter為true進行檢測,所以導致檢測失敗,失敗後直接導致ES不能啟動。詳見 :https://github.com/elastic/elasticsearch/issues/22899

解決方法:在elasticsearch.yml中配置bootstrap.system_call_filter為false,注意要在Memory下面:

bootstrap.memory_lock: false
bootstrap.system_call_filter: false

(5) ERROR: bootstrap checks failed max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536] max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]

解決方法:切換到root使用者,編輯limits.conf 新增類似如下內容 $: vi /etc/security/limits.conf

新增如下內容: * soft nofile 65536 * hard nofile 65536 * soft nproc 2048 * hard nproc 4096

開啟防火牆埠

檢視已經開放的埠:

firewall-cmd --list-ports

開啟埠

firewall-cmd --zone=public --add-port=80/tcp --permanent

命令含義:

–zone #作用域
–add-port=80/tcp #新增埠,格式為:埠/通訊協議
–permanent #永久生效,沒有此引數重啟後失效

重啟防火牆

firewall-cmd --reload #重啟firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動

CentOS 7.X 下安裝ElasticSearch-Head外掛

一、安裝nodejs和rpm
1. su - root
2. yum install epel-release
3. yum install nodejs npm

注意:如果失敗請檢查CDN配置
$: vi /etc/resole.conf
新增:
    nameserver: 8.8.8.8
    nameserver: 114.114.114.114
$: systemctl restart NetworkManager 重啟網路


二、下載並安裝elasticsearch-head (yum install git安裝Git)
1. git clone https://github.com/mobz/elasticsearch-head.git
2. cd elasticsearch-head
3. npm install

------------------------------------------------------------------------------
如果出現如下錯誤: 
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected]^1.0.0 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN [email protected] license should be a valid SPDX license expression
npm ERR! Linux 3.10.0-693.el7.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install"
npm ERR! node v6.14.3
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node install.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the phantomjs-prebuilt package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node install.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs phantomjs-prebuilt
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls phantomjs-prebuilt
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/elasticsearch-head/npm-debug.log

------------------------------------------------------------------------------
沒有問題直接執行
4. npm run start
5. open http://localhost:9100 or http://you IP Address:9100 能正常開啟說明head外掛安裝正確

三、如果想查詢叢集健康資訊,那麼需要在elasticsearch配置檔案中授權
1. 在elasticsearch下的elasticsearch.yml下新增一下兩行:
    http.cors.enabled: true             # elasticsearch中啟用CORS
    http.cors.allow-origin: "*"             # 允許訪問的IP地址段,* 為所有IP都可以訪問

2. 重啟elastsearch
    systemctl restart elasticsearch.service
    或者
    cd /es
    ./bin/elasticsearch

四、修改es-head的localhost地址
1. cd ./elasticsearch-head  #(elasticsearch-head原始碼資料夾)
2. vim Gruntfile.js
3. Add hostname
    connect: {
        server: {
            options: {
                hostname: '0.0.0.0',
                port: 9100,
                base: '.',
                keepalive: true
            }
        }
    }

五、修改head的連線地址
1. cd ./elasticsearch-head  #(elasticsearch-head原始碼資料夾)
2. vim ./_site/app.js
3.  將localhost修改為ESdeIP地址
    修改前:this.base_uri = this.config.base_uri;
    修改後:this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://you ip address:9200";

六、啟動elasticsearch-head
1. cd elasticsearch-head(elasticsearch-head原始碼目錄)
2. ./node_modules/grunt/bin/grunt server
後臺啟動 > nohup ./node_modules/grunt/bin/grunt server >1.out &
關閉:ps -axu | grep grunt
kill -9 $pid

若有不正確之處,請指正!!!