Helm3安装带有ik分词的ElasticSearch

发布时间:2024-07-10 09:01

前言

上一篇写了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镜像

下载完成之后,将文件夹按照下图显示排放
Helm3安装带有ik分词的ElasticSearch_第1张图片

然后,我们来编写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": "我喜欢你"
}

Helm3安装带有ik分词的ElasticSearch_第2张图片

成功!!!

总结

Dockerfile里面的8.2.2-debian-11-r0这个版本号不要写错了,我当时就是因为这个错纠结了一个星期,大家不要犯我一样的错!

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

引用

k8s无脑系列(十一)-安装ElasticSearch到集群并设置中文分词

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号