Server/Docker

Docker 에서 elasticsearch, kibana 설치 (8.13.2)-docker-compose사용

juhpark 2024. 4. 16. 22:42
반응형

 

docker-compose를 사용하여 3개의 elasticsearch 노드와 kibana를 한번에 설치하고 운영하는 할수 있는 방법을 설명한다.

 

curl 설치

먼저 docker-compose 를 사용하면서 서비스의 정상유무를 체크 하기위해 curl 프로그램을 사용하는데, 윈도우 기본적으로 설치되어 있는 curl은 사용이 불가하여, 별도의 curl을 다운로드 받아야 한다. 다음의 링크에서 맞는 버전을 다운로드 받는다.

 

curl for Windows

curl 8.7.1 for Windows These are the latest and most up to date official curl binary builds for Microsoft Windows. curl version: 8.7.1 Build: 8.7.1_7 Date: 2024-03-27 Changes: 8.7.1 changelog curl for 64-bit Size: 7.3 MB sha256: e80a9f569c988e51d1534f53255

curl.se

 

PATH 등록

위에서 다운로드 받은 파일을 C:\Program Files\curl-8.7.1_7 폴더에 복사해 놓고 환경변수 PATH에 등록하여 편하게 사용할 수 있게 한다.

Win+X > 시스템(Y) > 설정창 > 고급시스템설정

 

CMD 창에서 curl -X 실행시 아래의 같이 표시되면 정상적으로 동작하는 것임

 

Powershell 의 기본 curl 을 사용하지 않도록 변경

Window Powershell 에서는 기본적으로  curl이 alias 가 걸려 있어서 curl 실행시 새로 설치된 프로그램이 실행되지 않는다. 그래서 아래의 명령을 실행한 뒤, curl -X 를 실해하면 위의 그림과 같이 표시된다.

remove-item alias:\curl

 

영구적으로  적용하기 위해서는 파워쉘이 실행될때 자동으로 실행되는 profile에 등록이 되어아 하는데, profile이 없는 경우 아래와 같이 실행해서 profile을 생성한다.

New-Item $profile -force -itemtype file

 

이후 노트 패드로 profile을 열어서 편집한다.

notepad $profile

 

profile파일에 remove-item alias:\curl  명령을 입력한다.

 

이후 Powershell 을 실행하면 아래와 같은 오류가 떨어지는데,

 

관리자로 Powershell을 실행후 아래 명령어를 입력하고 Y를 입력하여 실행하면 오류없이 curl을 사용할 수 있다.

Set-ExecutionPolicy RemoteSigned

 

정상적인 실행화면

 

 

docker-compose를 이용한 구성 방법

환경화일 작성

파일명 : .env 

# Password for the 'elastic' user (at least 6 characters)
ELASTIC_PASSWORD=<패스워드입력>

# Password for the 'kibana_system' user (at least 6 characters)
KIBANA_PASSWORD=<패스워드입력>

# Version of Elastic products
STACK_VERSION=8.13.2

# Set the cluster name
CLUSTER_NAME=docker-cluster

SERVER_LIST=es01,es02,es03

# Set to 'basic' or 'trial' to automatically start the 30-day trial
LICENSE=basic
#LICENSE=trial

# Port to expose Elasticsearch HTTP API to the host
#ES_PORT=9200
ES_PORT=127.0.0.1:9200

# Port to expose Kibana to the host
KIBANA_PORT=5601
#KIBANA_PORT=80

# Increase or decrease based on the available host memory (in bytes)
MEM_LIMIT=1073741824

# Project namespace (defaults to the current folder name if not set)
#COMPOSE_PROJECT_NAME=myproject

 

파일명: docker-compose.yml

version: "2.2"

