一、介绍
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.0-openjdk && yum clean all
下载elasticsearch的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
启动elasticsearch服务
runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"
注意:不能使用systemctl命令启动elasticsearch服务,必须要以特权模式运行才行!
比如 docker run -it docker.io/centos privileged=true
四、编写Dockerfile
Dockerfile
新建一个空目录,编译文件Dockerfile
mkdir /opt/elasticsearch
vi /opt/elasticsearch/Dockerfile
内容如下:
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 9200
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 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端口
但是一般,我们需要将本机和容器做一个端口映射,要这样启动容器
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: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
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