前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于docker 搭建Elasticsearch6.2.4(centos)

基于docker 搭建Elasticsearch6.2.4(centos)

作者头像
py3study
发布2020-01-20 11:26:46
4970
发布2020-01-20 11:26:46
举报
文章被收录于专栏:python3python3

一、介绍

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

二、安装docker

本文使用的是centos7.5(CentOS-7-x86_64-Minimal-1804) 系统。

请确保本机有2G的内存。因为elasticsearch会占用1G内存。

安装docker

yum安装docker

代码语言:javascript
复制
yum install -y docker-io

需要添加国内镜像源

代码语言:javascript
复制
vim /etc/docker/daemon.json

默认内容是{},修改效果如下:

代码语言:javascript
复制
{  "registry-mirrors": ["https://registry.docker-cn.com"]
}

重启docker服务

代码语言:javascript
复制
systemctl restart docker

安装docker命令补全工具

代码语言:javascript
复制
yum install -y bash-completion

注意:必须要退出终端,重新登录一次才能生效。

下载centos系统镜像

代码语言:javascript
复制
docker pull centos

这个镜像就是centos7的

三、安装elasticsearch

下载rpm安装就可以了

代码语言:javascript
复制
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm

使用centos镜像启动一个容器

代码语言:javascript
复制
docker run -it docker.io/centos /bin/bash

进入容器之后,先安装wget和java,并清理rpm包

代码语言:javascript
复制
yum install -y wget java-1.8.0-openjdk && yum clean all

下载elasticsearch的rpm包并安装

代码语言:javascript
复制
rpm -ivh elasticsearch-6.2.4.rpm && rm -f elasticsearch-6.2.4.rpm

修改配置文件

代码语言:javascript
复制
sed -i '55s/#network.host: 192.168.0.1/network.host: 0.0.0.0/g' /etc/elasticsearch/elasticsearch.yml 
sed -i '59s/#http.port: 9200/http.port: 9200/g' /etc/elasticsearch/elasticsearch.yml

启动elasticsearch服务

代码语言:javascript
复制
runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"

注意:不能使用systemctl命令启动elasticsearch服务,必须要以特权模式运行才行!

比如 docker run -it docker.io/centos privileged=true /bin/bash 

四、编写Dockerfile

Dockerfile

新建一个空目录,编译文件Dockerfile

代码语言:javascript
复制
mkdir /opt/elasticsearchvi /opt/elasticsearch/Dockerfile

内容如下:

复制代码
复制代码
代码语言:javascript
复制
FROM centos

RUN yum install -y wget java-1.8.0-openjdk && yum clean all && \
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm && \
    rpm -ivh elasticsearch-6.2.4.rpm && rm -f elasticsearch-6.2.4.rpm && \    sed -i '55s/#network.host: 192.168.0.1/network.host: 0.0.0.0/g' /etc/elasticsearch/elasticsearch.yml && \    sed -i '59s/#http.port: 9200/http.port: 9200/g' /etc/elasticsearch/elasticsearch.yml
EXPOSE 9200ENTRYPOINT runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"
复制代码
复制代码

注意:每执行一次RUN,镜像就会增加一层。层数越多,镜像体积越大。

为了避免多次RUN,把相关命令统一到一个RUN中。

EXPOSE 9200 表示要暴露的端口号

ENTRYPOINT 表示镜像run起来之后,默认要执行的命令

runuser 指定用户执行命令。

  • -s 指定环境变量
  • -l 指定用户
  • -c 执行的命令 

编排镜像

代码语言:javascript
复制
docker build -t elasticsearch /opt/elasticsearch

启动容器

代码语言:javascript
复制
docker run -it elasticsearch

默认会直接调用命令 runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"

输出如下:

