项目需要搭建es环境,踩了一些坑,记录下

首选还是通过docker搭建,而通过docker最佳的部署方式就是docker compose

1.docker-compose.yml

version: "3.1"
# 服务配置
services:
  elasticsearch:
    container_name: es01
    image: elasticsearch:8.12.2
    restart: unless-stopped
    environment:
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
      - "TZ=Asia/Shanghai"
      - "discovery.type=single-node"
      - "ELASTIC_PASSWORD=123456"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      # - ./config:/usr/share/elasticsearch/config
      - ./data:/usr/share/elasticsearch/data
      - ./plugins:/usr/share/elasticsearch/plugins
    networks:
      - elastic_net
  kibana:
    container_name: kibana01
    image: kibana:8.12.2
    restart: unless-stopped
    environment:
      - "TZ=Asia/Shanghai"
      - "I18N_LOCALE=zh-CN"
      - "ELASTICSEARCH_HOSTS=http://elasticsearch:9200"
      - "ELASTICSEARCH_USERNAME=test"
      - "ELASTICSEARCH_PASSWORD=123456"
    ports:
      - "5601:5601"
        #volumes:
        #- ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
    networks:
      - elastic_net
    depends_on:
      - elasticsearch
# 网络配置
networks:
  elastic_net:
    driver: bridge

 经过权衡,我觉得es和kibana的配置文件都没必要映射出来,常用的参数在docker-compose.yml配置文件里指定即可,其他用默认的配置文件完全够用

这样做的好处就是不用操心配置文件的问题,以后升级容器也可以无脑升级

由于es和kibana特地配置在了一个网络elastic_net,所以es的连接地址用了服务名 http://elasticsearch:9200,这样部署脚本就比较通用,不用区分服务器ip

文件层级如下,这里把kibana也作为了es01的子文件,不过后面不需要kibana文件夹了,因为不用映射文件出来了

 

 

2.启动容器,查看服务状态

启动容器

docker compose up -d && docker compose logs -f

查看日志发现报错

kibana01 |                Root causes:
kibana01 |                       security_exception: unable to authenticate user [jsti] for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]

 这种问题是正常现象,因为新配置的账号在es里面没有导致的,新加账号即可

原来使用的是elastic默认账号,但是启动kibana报错了,新版本不给使用默认账号,所以docker-compose文件里使用了新账号

[FATAL][root] Error: [config validation of [elasticsearch].username]: value of "elastic" is forbidden. This is a superuser account that cannot write to system indices that Kibana needs to function. Use a service account token instead.

参考文档:https://blog.csdn.net/qq_52594679/article/details/136257664

 

访问es地址,输入elastic默认账号密码后,显示正常

 

访问kibana地址,显示kibana未准备

 

3.进入es容器,新增kibana账号

进入es容器

docker exec -it es01 /bin/bash

新增账号,并赋权

#创建新账户
elasticsearch-users useradd username

#给账户授权
elasticsearch-users roles -a superuser username
elasticsearch-users roles -a kibana_system username

  此时用该账号登陆kibana页面,一切正常

 

4.安装IK分词器

 注意:安装IK分词器的版本,必须和Elasticsearch的版本一致,

           上文安装的是Elasticsearch 8.12.2的,所以接下来安装的IK分词器版本是8.12.2

           这里知道es最新版本是8.13,我为啥用8.12.2了吧,因为IK分词器只更新到8.12.2 :)

(1)获取IK分词器下载地址

IK分词器下载地址:https://github.com/infinilabs/analysis-ik/releases/

 

(2)执行命令安装

进入es容器

docker exec -it es01 /bin/bash

执行在线安装命令

./bin/elasticsearch-plugin install https://github.com/infinilabs/analysis-ik/releases/download/v8.12.2/elasticsearch-analysis-ik-8.12.2.zip

如果环境不能下载,可以先下载到本地,再上传到容器里,执行离线安装命令

