[ 手把手教學 ] 用 Container 安裝 Elasticsearch 實戰篇

文/Jay|編輯/Anni

看完上篇 [手把手教學文] 三分鐘在 GCE 上部署 Container 若激起你興趣的話,就快來看看如何用 Container 將電商、媒體、網站等業者愛不釋手的搜尋系統 Elasticsearch 打包部署在 GCE 上!

在本地端建立 Docker Image

建立 Dockerfile

因為不需要 X-Pack plugin,所以在 Dockerfile 內將其移除

$ cat >> Dockerfile << EOF
FROM docker.elastic.co/elasticsearch/elasticsearch:5.6.5
ADD elasticsearch.yml /usr/share/elasticsearch/config/
RUN bin/elasticsearch-plugin remove x-pack --purge
USER root
RUN chown elasticsearch:elasticsearch config/elasticsearch.yml
USER elasticsearch
EOF

建立 Elasticsearch 設定檔 Elasticsearch.yml

因為需要從外部連線至 Elasticsearch,所以需要在 elasticsearch.yml 內設定 network.host

$ cat >> elasticsearch.yml << EOF
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
network.host: 0.0.0.0
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 3
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
EOF

建立 Docker Image

$ docker build -t [HOSTNAME]/[PROJECT-ID]/[IMAGE][:TAG] -f Dockerfile .

  • [HOSTNAME] 可以參考這裡
  • [PROJECT-ID] 請填入專案名稱
  • [IMAGE] 請填入 image 名稱
  • (選填) [:TAG] 請填入 tag 名稱

範例:

$ docker build -t us.gcr.io/my-project-id/elasticsearch:5.6.5v1.0.1 -f Dockerfile .

確認 docker images

$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
us.gcr.io/my-project-id/elasticsearch 5.6.5v1.0.1 4004749175b3 2 hours ago 555MB

上傳 Docker Image 至 Container Registry

開啟 Google Container Registry API

可參考官方教學,或使用 gcloud 指令開啟

$ gcloud services enable containerregistry.googleapis.com

使用 gcloud 指令上傳 Docker Image

可參考官方教學,上傳完成後即可在 GCP console 的 Container Registry 中看到 images

$ gcloud docker -- push [HOSTNAME]/[PROJECT-ID]/[IMAGE][:TAG]

icon/enlarge

建立 Elasticsearch Instance

準備建立執行個體

此部分可以從 Container Registry 或是 Compute Engine 頁面開始

  • 從 Container Registry
icon/enlarge

設定執行個體

  • 在 容器選項 勾選 將容器映像檔部署至這個 VM 執行個體
  • 容器映像檔 填入 docker image 名稱
icon/enlarge

主機目錄掛接項目 填入掛載的 data volume 路徑,讓資料能夠儲存在主機上而非 container 中

  • 掛接路徑 填入 /usr/share/elasticsearch/data
  • 主機路徑 填入 /mnt/stateful_partition/elasticsearch
  • 模式 選擇 讀取/寫入
icon/enlarge

防火牆 需開啟 tcp:9200 (for RESTful API with JSON over HTTP)、tcp:9300 (for Java API),請參閱這

icon/enlarge
  • 開機指令碼 填入以下 script 調整系統參數,讓 elasticsearch 能夠正常運行

#!/bin/bash
$ sudo su -
$ echo 262144 > /proc/sys/vm/max_map_count

icon/enlarge

設定完成後點選建立

icon/enlarge

修改主機路徑權限

SSH 連線至 instance 內,執行以下指令建立主機路徑並修改權限

$ sudo chmod 777 /mnt/stateful_partition/elasticsearch/

 

執行個體連線測試

  • SSH 連線至 instance 內進行測試
icon/enlarge

從外部進行連線測試

icon/enlarge
訂閱 CloudMile 電子報

所有 CloudMile 最新消息、產品動態、活動資訊和特別優惠,立即掌握。