复制代码
复制代码
代码语言:javascript
复制
runuser: warning: cannot change directory to /home/elasticsearch: No such file or directory
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
[2018-11-07T10:18:54,057][INFO ][o.e.n.Node               ] [] initializing ...
[2018-11-07T10:18:54,198][INFO ][o.e.e.NodeEnvironment    ] [qDmU4u_] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [15gb], net total_space [16.9gb], types [rootfs]
[2018-11-07T10:18:54,198][INFO ][o.e.e.NodeEnvironment    ] [qDmU4u_] heap size [1015.6mb], compressed ordinary object pointers [true]
[2018-11-07T10:18:54,202][INFO ][o.e.n.Node               ] node name [qDmU4u_] derived from node ID [qDmU4u_NTNKmpXVV-5vlEQ]; set [node.name] to override
[2018-11-07T10:18:54,202][INFO ][o.e.n.Node               ] version[6.2.4], pid[5], build[ccec39f/2018-04-12T20:37:28.497551Z], OS[Linux/3.10.0-862.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_191/25.191-b12]
[2018-11-07T10:18:54,202][INFO ][o.e.n.Node               ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch.C8ZXNqCd, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=/var/lib/elasticsearch, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, -XX:+PrintTenuringDistribution, -XX:+PrintGCApplicationStoppedTime, -Xloggc:/var/log/elasticsearch/gc.log, -XX:+UseGCLogFileRotation, -XX:NumberOfGCLogFiles=32, -XX:GCLogFileSize=64m, -Des.path.home=/usr/share/elasticsearch, -Des.path.conf=/etc/elasticsearch]
[2018-11-07T10:18:55,827][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [aggs-matrix-stats]
[2018-11-07T10:18:55,827][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [analysis-common]
[2018-11-07T10:18:55,827][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [ingest-common]
[2018-11-07T10:18:55,831][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [lang-expression]
[2018-11-07T10:18:55,831][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [lang-mustache]
[2018-11-07T10:18:55,831][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [lang-painless]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [mapper-extras]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [parent-join]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [percolator]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [rank-eval]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [reindex]
[2018-11-07T10:18:55,832][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [repository-url]
[2018-11-07T10:18:55,833][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [transport-netty4]
[2018-11-07T10:18:55,833][INFO ][o.e.p.PluginsService     ] [qDmU4u_] loaded module [tribe]
[2018-11-07T10:18:55,833][INFO ][o.e.p.PluginsService     ] [qDmU4u_] no plugins loaded
[2018-11-07T10:19:00,949][INFO ][o.e.d.DiscoveryModule    ] [qDmU4u_] using discovery type [zen]
[2018-11-07T10:19:02,075][INFO ][o.e.n.Node               ] initialized
[2018-11-07T10:19:02,075][INFO ][o.e.n.Node               ] [qDmU4u_] starting ...
[2018-11-07T10:19:02,531][INFO ][o.e.t.TransportService   ] [qDmU4u_] publish_address {172.17.0.2:9300}, bound_addresses {[::]:9300}
[2018-11-07T10:19:02,567][INFO ][o.e.b.BootstrapChecks    ] [qDmU4u_] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2018-11-07T10:19:05,811][INFO ][o.e.c.s.MasterService    ] [qDmU4u_] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:9300}
[2018-11-07T10:19:05,829][INFO ][o.e.c.s.ClusterApplierService] [qDmU4u_] new_master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:9300}, reason: apply cluster state (from master [master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:9300} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
[2018-11-07T10:19:05,887][INFO ][o.e.h.n.Netty4HttpServerTransport] [qDmU4u_] publish_address {172.17.0.2:9200}, bound_addresses {[::]:9200}
[2018-11-07T10:19:05,887][INFO ][o.e.n.Node               ] [qDmU4u_] started
[2018-11-07T10:19:05,897][INFO ][o.e.g.GatewayService     ] [qDmU4u_] recovered [0] indices into cluster_state
复制代码
复制代码

它会一值hold住,监听9200端口

但是一般,我们需要将本机和容器做一个端口映射,要这样启动容器

代码语言:javascript
复制
docker run -p 9200:9200 -d -it el

-p 表示端口映射,hostPort : containerPort,左边是本机的,右边是容器的

-d 表示后台运行

注意:这个2个参数要写在前面,不能写在后面

等待10秒,查看端口状态

复制代码
复制代码
代码语言:javascript
复制
[root@localhost el]# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      813/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1190/master         
tcp        0      0 192.168.91.133:22       192.168.91.1:56367      ESTABLISHED 11374/sshd: root@pt 
tcp6       0      0 :::9200                 :::*                    LISTEN      17942/docker-proxy- tcp6       0      0 :::22                   :::*                    LISTEN      813/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1190/master
复制代码
复制代码

通过以上信息,就可以看到端口起来了

访问url

代码语言:javascript
复制
http://192.168.91.133:9200/

页面输出:

复制代码
复制代码
代码语言:javascript
复制
{  "name" : "-sawdKe",  "cluster_name" : "elasticsearch",  "cluster_uuid" : "_7kUiLEyQBSnLQSOGxijtw",  "version" : {    "number" : "6.2.4",    "build_hash" : "ccec39f",    "build_date" : "2018-04-12T20:37:28.497551Z",    "build_snapshot" : false,    "lucene_version" : "7.2.1",    "minimum_wire_compatibility_version" : "5.6.0",    "minimum_index_compatibility_version" : "5.0.0"
  },  "tagline" : "You Know, for Search"}
复制代码
复制代码

备注:

如果不使用容器安装elasticsearch,直接使用以下命令,就可以启动elasticsearch

代码语言:javascript
复制
systemctl daemon-reload
systemctl start elasticsearch.service
systemctl enable elasticsearch.service

不要天真的以为,它就是以root用户运行的

看/usr/lib/systemd/system/elasticsearch 文件,它里面定义了运行用户是elasticsearch

所以实际运行用户还是elasticsearch

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、介绍
  • 二、安装docker
    • 安装docker
      • 下载centos系统镜像
      • 三、安装elasticsearch
      • 四、编写Dockerfile
        • Dockerfile
          • 编排镜像
            • 启动容器
            相关产品与服务
            Elasticsearch Service
            腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档