基於docker 搭建Elasticsearch6.2.4(centos)


一、介紹

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 /bin/bash 

 

四、編寫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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM