Helm3安裝帶有ik分詞的ElasticSearch

Awbeci發表於2022-07-13

前言

上一篇寫了Helm3安裝ElasticSearch和Kibana,但是發現沒有安裝ik中文分詞,於是在此基本上操作如何安裝帶有ik分詞的elasticsearch分享給大家。

操作

在操作之前我要感謝smokelee的文章:k8s無腦系列(十一)-安裝ElasticSearch到叢集並設定中文分詞,我的這篇文章也是基於此文章的。

操作步驟:
1、下載ik分詞包
2、製作帶有ik分詞的docker映象
3、修改bitnami/elasticsearch的values.yaml檔案,並使用剛剛製作的docker映象
4、安裝elasticsearch和kibana
5、在kibana中使用develop tools驗證ik是否安裝成功

1、下載ik和pinyin分詞包

下面是包地址,大家可以選擇合適的版本下載即可。

https://github.com/medcl/elasticsearch-analysis-ik/releases
https://github.com/medcl/elasticsearch-analysis-pinyin/releases

2、製作帶有ik和pinyin分詞的docker映象

下載完成之後,將資料夾按照下圖顯示排放
image.png

然後,我們來編寫Dockerfile

FROM docker.io/bitnami/elasticsearch:8.2.2-debian-11-r0
COPY ik /opt/bitnami/elasticsearch/plugins/ik

注意,這裡我們使用的是bitnami/elasticsearch的docker映象,版本號為8.2.2-debian-11-r0,大家可以參考這個地址選用合適的版本。

接著,我們在docker目錄下按順序執行下面命令來製作含有ik和pingyin分詞的docker映象。

docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
docker build -t registry.cn-hangzhou.aliyuncs.com/com-seaurl/seaurl-elasticsearch:x.x.x  .
docker push registry.cn-hangzhou.aliyuncs.com/com-seaurl/seaurl-elasticsearch:x.x.x

上面的地址我是使用阿里雲的,大家按照規範設定成自己的映象中心地址就好,這樣我們就完成了docker映象的製作。

3、修改bitnami/elasticsearch的values.yaml檔案,並使用剛剛製作的docker映象

按照上篇文章中的講解,我們來配置下values.yaml檔案

global:
  storageClass: "alicloud-cnfs-nas"
  elasticsearch:
    service:
      name: elasticsearch
      ports:
        restAPI: 9200
  kibanaEnabled: false
image:
  registry: registry.cn-hangzhou.aliyuncs.com
  repository: com-seaurl/seaurl-elasticsearch
  # 這裡的x.x.x就是你剛才製作docker映象的版本號:x.x.x
  tag: x.x.x
sysctlImage:
  enabled: false
plugins: analysis-icu
service:
  type: NodePort
master:
  replicaCount: 1
data:
  replicaCount: 1
coordinating:
  replicaCount: 1
ingest:
  replicaCount: 1

4、安裝elasticsearch和kibana

執行下面命令安裝elasticsearch,

helm install -f test-values.yaml test-elasticsearch bitnami/elasticsearch --namespace 

輸出:

elasticsearch
NAME: test-elasticsearch
LAST DEPLOYED: Mon Jul 11 21:44:14 2022
NAMESPACE: elasticsearch
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: elasticsearch
CHART VERSION: 18.2.9
APP VERSION: 8.2.2

-------------------------------------------------------------------------------
 WARNING

    Elasticsearch requires some changes in the kernel of the host machine to
    work as expected. If those values are not set in the underlying operating
    system, the ES containers fail to boot with ERROR messages.

    To check whether the host machine meets the requirements, run the command
    below:

      kubectl logs --namespace elasticsearch $(kubectl get --namespace elasticsearch \
        pods -l app=elasticsearch,role=master -o jsonpath='{.items[0].metadata.name}') \
        elasticsearch

    You can adapt the Kernel parameters on you cluster as described in the
    official documentation:

      https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster

    As an alternative, you can specify "sysctlImage.enabled=true" to use a
    privileged initContainer to change those settings in the Kernel:

      helm upgrade --namespace elasticsearch test-elasticsearch bitnami/elasticsearch --set sysctlImage.enabled=true

    Note that this requires the ability to run privileged containers, which is likely not
    the case on many secure clusters. To cover this use case, you can also set some parameters
    in the config file to customize the default settings:

      https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-store.html
      https://www.elastic.co/guide/en/cloud-on-k8s/master/k8s-virtual-memory.html

    For that, you can place the desired parameters by using the "config" block present in the values.yaml

** Please be patient while the chart is being deployed **

  Elasticsearch can be accessed within the cluster on port 9200 at test-elasticsearch.elasticsearch.svc.cluster.local

  To access from outside the cluster execute the following commands:

    export NODE_PORT=$(kubectl get --namespace elasticsearch -o jsonpath="{.spec.ports[0].nodePort}" services test-elasticsearch)
    export NODE_IP=$(kubectl get nodes --namespace elasticsearch -o jsonpath="{.items[0].status.addresses[0].address}")
    curl http://$NODE_IP:$NODE_PORT/

然後,我們通過上面命令獲取ip和port

# 獲取port
kubectl get --namespace elasticsearch -o jsonpath="{.spec.ports[0].nodePort}" services test-elasticsearch

# 獲取ip
kubectl get nodes --namespace elasticsearch -o jsonpath="{.items[0].status.addresses[0].address}"

獲取elasticsearch的ip和port之後,我們來安裝kibana,執行下面命令:

helm install -f test-values.yaml test-kibana bitnami/kibana --namespace elasticsearch    

輸出:

NAME: test-kibana
LAST DEPLOYED: Mon Jul 11 21:52:08 2022
NAMESPACE: elasticsearch
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: kibana
CHART VERSION: 10.1.9
APP VERSION: 8.2.2

** Please be patient while the chart is being deployed **

1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get --namespace elasticsearch -o jsonpath="{.spec.ports[0].nodePort}" services test-kibana)
  export NODE_IP=$(kubectl get nodes --namespace elasticsearch -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT

WARNING: Kibana is externally accessible from the cluster but the dashboard does not contain authentication mechanisms. Make sure you follow the authentication guidelines in your Elastic stack.
+info https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-up-authentication.html

然後,我們通過上面命令獲取ip和port

# 獲取port
kubectl get --namespace elasticsearch -o jsonpath="{.spec.ports[0].nodePort}" services test-kibana

# 獲取ip
kubectl get nodes --namespace elasticsearch -o jsonpath="{.items[0].status.addresses[0].address}"

5、在kibana中使用develop tools驗證ik是否安裝成功

安裝好elasticsearch和kibana之後,我們嘗試在Chrome裡面開啟Dev Tools,驗證ik分詞包是否安裝成功,於是我們輸入下面程式碼並執行:

POST _analyze
{
  "analyzer": "ik_smart",
  "text": "我喜歡你"
}

image.png

成功!!!

總結

Dockerfile裡面的8.2.2-debian-11-r0這個版本號不要寫錯了,我當時就是因為這個錯糾結了一個星期,大家不要犯我一樣的錯!

FROM docker.io/bitnami/elasticsearch:8.2.2-debian-11-r0

引用

k8s無腦系列(十一)-安裝ElasticSearch到叢集並設定中文分詞

相關文章