因为我们目前在做的app存在一个在线的实时搜索功能,所以需要接入es作为搜索,下面讲解下整个的搭建过程。因为目前就只有一个服务器所以目前暂时没做集群,后面版本迭代在考虑升级(主要是怕服务器吃不消)。
首先我的springboot版本号为2.3.2所以对应es的版本号就是7.6.2.所以直接引入对应的依赖即可。(代码再我github有体现这里就不做赘述,直接讲下几个注意点)

docker-compose方式部署

docker-compose文件

  1. version: '3'
  2. services:
  3. elasticsearch:
  4. image: elasticsearch:7.6.2
  5. container_name: elasticsearch7.6.2
  6. restart: always
  7. networks:
  8. - net-es
  9. ports:
  10. - "9200:9200"
  11. - "9300:9300"
  12. volumes:
  13. # # # # 会出现找不到配置文件的情况 docker cp elasticsearch7.6.2:/usr/share/elasticsearch/config ./elasticsearch/ 把容器内文件挂在出来
  14. # docker cp ./elasticsearch/plugins elasticsearch7.6.2:/usr/share/elasticsearch/ 把文件导入到容器内
  15. # # # # 还有就是权限问题,给我们宿主机赋权 chmod 777 ./elasticsearch/你要挂在的文件夹
  16. - "./elasticsearch/data:/usr/share/elasticsearch/data:rw"
  17. - "./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
  18. - "./elasticsearch/plugins/:/usr/share/elasticsearch/plugins/"
  19. environment:
  20. - discovery.type=single-node
  21. - ES_JAVA_OPTS=-Xms512m -Xmx512m
  22. - TZ=Asia/Shanghai
  23. - TAKE_FILE_OWNERSHIP=true # 就是这货 volumes 挂载权限问题
  24. elastichd:
  25. image: containerize/elastichd:latest
  26. container_name: elasticsearch-hd
  27. restart: always
  28. networks:
  29. - net-es
  30. ports:
  31. - "9800:9800"
  32. depends_on:
  33. - "elasticsearch"
  34. kibana:
  35. image: kibana:7.6.2
  36. container_name: kibana7.6.2
  37. restart: always
  38. links:
  39. - elasticsearch:es #可以用es这个域名访问elasticsearch服务
  40. depends_on:
  41. - elasticsearch #kibana在elasticsearch启动之后再启动
  42. environment:
  43. - TZ=Asia/Shanghai
  44. volumes:
  45. - ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
  46. networks:
  47. - net-es
  48. ports:
  49. - "5601:5601"
  50. #这里要注意,es和eshd要在相同网络才能被links
  51. networks:
  52. net-es:
  53. external: false

启动前所需准备

数据挂载赋权

你直接启动会出现文件无法访问的问题,因为你要做数据挂载,需要往elasticsearch文件夹写入数据,但是你执行用户不是root,docker-compose拉取镜像还有创建的容器实际上都是非root用户创建的所以你在做数据挂载之前,需要多挂载的文件夹进行一个赋权。chmod 777 ./elasticsearch

容器内配置数据挂失败

再次启动还是会出现找不到配置文件的情况 ,所以先把挂载注释掉,先把项目启动起来后,直接运行镜像文件拷贝到当前目录下即可。kibana也同理,为了省略我这里只写了一个作为参考

  1. docker cp elasticsearch7.6.2:/usr/share/elasticsearch/config/ ./elasticsearch/

分词器下载

安装中文分词器ik插件(请确保版本号一定要一致,他那个github版本很多的,一定可以找到属于自己的版本),并且检查下分词器的properties配置文件的版本号是否一致,不一致需要修改,不过只要你下载了自己版本的一般无需修改,如果版本相差很大,启动es可能会有各种异常。
image.pngimage.png

修改es的yml文件和kibana的yml文件

