首页 技术 正文
技术 2022年11月15日
0 收藏 578 点赞 4,550 浏览 7260 个字

一、介绍

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

二、安装docker

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

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

安装docker

yum安装docker

yum install -y docker-io

需要添加国内镜像源

vim /etc/docker/daemon.json

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

{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

重启docker服务

systemctl restart docker

安装docker命令补全工具

yum install -y bash-completion

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

下载centos系统镜像

docker pull centos

这个镜像就是centos7的

三、安装elasticsearch

下载rpm安装就可以了

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm

使用centos镜像启动一个容器

docker run -it docker.io/centos /bin/bash

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

yum install -y wget java-1.8.-openjdk && yum clean all

下载elasticsearch的rpm包并安装

rpm -ivh elasticsearch-6.2..rpm && rm -f elasticsearch-6.2..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

启动elasticsearch服务

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

mkdir /opt/elasticsearch
vi /opt/elasticsearch/Dockerfile

内容如下:

FROM centosRUN yum install -y wget java-1.8.-openjdk && yum clean all && \
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm && \
rpm -ivh elasticsearch-6.2..rpm && rm -f elasticsearch-6.2..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
ENTRYPOINT runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"

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

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

EXPOSE 9200 表示要暴露的端口号

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

runuser 指定用户执行命令。

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

编排镜像

docker build -t elasticsearch /opt/elasticsearch

启动容器

docker run -it elasticsearch

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

输出如下:

runuser: warning: cannot change directory to /home/elasticsearch: No such file or directory
OpenJDK -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
[--07T10::,][INFO ][o.e.n.Node ] [] initializing ...
[--07T10::,][INFO ][o.e.e.NodeEnvironment ] [qDmU4u_] using [] data paths, mounts [[/ (rootfs)]], net usable_space [15gb], net total_space [.9gb], types [rootfs]
[--07T10::,][INFO ][o.e.e.NodeEnvironment ] [qDmU4u_] heap size [.6mb], compressed ordinary object pointers [true]
[--07T10::,][INFO ][o.e.n.Node ] node name [qDmU4u_] derived from node ID [qDmU4u_NTNKmpXVV-5vlEQ]; set [node.name] to override
[--07T10::,][INFO ][o.e.n.Node ] version[6.2.], pid[], build[ccec39f/--12T20::.497551Z], OS[Linux/3.10.-.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK -Bit Server VM/1.8.0_191/25.191-b12]
[--07T10::,][INFO ][o.e.n.Node ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=, -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=, -XX:GCLogFileSize=64m, -Des.path.home=/usr/share/elasticsearch, -Des.path.conf=/etc/elasticsearch]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [aggs-matrix-stats]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [analysis-common]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [ingest-common]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [lang-expression]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [lang-mustache]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [lang-painless]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [mapper-extras]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [parent-join]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [percolator]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [rank-eval]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [reindex]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [repository-url]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [transport-netty4]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] loaded module [tribe]
[--07T10::,][INFO ][o.e.p.PluginsService ] [qDmU4u_] no plugins loaded
[--07T10::,][INFO ][o.e.d.DiscoveryModule ] [qDmU4u_] using discovery type [zen]
[--07T10::,][INFO ][o.e.n.Node ] initialized
[--07T10::,][INFO ][o.e.n.Node ] [qDmU4u_] starting ...
[--07T10::,][INFO ][o.e.t.TransportService ] [qDmU4u_] publish_address {172.17.0.2:}, bound_addresses {[::]:}
[--07T10::,][INFO ][o.e.b.BootstrapChecks ] [qDmU4u_] bound or publishing to a non-loopback address, enforcing bootstrap checks
[--07T10::,][INFO ][o.e.c.s.MasterService ] [qDmU4u_] zen-disco-elected-as-master ([] nodes joined), reason: new_master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:}
[--07T10::,][INFO ][o.e.c.s.ClusterApplierService] [qDmU4u_] new_master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:}, reason: apply cluster state (from master [master {qDmU4u_}{qDmU4u_NTNKmpXVV-5vlEQ}{Terj8KYoQvWwHYsUYkNNyA}{172.17.0.2}{172.17.0.2:} committed version [] source [zen-disco-elected-as-master ([] nodes joined)]])
[--07T10::,][INFO ][o.e.h.n.Netty4HttpServerTransport] [qDmU4u_] publish_address {172.17.0.2:}, bound_addresses {[::]:}
[--07T10::,][INFO ][o.e.n.Node ] [qDmU4u_] started
[--07T10::,][INFO ][o.e.g.GatewayService ] [qDmU4u_] recovered [] indices into cluster_state

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

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

docker run -p 9200:9200 -d -it el

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

-d 表示后台运行

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

等待10秒,查看端口状态

[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.0.0:* LISTEN /sshd
tcp 127.0.0.1: 0.0.0.0:* LISTEN /master
tcp 192.168.91.133: 192.168.91.1: ESTABLISHED /sshd: root@pt
tcp6 0 0 :::9200 :::* LISTEN 17942/docker-proxy-
tcp6 ::: :::* LISTEN /sshd
tcp6 ::: :::* LISTEN /master

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

访问url

http://192.168.91.133:9200/

页面输出:

{
"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

systemctl daemon-reload
systemctl start elasticsearch.service
systemctl enable elasticsearch.service

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

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

所以实际运行用户还是elasticsearch

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298