ES 8.0 及其生態配置過程一條龍服務

語言: CN / TW / HK

背景

前兩天長三角某一線城市的資料洩漏了,沒有上熱搜被壓下去了,但是圈內傳開了,我們 leader 給我打了個緊急電話,詢問我們 ES 架構的安全性,儘管我說了我們使用的 7.x 我已經開啟了安全配置,但是他還是懇求我升到最高版本,沒辦法只能升級了,順便說一句資料洩漏真的是夠過很嚴重,我在某網站上通過輸入我的手機號查到了近幾年的賬號和住址資訊等,更可怕的是這個網站如果付費能查到更詳細的資訊,細思極恐。

  • 資訊洩漏.png

下載

根據自己的硬體選擇合適的安裝包進行下載,我這邊是 mac x86 版本:

  • Elasticsearch 8.0.0 :http://www.elastic.co/downloads/past-releases/elasticsearch-8-0-0
  • Kibana 8.0.0:http://www.elastic.co/downloads/past-releases/kibana-8-0-0

解壓

將下載好的兩個壓縮包進行解壓,可以的到兩個資料夾

kibana-8.0.0
elasticsearch-8.0.0

Elasticsearch

配置 elasticsearch.yml

修改 elasticsearch 主目錄下的 config/elasticsearch.yml 檔案

# ---------------------------------- Cluster -----------------------------------
cluster.name: qjfy
# ------------------------------------ Node ------------------------------------
node.name: node-1
# ----------------------------------- Memory -----------------------------------
bootstrap.memory_lock: true
# ---------------------------------- Network -----------------------------------
network.host: localhost
http.port: 9200

啟動 elasticsearch

在 es 主目錄中 ,執行 :

 ./bin/elasticsearch

稍等片刻,出現以下資訊:

  • 這裡是為了說明 8.x 自動開啟了安全設定,給出了 elastic 使用者的初始密碼,可以使用命令進行修改
  • 另外給出了 HTTP CA 證書
  • 如果需要安裝 kibana (下面的章節會講到),我們只需要啟動 kibana 點選給出的網址之後將這裡給出的一長串 token 拷貝進去即可,切記只有 30 分鐘有效,如果超時,執行命令重新生成即可,執行命令即可返回一長串新的 token
     ./bin/elasticsearch-create-enrollment-token -s kibana
    
    • 如果想要其他節點加入叢集,按照以下的操作進行即可

      ✅ Elasticsearch security features have been automatically configured! ✅ Authentication is enabled and cluster connections are encrypted.

      ℹ️ Password for the elastic user (reset with bin/elasticsearch-reset-password -u elastic): OxhN+dqEpl+MR_UaVUgV

      ℹ️ HTTP CA certificate SHA-256 fingerprint: d5f97e829d095c89a8eeb03df6b17792a9e073e5a85448258697b647da7a752b

      ℹ️ Configure Kibana to use this cluster: • Run Kibana and click the configuration link in the terminal when Kibana starts. • Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes): eyJ2ZXIiOiI4LjAuMCIsImFkciI6WyIxMjcuMC4wLjE6OTIwMCIsIls6OjFdOjkyMDAiXSwiZmdyIjoiZDVmOTdlODI5ZDA5NWM4OWE4ZWViMDNkZjZiMTc3OTJhOWUwNzNlNWE4NTQ0ODI1ODY5N2I2NDdkYTdhNzUyYiIsImtleSI6IjJSZ1Z5SUVCVkFIZWZJc3JCZXd2OmQ1MUtlN0ZpUnRLYk56SU9Dd2lURGcifQ==

      ℹ️ Configure other nodes to join this cluster: • On this node: ⁃ Create an enrollment token with bin/elasticsearch-create-enrollment-token -s node. ⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml. ⁃ Restart Elasticsearch. • On other nodes: ⁃ Start Elasticsearch with bin/elasticsearch --enrollment-token <token>, using the enrollment token that you generated.

回看 elasticsearch.yml

