Elasticsearch安装
- docker镜像下载
docker pull elasticsearch:5.6.8
docker run -di --name=elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8
9200端口(Web管理平台端口) 9300(服务默认端口)
-
浏览器输入地址访问:http://192.168.xx.xx:9200/
-
开启远程连接
上面完成安装后,es并不能正常使用,elasticsearch从5版本以后默认不开启远程连接
,程序直接连接会报如下错误:
failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{5ttLpMhkRjKLkvoY7ltUWg}{192.168.211.132}{192.168.211.132:9300}]
我们需要修改es配置开启远程连接
,代码如下:
- 登录容器
docker exec -it elasticsearch /bin/bash
- 查看目录结构 输入: dir
root@07f22eb41bb5:/usr/share/elasticsearch# dir
NOTICE.txt README.textile bin config data lib logs modules plugins
- 进入config目录
cd config
- 查看文件
root@07f22eb41bb5:/usr/share/elasticsearch/config# ls
elasticsearch.yml log4j2.properties scripts
- 修改elasticsearch.yml文件
root@07f22eb41bb5:/usr/share/elasticsearch/config# vi elasticsearch.yml
bash: vi: command not found
vi命令无法识别,因为docker容器里面没有该命令,我们可以安装该编辑器。
- 安装vim编辑器
apt-get update
apt-get install vim
- 安装好了后,修改
elasticsearch.yml
配置,如下图:
vi elasticsearch.yml
- 修改如下图:
- 同时添加下面一行代码:
cluster.name: my-application
- 重启docker
docker restart changgou_elasticsearch
- 系统参数配置
重启后发现重启启动失败了,这时什么原因呢?这与我们刚才修改的配置有关,因为elasticsearch在启动的时候会进行一些检查,比如最多打开的文件的个数以及虚拟内存区域数量等等,如果你放开了此配置,意味着需要打开更多的文件以及虚拟内存,所以我们还需要系统调优
修改vi /etc/security/limits.conf
,追加内容 (nofile是单个进程允许打开的最大文件个数 soft nofile 是软限制 hard nofile是硬限制 )
* soft nofile 65536
* hard nofile 65536
修改vi /etc/sysctl.conf
,追加内容 (限制一个进程可以拥有的VMA(虚拟内存区域)的数量 )
vm.max_map_count=655360
- 执行下面命令 修改内核参数马上生效
sysctl -p
- 重新启动虚拟机,再次启动容器,发现已经可以启动并远程访问
reboot
- 跨域配置
修改elasticsearch/config
下的配置文件:elasticsearch.yml
,增加以下三句命令,并重启:
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 192.168.211.132
其中:
http.cors.enabled: true: 此步为允许elasticsearch跨域访问,默认是false。
http.cors.allow-origin: “*”:表示跨域访问允许的域名地址(*表示任意)。
- 重启
docker restart changgou_elasticsearch
小提示:如果想让容器开启重启,可以执行下面命令
docker update --restart=always 容器名称或者容器id
注意:
docker 中es内存不足怎么办?
# 进入docker 中的es里
docker exec -it elasticsearch /bin/bash
# 找到JVM.options
cd /etc/elasticsearch
vim jvm.options
2. IK分词器安装
IK分词器下载地址https://github.com/medcl/elasticsearch-analysis-ik/releases 下载的版本与elasticsearch对应!!!!
- 将ik分词器上传到服务器上,然后解压,并改名字为ik
unzip elasticsearch-analysis-ik-5.6.8.zip
mv elasticsearch ik
- 将ik目录拷贝到docker容器的plugins目录下
docker cp ./ik changgou_elasticsearch:/usr/share/elasticsearch/plugins
- IK分词器测试
访问:http://192.168.xx.xx:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员
访问:http://192.168.211.132:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员
3 Kibana下载安装
- 镜像下载
docker pull docker.io/kibana:5.6.8
- 安装kibana容器
执行如下命令,开始安装kibana容器
docker run -it -d -e ELASTICSEARCH_URL=http://192.168.xx.xx:9200 --name kibana --restart=always -p 5601:5601 kibana:5.6.8
ELASTICSEARCH_URL=http://192.168.xx.xx:9200:是指链接的ES地址
restart=always:每次服务都会重启,也就是开启启动
5601:5601:端口号
- 访问测试