services:
  setup:
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - certs:/usr/share/elasticsearch/config/certs
    user: "0"
    command: >
      bash -c '
        if [ x${ELASTIC_PASSWORD} == x ]; then
          echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
          exit 1;
        elif [ x${KIBANA_PASSWORD} == x ]; then
          echo "Set the KIBANA_PASSWORD environment variable in the .env file";
          exit 1;
        fi;
        if [ ! -f config/certs/ca.zip ]; then
          echo "Creating CA";
          bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
          unzip config/certs/ca.zip -d config/certs;
        fi;
        if [ ! -f config/certs/certs.zip ]; then
          echo "Creating certs";
          echo -ne \
          "instances:\n"\
          "  - name: es01\n"\
          "    dns:\n"\
          "      - es01\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          "  - name: es02\n"\
          "    dns:\n"\
          "      - es02\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          "  - name: es03\n"\
          "    dns:\n"\
          "      - es03\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          > config/certs/instances.yml;
          bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
          unzip config/certs/certs.zip -d config/certs;
        fi;
        echo "Setting file permissions"
        chown -R root:root config/certs;
        find . -type d -exec chmod 750 \{\} \;;
        find . -type f -exec chmod 640 \{\} \;;
        echo "Waiting for Elasticsearch availability";
        until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
        echo "Setting kibana_system password";
        until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
        echo "All done!";
      '
    healthcheck:
      test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
      interval: 1s
      timeout: 5s
      retries: 120

  es01:
    depends_on:
      setup:
        condition: service_healthy
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - certs:/usr/share/elasticsearch/config/certs
      - esdata01:/usr/share/elasticsearch/data
    ports:
      - ${ES_PORT}:9200
    environment:
      - node.name=es01
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=${SERVER_LIST}
      - discovery.seed_hosts=es02,es03
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es01/es01.key
      - xpack.security.http.ssl.certificate=certs/es01/es01.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es01/es01.key
      - xpack.security.transport.ssl.certificate=certs/es01/es01.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  es02:
    depends_on:
      - es01
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - certs:/usr/share/elasticsearch/config/certs
      - esdata02:/usr/share/elasticsearch/data
    environment:
      - node.name=es02
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=${SERVER_LIST}
      - discovery.seed_hosts=es01,es03
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es02/es02.key
      - xpack.security.http.ssl.certificate=certs/es02/es02.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es02/es02.key
      - xpack.security.transport.ssl.certificate=certs/es02/es02.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  es03:
    depends_on:
      - es02
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - certs:/usr/share/elasticsearch/config/certs
      - esdata03:/usr/share/elasticsearch/data
    environment:
      - node.name=es03
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=${SERVER_LIST}
      - discovery.seed_hosts=es01,es02
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es03/es03.key
      - xpack.security.http.ssl.certificate=certs/es03/es03.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es03/es03.key
      - xpack.security.transport.ssl.certificate=certs/es03/es03.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  kibana:
    depends_on:
      es01:
        condition: service_healthy
      es02:
        condition: service_healthy
      es03:
        condition: service_healthy
    image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
    volumes:
      - certs:/usr/share/kibana/config/certs
      - kibanadata:/usr/share/kibana/data
    ports:
      - ${KIBANA_PORT}:5601
    environment:
      - SERVERNAME=kibana
      - ELASTICSEARCH_HOSTS=https://es01:9200
      - ELASTICSEARCH_USERNAME=kibana_system
      - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
      - ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
    mem_limit: ${MEM_LIMIT}
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

volumes:
  certs:
    driver: local
  esdata01:
    driver: local
  esdata02:
    driver: local
  esdata03:
    driver: local
  kibanadata:
    driver: local

 

위 두개의 환경파일을 특정 폴더 (ex: elastic)에 저장하고 docker-compose 명령어를 powershell에서 실행하여 환경을 구성한다.

# 환경구성
docker-compose up -d

# 환경삭제
docker-compose down

# 환경 및 데이터(volume) 전체 삭제
docker-compose down -v

 

환경 구성 결과 화면 (docker-compose up -d)

이미지 다운로드 상태, kibana와 elasticsearch 이미지가 다운로드 되어 있다.

 

컨테이너 실행화면

아래 그림에 폴더명(cloud) 아래로 3개의 elasticsearch 노드가 실행되었고, kibana가 순차적으로 실행된다. 

 

 

이후, https://localhost:5601/ 주소를 웹브라우저에서 실행하면, 로그인 창을 볼수 있다.

ID: elastic 

PW: .env 파일에 입력했던 패스워드

 

 

아래의 화면에서 [Explore on my own]  버튼을 클릭하여 kibana 홈화면으로 이동한다.

 

 

최종 로그인 된 화면이다.

 

최종 로그인 완료된 화면이다.

 

참고, 생성된 컨테이너들 중에 setup-1은 최초 환경구성에만 사용되고 바로 종료된 상태로 계속 유지된다. 

certs:/usr/share/elasticsearch/config/certs
esdata01:/usr/share/elasticsearch/data
esdata02:/usr/share/elasticsearch/data
esdata03:/usr/share/elasticsearch/data

 

volume는 es01은 certs, esdata01, es02는 certs, esdata02, es03은 certs, esdata03의 데이터를 가지게 되는데, docker-compose down 을 실행해도, volume는 사라지지 않으며, 다시 docker-compose up -d 로 생성하게 되면, 데이터는 동일하게 유지 됨을 알수 있다. 

데이터까지 모두 삭제하기 위해서는 docker-compose down -v 명령을 통해 완전히 삭제할 수 있다.

 

반응형