我們開啟一個新的終端,再看配置檔案 elasticsearch.yml (如下所示),可以發現上面的部分是我們自己配置的內容,下面的部分系統自動給我們寫入了一些 “SECURITY AUTO CONFIGURATION” 配置,這些內容就是系統自動預設為我們開啟了 HTTP API 客戶端連線開啟加密,叢集間加密傳輸和身份驗證、自動加入叢集等配置。這些在以前都是需要手動配置的,現在自動生成倒也方便。

# ---------------------------------- Cluster -----------------------------------
cluster.name: qjfy
# ------------------------------------ Node ------------------------------------
node.name: node-1
# ----------------------------------- Memory -----------------------------------
bootstrap.memory_lock: true
# ---------------------------------- Network -----------------------------------
network.host: localhost
http.port: 9200

#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 04-07-2022 07:19:54
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["node-1"]

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

Kibana

配置 kibana.yml

修改 kibana 主目錄下面的 config/kibana.yml 檔案

server.port: 5601
server.host: "localhost"
elasticsearch.hosts: ["http://localhost:9200"]

啟動 Kibana

在 kibana 主目錄啟動,命令:

./bin/kibana

稍等片刻我們發現,會列印如下的資訊,我們將連結 http://localhost:5601/?code=157557 拷貝到瀏覽器,然後將上面啟動 es 時候得到的 token (如果超時按照上面的方法重新生成一個即可)拷貝進去,然後進行自動配置即可。

i Kibana has not been configured.

Go to http://localhost:5601/?code=157557 to get started.

如果配置成功,會出現登入頁面,此時輸入賬號和密碼即可,賬號是 elastic ,密碼就是上面章節自動生成的密碼 OxhN+dqEpl+MR_UaVUgV 。

登陸.png

到此為止 ES 和 Kibana 成功啟動了,當然了我這裡只是單機版本,叢集版本還需要摸索。

對比 7.x 和 8.x 配置

其實對於單節點來說,從配置上來看我沒發現 elasticsearch 8.x 有比 7.x 更安全的配置,因為這些配置我在 7.x 的時候都手動配置了,所以我覺得 8.x 只是實現了“一鍵配置”這個功能,至於說在相同配置檔案下 8.x 有比 7.x 更安全的內在防護或者對安全進行了升級,這個我沒有研究不能下定論。

IK

下載 IK 外掛

從這裡下載 8.0 版本的 ik 外掛的原始碼

  • http://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v8.0.0

修改原始碼

(細節太多,篇幅過長,如果有興趣,我可以再寫一篇)下載原始碼之後,匯入 idea ,對 pom.xml 執行 reload project ,稍等片刻,然後對原始碼進行修改,主要目的是能夠定時掃描連線的資料庫中的詞典,更新 IK 分詞器所使用到的詞庫。然後 mvn package 打包得到的壓縮包,解壓之後將資料夾改為 ik 放入 ES 的 plugins 目錄下。重啟 ES 之後終端會列印一下資訊,表明 ES 載入外掛執行成功:

[2022-07-05T15:07:13,619][INFO ][o.e.p.PluginsService     ] [node-1] loaded plugin [analysis-ik]
...
[2022-07-05T15:07:28,065][INFO ][stdout                   ] [node-1] 準備!
[2022-07-05T15:07:28,212][INFO ][stdout                   ] [node-1] 連線伺服器中的資料庫成功!
[2022-07-05T15:07:38,924][INFO ][stdout                   ] [node-1] 載入表中分詞

測試分詞器

在 Kibana 命令列中進行測試:

GET _analyze
{
  "text": "杭州市文三路15號",
  "analyzer": "ik_smart"
}

結果可以看出來分詞器已經載入了我資料庫中的詞表,對“杭州市文三路15號”進行了分詞。

