因为我们目前在做的app存在一个在线的实时搜索功能,所以需要接入es作为搜索,下面讲解下整个的搭建过程。因为目前就只有一个服务器所以目前暂时没做集群,后面版本迭代在考虑升级(主要是怕服务器吃不消)。
首先我的springboot版本号为2.3.2所以对应es的版本号就是7.6.2.所以直接引入对应的依赖即可。(代码再我github有体现这里就不做赘述,直接讲下几个注意点)
docker-compose方式部署
docker-compose文件
version: '3'
services:
elasticsearch:
image: elasticsearch:7.6.2
container_name: elasticsearch7.6.2
restart: always
networks:
- net-es
ports:
- "9200:9200"
- "9300:9300"
volumes:
# # # # 会出现找不到配置文件的情况 docker cp elasticsearch7.6.2:/usr/share/elasticsearch/config ./elasticsearch/ 把容器内文件挂在出来
# docker cp ./elasticsearch/plugins elasticsearch7.6.2:/usr/share/elasticsearch/ 把文件导入到容器内
# # # # 还有就是权限问题,给我们宿主机赋权 chmod 777 ./elasticsearch/你要挂在的文件夹
- "./elasticsearch/data:/usr/share/elasticsearch/data:rw"
- "./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
- "./elasticsearch/plugins/:/usr/share/elasticsearch/plugins/"
environment:
- discovery.type=single-node
- ES_JAVA_OPTS=-Xms512m -Xmx512m
- TZ=Asia/Shanghai
- TAKE_FILE_OWNERSHIP=true # 就是这货 volumes 挂载权限问题
elastichd:
image: containerize/elastichd:latest
container_name: elasticsearch-hd
restart: always
networks:
- net-es
ports:
- "9800:9800"
depends_on:
- "elasticsearch"
kibana:
image: kibana:7.6.2
container_name: kibana7.6.2
restart: always
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
environment:
- TZ=Asia/Shanghai
volumes:
- ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
networks:
- net-es
ports:
- "5601:5601"
#这里要注意,es和eshd要在相同网络才能被links
networks:
net-es:
external: false
启动前所需准备
数据挂载赋权
你直接启动会出现文件无法访问的问题,因为你要做数据挂载,需要往elasticsearch文件夹写入数据,但是你执行用户不是root,docker-compose拉取镜像还有创建的容器实际上都是非root用户创建的所以你在做数据挂载之前,需要多挂载的文件夹进行一个赋权。chmod 777 ./elasticsearch
容器内配置数据挂失败
再次启动还是会出现找不到配置文件的情况 ,所以先把挂载注释掉,先把项目启动起来后,直接运行镜像文件拷贝到当前目录下即可。kibana也同理,为了省略我这里只写了一个作为参考
docker cp elasticsearch7.6.2:/usr/share/elasticsearch/config/ ./elasticsearch/
分词器下载
安装中文分词器ik插件(请确保版本号一定要一致,他那个github版本很多的,一定可以找到属于自己的版本),并且检查下分词器的properties配置文件的版本号是否一致,不一致需要修改,不过只要你下载了自己版本的一般无需修改,如果版本相差很大,启动es可能会有各种异常。
修改es的yml文件和kibana的yml文件
一定要配置network.publish_host 否则boot启动连接es会报错
cluster.name: "es-cluster1"
network.host: 0.0.0.0
network.publish_host: 192.168.1.148
#添加的文件
node.name: es-node1
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
bootstrap.memory_lock: flase
bootstrap.system_call_filter: false
k8s方式部署
es
StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: elasticsearch
labels:
app: elasticsearch
namespace: prod
spec:
serviceName: elasticsearch
replicas: 1
selector:
matchLabels:
app: elasticsearch
template:
metadata:
labels:
app: elasticsearch
spec:
containers:
- name: elasticsearch
image: elasticsearch:7.6.2
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 250m
memory: 2Gi
requests:
cpu: 100m
memory: 1Gi
env:
- name: TZ
value: Asia/Shanghai
ports:
- name: rest
containerPort: 9200
- name: inter
containerPort: 9300
volumeMounts:
- name: es-data
mountPath: /usr/share/elasticsearch/data
- name: es-yaml
mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
- name: es-plugins
mountPath: /usr/share/elasticsearch/plugins/
volumes:
- name: es-data
hostPath:
path: /root/project-server/backend/es/data
type: Directory
- name: es-yaml
hostPath:
path: /root/project-server/backend/es/conf/elasticsearch.yml
type: File
- name: es-plugins
hostPath:
path: /root/project-server/backend/es/plugins/
type: Directory
Service
apiVersion: v1
kind: Service
metadata:
name: elasticsearch
namespace: prod
labels:
app: elasticsearch
spec:
type: NodePort
ports:
- port: 9200
targetPort: 9200
nodePort: 30005
name: es-9200
- port: 9300
targetPort: 9300
nodePort: 30006
name: es-9300
selector:
app: elasticsearch
kibana
StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kibana
labels:
app: kibana
namespace: prod
spec:
serviceName: kibana
replicas: 1
selector:
matchLabels:
app: kibana
template:
metadata:
labels:
app: kibana
spec:
containers:
- name: kibana
image: kibana:7.6.2
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 250m
memory: 1000Mi
requests:
cpu: 100m
memory: 768Mi
env:
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 5601
volumeMounts:
- name: kibana-yaml
mountPath: /usr/share/kibana/config/kibana.yml
- name: elastichd
image: containerize/elastichd:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9800
volumes:
- name: kibana-yaml
hostPath:
path: /root/project-server/backend/kibana/conf/kibana.yml
type: File
Service
apiVersion: v1
kind: Service
metadata:
namespace: prod
name: kibana
labels:
app: kibana
spec:
type: NodePort
ports:
- port: 5601
targetPort: 5601
nodePort: 30007
name: kibana
- port: 9800
targetPort: 9800
nodePort: 30008
name: elastichd
selector:
app: kibana