码迷,mamicode.com
首页 > 其他好文 > 详细

Docker 下Elasticsearch 的安装 和ik分词器

时间:2021-02-16 12:12:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:reboot   git   elastic   没有   错误   区域   进入   exp   word   

(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配置开启远程连接,代码如下:

登录容器

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

2. IK分词器安装

(1)安装ik分词器

IK分词器下载地址https://github.com/medcl/elasticsearch-analysis-ik/releases

将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

(2)IK分词器测试

访问:http://192.168.211.132:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员

 

技术图片

 

访问:http://192.168.211.132:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员

 

技术图片

 

 

 

 
 

Docker 下Elasticsearch 的安装 和ik分词器

标签:reboot   git   elastic   没有   错误   区域   进入   exp   word   

原文地址:https://www.cnblogs.com/yuyyg/p/14399065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!