{
  "tokens" : [
    {
      "token" : "杭州市",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "文三路",
      "start_offset" : 3,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "15號",
      "start_offset" : 6,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

pinyin

因為我做的業務有用到漢語拼音外掛的功能,所以這裡也記錄一下配置過程。

下載 pinyin 外掛

從這裡下載 pinyin 8.0.0 :

  • http://github.com/medcl/elasticsearch-analysis-pinyin/releases/tag/v8.0.0

配置

將下載好的壓縮包解壓得到資料夾改名為 pinyin ,並且放到 ES 的 plugins 下 將 pinyin 資料夾下的 plugin-descriptor.properties 中的 elasticsearch.version 改為 8.0.0 (和你的 ES 版本一樣) ,然後重新啟動 ES 載入改外掛,列印如下資訊表明載入成功:

[2022-07-05T15:25:13,591][INFO ][o.e.p.PluginsService     ] [node-1] loaded plugin [analysis-pinyin]

測試

在 Kibana 終端中使用 pinyin 分析器測試

GET _analyze
{
  "text": "杭州市",
  "analyzer": "pinyin"
}

可以看到打印出了相關漢字的單字拼音還有首字母的縮寫。

{
  "tokens" : [
    {
      "token" : "hang",
      "start_offset" : 0,
      "end_offset" : 0,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "hzs",
      "start_offset" : 0,
      "end_offset" : 0,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "zhou",
      "start_offset" : 0,
      "end_offset" : 0,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "shi",
      "start_offset" : 0,
      "end_offset" : 0,
      "type" : "word",
      "position" : 2
    }
  ]
}

dynamic-synonym

編譯

從這裡 clone 下來程式碼進入 idea

http://github.com/bells/elasticsearch-analysis-dynamic-synonym

然後進行編譯打包 mvn package 得到壓縮包,將該專案 target/releases 下的壓縮包進行解壓得到資料夾,改名為 dynamic-synonym 移動到 ES 的 plugins 下

配置

將 dynamic-synonym 資料夾下的 plugin-descriptor.properties 檔案中的最下面的 ES 版本改為 8.0.0 , 重啟 ES ,如果出現下面日誌說明 ES 載入外掛成功:

 [2022-07-05T15:52:35,707][INFO ][o.e.p.PluginsService     ] [node-1] loaded plugin [analysis-dynamic-synonym]
...
[2022-07-05T16:04:25,799][INFO ][dynamic-synonym          ] [node-1] start reload local synonym from /Users/wys/elasticsearch-8.0.0/config/synonyms.txt.

測試

在 ES 的 config 目錄下建立一個儲存同義詞的檔案 synonyms.txt ,寫入一下內容:

kfc,肯德基
mc,麥當勞
hz,杭州

在 Kibana 中進行測試,先要建立一個索引,裡面有分詞器,分詞器中把加進去:

PUT /test
{
  "settings":{
    "analysis":{

      "filter": {
          "my_synonym" : {
            "type" : "dynamic_synonym",
            "synonyms_path" : "synonyms.txt"
          }
      },
      "analyzer":{
        "ik_search_analyzer":{
          "type": "custom",
          "tokenizer":"ik_smart",
          "filter": [ "my_synonym" ]
        }
      }
    }
  }
}

GET test/_analyze
{
  "text": "杭州肯德基",
  "analyzer": "ik_search_analyzer"
}

列印結果,可以看到先通過 ik_smart 進行分詞,然後將同義詞也都找了出來:

{
  "tokens" : [
    {
      "token" : "杭州",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "hz",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "SYNONYM",
      "position" : 0
    },
    {
      "token" : "肯德基",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "kfc",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "SYNONYM",
      "position" : 1
    }
  ]
}

隨時可以在 synonyms.txt 中按照上面的格式加新的同義詞, ES 會在幾秒內重新載入同義詞,並列印資訊:

[2022-07-05T16:05:25,802][INFO ][dynamic-synonym          ] [node-1] start reload local synonym from /Users/wys/elasticsearch-8.0.0/config/synonyms.txt.
[2022-07-05T16:05:25,821][INFO ][dynamic-synonym          ] [node-1] success reload synonym

尾記

本文主要記錄了安裝 ES 8.0.0 、安裝 Kibana 8.0.0 、 ik 8.0.0 、 pinyin 8.0.0 、 dynamic_synonym 7.16.0(可以當作 8.0.0 使用)等過程。

活動連結

我正在參與掘金技術社群創作者簽約計劃招募活動,點選連結報名投稿