ElasticSearch 安裝與運行

公號:碼農充電站pro
主頁:https://codeshellme.github.io

本節來介紹 ES 的安裝。

1,下載 ES

ES 是基於 Java 語言開發的,因此,要安裝 ES,首先需要有 Java 環境。

從 ES 7.0 開始,ES 內置了 Java 環境,所以如果安裝的是 7.0 及以上版本的 ES,就不需要額外安裝 Java 環境了。

我們可以到 ES 的下載頁面去下載 ES 安裝包,你可以根據你的系統,選擇不同的安裝包進行安裝。

在這裏插入圖片描述

我這裏選擇的是 Windows 版本,下載好壓縮包後,將其解壓。解壓後的目錄如下所示:

在這裏插入圖片描述

來看下每個目錄的作用:

  • bin 目錄中是一些工具命令。
  • data 目錄存儲數據文件。
  • jdk 目錄是 Java 運行環境。
  • lib 目錄是 Java 開發類庫。
  • logs 目錄用於存放日誌。
  • modules 目錄中包含了所有的 ES 模塊。
  • plugins 目錄包含所有已安裝的插件。
  • config 目錄是一些配置文件。
    • elasticsearch.yml 文件用於配置 ES 服務。
    • jvm.options 文件用於配置 JVM 參數。
      • 其中 Xmx 和 Xms 建議設置的大小一樣,且不超過機器內存的一半。
      • Xmx 和 Xms 默認爲 1g。
      • 這裏有一些介紹,你可以參考一下。

2,啓動 ES

bin 目錄中有一個 elasticsearch 命令,用於運行 ES 實例。我們可以通過 --help 參數查看其幫助:

> bin\elasticsearch --help
Starts Elasticsearch

Option                Description
------                -----------
-E <KeyValuePair>     Configure a setting
-V, --version         Prints Elasticsearch version information and exits
-d, --daemonize       Starts Elasticsearch in the background `在後臺運行`
-h, --help            Show help
-p, --pidfile <Path>  Creates a pid file in the specified path on start
-q, --quiet           Turns off standard output/error streams logging in console
-s, --silent          Show minimal output
-v, --verbose         Show verbose output

進入到解壓後的目錄中,在 Windows 系統中用下面命令來啓動 ES:

bin\elasticsearch

在 Linux 系統中使用下面命令啓動 ES:

bin/elasticsearch

如果啓動成功,ES Server 將在本機的 9200 端口監聽服務。

我們可以使用 curl 命令訪問本機 9200 端口,查看 ES 是否啓動成功。如果輸出像下面這樣,則說明啓動成功:

> curl http://localhost:9200/ 
{
  "name" : "LAPTOP-VH778PAK",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "trxvXvAfQ5GxWe3D4vEIXA",
  "version" : {
    "number" : "7.10.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
    "build_date" : "2020-12-05T01:00:33.671820Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

你也可以在瀏覽器中訪問服務地址,來查看是否啓動成功:

在這裏插入圖片描述

3,安裝 ES 插件

我們可以通過安裝 ES 插件來爲 ES 擴展功能。

bin 目錄中有一個 elasticsearch-plugin.bat 命令,是關於 ES 插件的命令,可以使用 --help 參數來查看其用法:

> bin\elasticsearch-plugin --help
A tool for managing installed elasticsearch plugins

Commands
--------
list - Lists installed elasticsearch plugins
install - Install a plugin
remove - removes a plugin from Elasticsearch

Non-option arguments:
command

Option             Description
------             -----------
-E <KeyValuePair>  Configure a setting
-h, --help         Show help
-s, --silent       Show minimal output
-v, --verbose      Show verbose output

使用 list 參數查看是否有插件:

> bin\elasticsearch-plugin list

沒有任何輸出,說明沒有插件。

下面演示安裝 analysis-icu 插件,這是一個分詞插件:

> bin\elasticsearch-plugin install analysis-icu
-> Installing analysis-icu
-> Downloading analysis-icu from elastic
[=================================================] 100%
-> Installed analysis-icu

安裝完成後,再次查看插件列表:

> bin\elasticsearch-plugin list
analysis-icu

可以看到,這時有了一個插件。

重新啓動 ES 服務後,我們也可以訪問 HTTP 接口來查看插件:

> curl localhost:9200/_cat/plugins
LAPTOP-VH778PAK analysis-icu 7.10.1
`服務名稱`       `插件名稱`    `插件版本`

添加 ?v 後綴可以查看字段的解釋:

> curl localhost:9200/_cat/plugins?v
name            component    version  `解釋`
LAPTOP-VH778PAK analysis-icu 7.10.1

這裏是關於 ES 插件的介紹,你可以瞭解一下。

4,運行 ES 集羣

我們可以運行多個 ES 實例,將其組成一個 ES 集羣,命令如下:

> bin\elasticsearch -E node.name=node1 -E cluster.name=escluster -E path.data=node1_data -d
> bin\elasticsearch -E node.name=node2 -E cluster.name=escluster -E path.data=node2_data -d
> bin\elasticsearch -E node.name=node3 -E cluster.name=escluster -E path.data=node3_data -d

其中 -E 用於指定命令參數,node.name 表示節點名稱,cluster.name 表示集羣名稱,path.data 表示數據目錄,-d 表示在後臺運行實例。

查看集羣中的節點:

> curl localhost:9200/_cat/nodes?v
ip        heap.percent ram.percent cpu  node.role  master name
127.0.0.1           30          91   9  cdhilmrstw -      node2
127.0.0.1           28          91   9  cdhilmrstw -      node3
127.0.0.1           34          91   9  cdhilmrstw *      node1

可以看到有 3 個節點,分別是 node1,node2,node3。其中標有星號 * 的節點爲主節點。

默認情況下,集羣中啓動的第一個節點,會將自己選舉爲 Master 節點

查看集羣健康狀態:

> curl localhost:9200/_cluster/health
{
    "cluster_name":"escluster",   `集羣名稱`
    "status":"green",             `健康狀態`
    "timed_out":false,            `是否超時`
    "number_of_nodes":3,          `節點數量`
    "number_of_data_nodes":3,     `數據節點數量` 
    "active_primary_shards":0,
    "active_shards":0,
    "relocating_shards":0,
    "initializing_shards":0,
    "unassigned_shards":0,
    "delayed_unassigned_shards":0,
    "number_of_pending_tasks":0,
    "number_of_in_flight_fetch":0,
    "task_max_waiting_in_queue_millis":0,
    "active_shards_percent_as_number":100
}

(本節完。)


推薦閱讀:

ElasticSearch 入門簡介


歡迎關注作者公衆號,獲取更多技術乾貨。

碼農充電站pro

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章