./bin/elasticsearch-plugin install  /路径/elasticsearch-analysis-ik-8.12.2.zip

重启容器

exit
docker restart es01

之后再查看plugins文件夹里面已经有analysis-ik目录了

 此时目录结构如下

 

 

 

参考文档:

https://blog.csdn.net/Acloasia/article/details/130683934

https://blog.csdn.net/qq_27895435/article/details/131430040

https://blog.csdn.net/qq_43711451/article/details/137779312

https://www.jianshu.com/p/6b8ad80382e9

https://blog.csdn.net/Tomwildboar/article/details/134655491

https://blog.csdn.net/qq_35787138/article/details/130863261

 

后来接到项目通知,需要搭一套旧版本的es适配老代码

没办法,又搭了一套,这里记录下

version: "3.1"
# 服务配置
services:
  elasticsearch:
    container_name: es02
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
    restart: unless-stopped
    environment:
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
      - "TZ=Asia/Shanghai"
      - "discovery.type=single-node"
      - "ELASTIC_PASSWORD=123456"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
    ports:
      - "9202:9200"
      - "9302:9300"
    volumes:
      #- ./config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./data:/usr/share/elasticsearch/data
      - ./plugins:/usr/share/elasticsearch/plugins
    networks:
      - elastic_net2
  kibana:
    container_name: kibana02
    image: docker.elastic.co/kibana/kibana:7.15.1
    restart: unless-stopped
    environment:
      - "TZ=Asia/Shanghai"
      - "I18N_LOCALE=zh-CN"
      - "ELASTICSEARCH_HOSTS=http://elasticsearch:9200"
      - "ELASTICSEARCH_USERNAME=test"
      - "ELASTICSEARCH_PASSWORD=123456"
      - "xpack.security.encryptionKey=something_at_least_32_characters"
      - "xpack.reporting.encryptionKey=something_at_least_32_characters"
    ports:
      - "5602:5601"
        #volumes:
        #- ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
    networks:
      - elastic_net2
    depends_on:
      - elasticsearch
  elasticsearch-head:
    container_name: es-head5-02
    image: mobz/elasticsearch-head:5
    restart: unless-stopped
    environment:
      - "TZ=Asia/Shanghai"
    #volumes:
    #       - /opt/modules/elasticsearch/head/Gruntfile.js:/usr/src/app/Gruntfile.js
    #       - /opt/modules/elasticsearch/head/_site/app.js:/usr/src/app/_site/app.js        
    ports:
      - "9102:9100"
    networks:
      - elastic_net2
    depends_on:
      - elasticsearch
# 网络配置
networks:
  elastic_net2:
    driver: bridge

yml说明:

1.这个安装了es,kibana,es-head,其中es和kibana版本要保持一致

2.旧版本的es和kibana镜像不太好找,好在通过es官网找到了:https://www.elastic.co/guide/en/elasticsearch/reference/7.15/getting-started.html   docker.elastic.co/elasticsearch/elasticsearch:7.15.1

3.es-head做为前端调用后端es服务,存在跨域问题,需要在es里面加入允许跨域参数:

- "http.cors.enabled=true"
- "http.cors.allow-origin=*"

4.es 7.15.1默认没有账号密码,这里账号密码未生效,暂时未关注,因为用于测试环境

5.kibana调用es的地址一定要用9200容器内端口,而不是宿主机的9202,因为使用场景是docker创建的容器内网段

6.这里同样没有使用把配置文件映射出来的方式,而是尽量在yml文件里指定

参考文档:

https://blog.csdn.net/m0_67403272/article/details/126327284

https://blog.csdn.net/tianqiuhao/article/details/130368132

https://www.jianshu.com/p/8572c537bb8a

https://blog.csdn.net/yangkei/article/details/126708031

https://www.cnblogs.com/jthr/p/17078888.html

https://blog.csdn.net/weixin_42715225/article/details/115217463

posted on 2024-04-23 18:34  06  阅读(3167)  评论(0编辑  收藏  举报