這次來試玩一下傳說中的 Elasticsearch,主要是想要把網站記錄的 Log 存到這裡面來,再設定一些 UI 來顯示,就不用每次查網站的 Log 都要進資料庫用 SQL 查。
Elasticsearch 有雲端版和自己架設,雲端要付費,自架免費。對於喜歡亂玩又沒什麼錢的我,當然選擇自己架設。
主要以 single-node cluster 為主,版本為 8.0.0
方法一:各 image 逐個建立
建議搭配 volumn(範例中未使用)
1. Docker 建立專用 network
1
| docker network create elastic
|
2. 安裝 Elasticsearch
1 2
| docker pull docker.elastic.co/elasticsearch/elasticsearch:8.1.0 docker run -e ES_JAVA_OPTS="-Xms1g -Xmx1g" --name es-node01 --net elastic -p 9200:9200 -p 9300:9300 -t docker.elastic.co/elasticsearch/elasticsearch:8.1.0
|
備註:
設定 JVM 使用記憶體的大小
-e ES_JAVA_OPTS="-Xms1g -Xmx1g"
最大使用 1G,最小使用 1G
https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html#set-jvm-heap-size
port 9200 為對外使用,9300 為內部 node 溝通使用
裝好後會有帳號、密碼、enroll token(有效30分鐘)等資料,可先複製起來
3. 安裝 Kibana
1 2
| docker pull docker.elastic.co/kibana/kibana:8.1.0 docker run --name kib-01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.1.0
|
4. 開啟 Kibana
輸入 http://localhost:5601 開啟 Kibana,首次開啟需要使用上面的 enroll token 綁定 Elasticsearch,然後輸入剛剛的帳號和密碼
5. 完成
可以愉快地玩耍了
方法二:使用 docker-compose
有搭配volume
1. 建立 .env 檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| # 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.0.0
# Set the cluster name CLUSTER_NAME=docker-cluster
# 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
|
2. 建立 docker-compose.yml
此處會建立
三個 container:
setup:建立憑證用,建立好之後會關閉
es01:Elasticsearch 1號(只建立一個節點)
kibana:Kibana
一個 network:
elastic-default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| 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 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 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"\ > 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=es01 - 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.http.ssl.verification_mode=certificate - 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
kibana: depends_on: es01: 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 kibanadata: driver: local
|
3. 開始執行
將 .env 和 docker-compose.yml 放在同一個資料夾,然後到該資料夾執行指令
4. 完成
可以愉快地玩耍了
注意事項
如果遇到 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
的錯誤,代表 kernel 中 vm.max_map_count
的設定需要調整為至少 262144
。
調整方法
Windows Docker Desktop WSL 2:
1 2
| wsl -d docker-desktop sysctl -w vm.max_map_count=262144
|
macOS Docker Desktop:
1 2
| docker-machine ssh sudo sysctl -w vm.max_map_count=262144
|
參考資料