一、Elasticsearch安装
拉去镜像:docker pull elasticsearch:5.6.8
安装es容器:docker run -di --name=changgou_elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8
浏览器输入地址访问:http://192.168.211.132: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配置开启远程连接,代码如下:
登录容器
```java
docker exec -it changgou_elasticsearch /bin/bash
查看目录结构 输入: dir
```java
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
修改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
(5)跨域配置
修改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