[Elasticsearch] elasticsearch-docker 만들어 보기
Elastic/Elasticsearch 2018. 4. 17. 18:14이미 Elastic 사에서 docker 를 제공하고 있습니다.
정보는 아래 링크를 참고하세요.
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
https://github.com/elastic/elasticsearch-docker
https://docs.docker.com/get-started/
Docker 를 이용해서 이미지를 만들어 보고 실행 및 클러스터 구성을 어떻게 하는지 단계 별 실행 코드를 따라 하면서 배워 보도록 하겠습니다.
그냥 따라만 하시면 될 겁니다. (Maybe)
제 개발 환경은 mac 입니다.
- docker 당연히 설치가 되어 있어야 합니다.
그 이외는 음... 그냥 개발자시면 쉽게 하실 수 있을 실거예요. ^^
Step 1) 이미지를 만들기 위한 repo 를 하나 만듭니다.
$ mkdir elasticsearch-docker
$ cd elasticsearch-docker
Step 2) 이미지에 패키징 할 패키지들을 준비해 둡니다.
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
$ download jdk-8u152-linux-x64.tar.gz
Step 3) 혹시 커스텀 설정을 하실 거라면 만들어 보세요.
$ mkdir config
$ touch elasticsearch.yml
$ touch jvm.options
$ touch log4j2.properties
# elasticsearch default config 파일로 구성을 합니다.
# 또는 변경이 필요할 경우 작성을 합니다.
Step 4) 이미지 생성을 위한 도커 파일을 만듭니다.
$ vi Dockerfile
From ubuntu:14.04
MAINTAINER HENRY JEONG sophistlv@gmail.com
ARG arg_user_home
ARG arg_es_version
ENV PATH $arg_user_home/apps/elasticsearch/bin:$arg_user_home/apps/jdk/bin:$PATH
ENV ES_HOME $arg_user_home/apps/elasticsearch
ENV JAVA_HOME $arg_user_home/apps/jdk
RUN groupadd -g 1000 elasticsearch && useradd elasticsearch -u 1000 -g 1000 -m -b $arg_user_home -d $arg_user_home -s /bin/bash
USER 1000
# 기본 디렉토리를 생성 합니다.
WORKDIR $arg_user_home
RUN mkdir apps
# 현재 작업 디렉토리를 선언 합니다.
WORKDIR $arg_user_home/apps
# 아래와 같이 다운로드를 받거나 이미 받아 놓은 파일을 추가 합니다.
#RUN wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$arg_es_version.tar.gz
COPY elasticsearch-$arg_es_version.tar.gz .
COPY jdk-8u152-linux-x64.tar.gz .
# 압축을 해제 합니다.(자동으로 해제 됩니다.)
RUN tar -xvzf elasticsearch-$arg_es_version.tar.gz
RUN tar -xvzf jdk-8u152-linux-x64.tar.gz
RUN rm -f elasticsearch-$arg_es_version.tar.gz
RUN rm -f jdk-8u152-linux-x64.tar.gz
# 설정 파일을 추가 합니다.
COPY config/elasticsearch.yml elasticsearch-$arg_es_version/config/
COPY config/jvm.options elasticsearch-$arg_es_version/config/
COPY config/log4j2.properties elasticsearch-$arg_es_version/config/
# symlink 를 설정 합니다.
RUN ln -s elasticsearch-$arg_es_version elasticsearch
RUN ln -s jdk1.8.0_152 jdk
# WORKDIR 을 변경 합니다.
WORKDIR elasticsearch-$arg_es_version
RUN set -ex && for path in data logs config/scripts; do \
mkdir -p "$path"; \
chown -R elasticsearch:elasticsearch "$path"; \
done
# 데몬을 실행 합니다.
#CMD bin/elasticsearch
EXPOSE 9200 9201 9300 9301
Step 5) 이미지를 생성 합니다.
$ docker build -t elasticsearch-6.2.3-ubuntu-14.04-jdk8u152 . --build-arg arg_user_home=/home/es --build-arg arg_es_version=6.2.3
Sending build context to Docker daemon 218.9MB
Step 1/26 : From ubuntu:14.04
---> 67759a80360c
Step 2/26 : MAINTAINER HENRY JEONG sophistlv@gmail.com
---> Using cache
---> be0de8404ce2
Step 3/26 : ARG arg_user_home
---> Using cache
---> 9cad1b4a18c5
Step 4/26 : ARG arg_es_version
---> Using cache
---> 693e1b4ae411
Step 5/26 : ENV PATH $arg_user_home/apps/elasticsearch/bin:$arg_user_home/apps/jdk/bin:$PATH
---> Using cache
---> 5a249008ab07
Step 6/26 : ENV ES_HOME $arg_user_home/apps/elasticsearch
---> Using cache
---> 095bc65a6dfb
Step 7/26 : ENV JAVA_HOME $arg_user_home/apps/jdk
---> Using cache
---> cb02bd60ea16
Step 8/26 : RUN groupadd -g 1000 elasticsearch && useradd elasticsearch -u 1000 -g 1000 -m -b $arg_user_home -d $arg_user_home -s /bin/bash
---> Running in 64ebd0c6b8cf
Removing intermediate container 64ebd0c6b8cf
---> 49759958cddd
Step 9/26 : USER 1000
---> Running in 849c7bde5f44
Removing intermediate container 849c7bde5f44
---> e0f04944d08c
Step 10/26 : WORKDIR $arg_user_home
Removing intermediate container a7e04ccfe107
---> c5b262000886
Step 11/26 : RUN mkdir apps
---> Running in 0272f8c59b01
Removing intermediate container 0272f8c59b01
---> 5e860b3ae335
Step 12/26 : WORKDIR $arg_user_home/apps
Removing intermediate container 093bbfc52201
---> 541e34eb3f99
Step 13/26 : COPY elasticsearch-$arg_es_version.tar.gz .
---> b7f35e466950
Step 14/26 : COPY jdk-8u152-linux-x64.tar.gz .
---> d47f3d93cfd6
Step 15/26 : RUN tar -xvzf elasticsearch-$arg_es_version.tar.gz
---> Running in ec1b02ec10b4
...중략...
Removing intermediate container ec1b02ec10b4
---> 79d8bec8ee89
Step 16/26 : RUN tar -xvzf jdk-8u152-linux-x64.tar.gz
---> Running in c8ae086e2868
Removing intermediate container b94059a73a2c
---> 6fa8928d2367
...중략...
Step 17/26 : RUN rm -f elasticsearch-$arg_es_version.tar.gz
---> Running in beec4cdec4eb
Removing intermediate container beec4cdec4eb
---> 0c30468566ea
Step 18/26 : RUN rm -f jdk-8u152-linux-x64.tar.gz
---> Running in 998cd2eb3d33
Removing intermediate container 998cd2eb3d33
---> 8e1d588a3bbc
Step 19/26 : COPY config/elasticsearch.yml elasticsearch-$arg_es_version/config/
---> b2faa21e1692
Step 20/26 : COPY config/jvm.options elasticsearch-$arg_es_version/config/
---> 36c3ce020763
Step 21/26 : COPY config/log4j2.properties elasticsearch-$arg_es_version/config/
---> 48314fe634e1
Step 22/26 : RUN ln -s elasticsearch-$arg_es_version elasticsearch
---> Running in 2f7610ab8ce4
Removing intermediate container 2f7610ab8ce4
---> 539eb5fce438
Step 23/26 : RUN ln -s jdk1.8.0_152 jdk
---> Running in 1f4585206800
Removing intermediate container 1f4585206800
---> f61c467a833b
Step 24/26 : WORKDIR elasticsearch-$arg_es_version
Removing intermediate container 786de237b577
---> a92587ddf7bf
Step 25/26 : RUN set -ex && for path in data logs config/scripts; do mkdir -p "$path"; chown -R elasticsearch:elasticsearch "$path"; done
---> Running in dfe771b1d02d
+ mkdir -p data
+ chown -R elasticsearch:elasticsearch data
+ mkdir -p logs
+ chown -R elasticsearch:elasticsearch logs
+ mkdir -p config/scripts
+ chown -R elasticsearch:elasticsearch config/scripts
Removing intermediate container dfe771b1d02d
---> 90193d0f5dcc
Step 26/26 : EXPOSE 9200 9201 9300 9301
---> Running in b14404086bc4
Removing intermediate container b14404086bc4
---> a2c69d28a015
Successfully built a2c69d28a015
Successfully tagged elasticsearch-6.2.3-ubuntu-14.04-jdk8u152:latest
Step 6) Elasticsearch 가 사용할 네트워크를 생성 합니다.
$ docker network create es-net
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
ed35ea6eb949 bridge bridge local
a6c8b4fd7996 es-net bridge local
f3ab4273e30a host host local
9d6aa0be922f none null local
Step 7) 만들어진 이미지가 잘 되었는지 한번 명령어를 실행 시켜 봅니다.
$ docker run elasticsearch-6.2.3-ubuntu-14.04-jdk8u152 ls -al
total 248
drwxr-xr-x 1 elasticsearch elasticsearch 4096 Apr 17 07:45 .
drwxr-xr-x 1 elasticsearch elasticsearch 4096 Apr 17 07:45 ..
-rw-r--r-- 1 elasticsearch elasticsearch 11358 Mar 13 10:02 LICENSE.txt
-rw-r--r-- 1 elasticsearch elasticsearch 191887 Mar 13 10:07 NOTICE.txt
-rw-r--r-- 1 elasticsearch elasticsearch 9268 Mar 13 10:02 README.textile
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Apr 17 07:44 bin
drwxr-xr-x 1 elasticsearch elasticsearch 4096 Apr 17 07:45 config
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Apr 17 07:45 data
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Mar 13 10:08 lib
drwxr-xr-x 1 elasticsearch elasticsearch 4096 Mar 13 10:08 logs
drwxr-xr-x 16 elasticsearch elasticsearch 4096 Mar 13 10:08 modules
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Mar 13 10:08 plugins
Step 8) 마지막으로 클러스터 구성을 테스트 합니다.
# -d 를 추가 하면 daemonize 로 동작
$ docker run -p 9200:9200 -p 9300:9300 elasticsearch-6.2.3-ubuntu-14.04-jdk8u152 bin/elasticsearch
# Single node 실행
$ docker run --memory=2g -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 -u 1000 elasticsearch-6.2.3-ubuntu-14.04-jdk8u152:latest \
bin/elasticsearch \
-Ecluster.name=cluster-docker \
-Enode.name=node-docker1 \
-Enode.master=true \
-Enode.data=true \
-Ediscovery.zen.minimum_master_nodes=1 \
-Enetwork.host=0.0.0.0 \
-Ediscovery.zen.ping.unicast.hosts=192.168.221.237:9301 \
-Ehttp.port=9200 \
-Etransport.tcp.port=9300
$ docker run --memory=2g -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9201:9200 -p 9301:9300 -u 1000 elasticsearch-6.2.3-ubuntu-14.04-jdk8u152:latest \
bin/elasticsearch \
-Ecluster.name=cluster-docker \
-Enode.name=node-docker2 \
-Enode.master=false \
-Enode.data=true \
-Ediscovery.zen.minimum_master_nodes=1 \
-Enetwork.host=0.0.0.0 \
-Ediscovery.zen.ping.unicast.hosts=192.168.221.237:9300 \
-Ehttp.port=9200 \
-Etransport.tcp.port=9300
별로 어려운것 없이 쉽게 할 수 있습니다.
여기서 개선 및 업그레이드는 각자 편하게 하시면 될 것 같습니다.