一定要配置network.publish_host 否则boot启动连接es会报错

  1. cluster.name: "es-cluster1"
  2. network.host: 0.0.0.0
  3. network.publish_host: 192.168.1.148
  4. #添加的文件
  5. node.name: es-node1
  6. http.port: 9200
  7. transport.tcp.port: 9300
  8. http.cors.enabled: true
  9. http.cors.allow-origin: "*"
  10. bootstrap.memory_lock: flase
  11. bootstrap.system_call_filter: false

image.pngimage.png

k8s方式部署

es

StatefulSet

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: elasticsearch
  5. labels:
  6. app: elasticsearch
  7. namespace: prod
  8. spec:
  9. serviceName: elasticsearch
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: elasticsearch
  14. template:
  15. metadata:
  16. labels:
  17. app: elasticsearch
  18. spec:
  19. containers:
  20. - name: elasticsearch
  21. image: elasticsearch:7.6.2
  22. imagePullPolicy: IfNotPresent
  23. resources:
  24. limits:
  25. cpu: 250m
  26. memory: 2Gi
  27. requests:
  28. cpu: 100m
  29. memory: 1Gi
  30. env:
  31. - name: TZ
  32. value: Asia/Shanghai
  33. ports:
  34. - name: rest
  35. containerPort: 9200
  36. - name: inter
  37. containerPort: 9300
  38. volumeMounts:
  39. - name: es-data
  40. mountPath: /usr/share/elasticsearch/data
  41. - name: es-yaml
  42. mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
  43. - name: es-plugins
  44. mountPath: /usr/share/elasticsearch/plugins/
  45. volumes:
  46. - name: es-data
  47. hostPath:
  48. path: /root/project-server/backend/es/data
  49. type: Directory
  50. - name: es-yaml
  51. hostPath:
  52. path: /root/project-server/backend/es/conf/elasticsearch.yml
  53. type: File
  54. - name: es-plugins
  55. hostPath:
  56. path: /root/project-server/backend/es/plugins/
  57. type: Directory

Service

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: elasticsearch
  5. namespace: prod
  6. labels:
  7. app: elasticsearch
  8. spec:
  9. type: NodePort
  10. ports:
  11. - port: 9200
  12. targetPort: 9200
  13. nodePort: 30005
  14. name: es-9200
  15. - port: 9300
  16. targetPort: 9300
  17. nodePort: 30006
  18. name: es-9300
  19. selector:
  20. app: elasticsearch

kibana

StatefulSet

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: kibana
  5. labels:
  6. app: kibana
  7. namespace: prod
  8. spec:
  9. serviceName: kibana
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: kibana
  14. template:
  15. metadata:
  16. labels:
  17. app: kibana
  18. spec:
  19. containers:
  20. - name: kibana
  21. image: kibana:7.6.2
  22. imagePullPolicy: IfNotPresent
  23. resources:
  24. limits:
  25. cpu: 250m
  26. memory: 1000Mi
  27. requests:
  28. cpu: 100m
  29. memory: 768Mi
  30. env:
  31. - name: TZ
  32. value: Asia/Shanghai
  33. ports:
  34. - containerPort: 5601
  35. volumeMounts:
  36. - name: kibana-yaml
  37. mountPath: /usr/share/kibana/config/kibana.yml
  38. - name: elastichd
  39. image: containerize/elastichd:latest
  40. imagePullPolicy: IfNotPresent
  41. ports:
  42. - containerPort: 9800
  43. volumes:
  44. - name: kibana-yaml
  45. hostPath:
  46. path: /root/project-server/backend/kibana/conf/kibana.yml
  47. type: File

Service

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. namespace: prod
  5. name: kibana
  6. labels:
  7. app: kibana
  8. spec:
  9. type: NodePort
  10. ports:
  11. - port: 5601
  12. targetPort: 5601
  13. nodePort: 30007
  14. name: kibana
  15. - port: 9800
  16. targetPort: 9800
  17. nodePort: 30008
  18. name: elastichd
  19. selector:
  20. app: kibana