Loading

ElasticSearch安装

Elasticsearch 安装

(1)docker镜像下载

docker pull elasticsearch:5.6.8

(2)安装es容器

docker run -di --name=changgou_elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8

9200端口(Web管理平台端口) 9300(服务默认端口)

浏览器输入地址访问:http://192.168.211.132:9200/

 

(3)开启远程连接

上面完成安装后,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配置开启远程连接,代码如下:

进入es容器

docker exec -it changgou_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

(4)系统参数配置

重启后发现重启启动失败了,这时什么原因呢?这与我们刚才修改的配置有关,因为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

 

posted @ 2021-11-28 14:24  1640808365  阅读(46)  评论(0编辑  收藏  举报