tienyulin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Elasticsearch (七) - 多節點 Cluster 建立搭配 Kibana 在 [Elasticsearch (二) - 快速搭建與 Document 的建立、更新和刪除](https://tienyulin.github.io/elasticsearch-kibana-command-dsl-docker-compose/) 中,我們用 Docker Compose 建立了單節點的 Elasticsearch server 和 Kibana,本篇要來介紹如何用 Docker Compose 建立多節點的 Elasticsearch server 和 Kibana。 在建立前要先提醒一下,官方所提供的文件有許多坑是要注意的,如果對於 Docker Compose 不夠熟就很容易掉進坑裡。尤其是要搭配 Kibana 的地方要多加注意。本篇會以 Elasticsearch 7.8 和 Kibana 7.8 來做範例。 <!-- more --> ## Elasticsearch Cluster 建立多節點的 Elasticsearch cluster 在 [Elasticsearch 官網](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/docker.html#docker-compose-file) 就有提供 docker-compose.yml 的內容,可以直接複製下來使用。如下 : ```yaml= version: '2.2' services: es01: image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1 container_name: es01 environment: - node.name=es01 - cluster.name=es-docker-cluster - discovery.seed_hosts=es02,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data01:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - elastic es02: image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1 container_name: es02 environment: - node.name=es02 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data02:/usr/share/elasticsearch/data networks: - elastic es03: image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1 container_name: es03 environment: - node.name=es03 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es02 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data03:/usr/share/elasticsearch/data networks: - elastic volumes: data01: driver: local data02: driver: local data03: driver: local networks: elastic: driver: bridge ``` 啟動完成後就可以在瀏覽器輸入 http://localhost:9200 來連接 Elasticsearch。如果 Elasticsearch 正常運作的話,會回傳一個 Json 作為 Response。如下 : ```json= { "name" : "es01", "cluster_name" : "es-docker-cluster", "cluster_uuid" : "kVJqK-SgTa2Scab82iL0Og", "version" : { "number" : "7.8.1", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89", "build_date" : "2020-07-21T16:40:44.668009Z", "build_snapshot" : false, "lucene_version" : "8.5.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" } ``` ### vm.max_map_count [65530] is too low 啟動過程中如果失敗並且 log 顯示 `max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]`,這時需要去修改 systcl.conf。作法如下 : 1. Vim 開啟 sysctl.conf ```bash= vim /etc/sysctl.conf ``` 2. 在最後一行加上 ```bash= vm.max_map_count=262144 ``` 3. 執行 `sysctl -p` 來重新載入 systcl.conf 的設定,這樣就不需要重新開機。 ```bash= sysctl -p ``` 4. 重新啟動 Container ## Elasticsearch Cluster + Kibana 要搭配 Kibana 首先要先知道 Kibana 啟動時會從 `kibana.yml` 中讀取設定的參數,而在 `kibana.yml` 中預設 `elasticsearch.hosts` 是 `http://elasticsearch:9200`。預設值可以參考 [Elasticsearch 官網](https://www.elastic.co/guide/en/kibana/current/docker.html#docker-defaults)。 這個 `http://elasticsearch:9200` 的 `elasticsearch` 是在 docker-compose.yml 中所定義的 Service 名稱,也就是下方範例的第三行這個 Service 所啟動的 Container。kibana 可以直接這樣連線到 elasticsearch 是因為他們在同一個網路環境下,而 Docker-Compose 允許在同一個網路環境下的 Container 可以直接以其他 Container 的 Service 名稱作為 Domain 來進行連線,這樣就不需要自己設定 Domain。 所以 Elasticsearch 官方提供的建立 Elasticsearch cluster 和 Kibana 的兩個 docker-compose.yml 不能直接合併,要稍做修改才能使用。如果直接合併使用不修改你就會發現 Kibana 一直連不到 `http://elasticsearch:9200`。修改後的 docker-compose.yml 如下 : ```yaml= version: '3' services: elasticsearch: image: elasticsearch:7.8.1 container_name: es01 environment: - node.name=es01 - cluster.name=es-docker-cluster - discovery.seed_hosts=es02,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data01:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - elastic elasticsearch2: image: elasticsearch:7.8.1 container_name: es02 environment: - node.name=es02 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data02:/usr/share/elasticsearch/data networks: - elastic depends_on: - elasticsearch elasticsearch3: image: elasticsearch:7.8.1 container_name: es03 environment: - node.name=es03 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es02 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data03:/usr/share/elasticsearch/data networks: - elastic depends_on: - elasticsearch kibana: image: kibana:7.8.1 container_name: kibana ports: - 5601:5601 networks: - elastic depends_on: - elasticsearch volumes: data01: driver: local data02: driver: local data03: driver: local networks: elastic: driver: bridge ``` 我們總不可能為了 Kibana 預設連線到 `http://elasticsearch:9200` 就一定要命名一個叫做 elasticsearch 的 Service,所以這裡就要來介紹如何修改預設的連線。 下面的範例將 Elasticsearch 的 docker-compose.yml 做了一下調整,針對三個 es 的 Service 名稱改成 es001、es002、es003。 接著看到 Kibana 的地方,這裡新增了一個環境變數 `ELASTICSEARCH_HOSTS` 並且設定為 `http://es001:9200`。`ELASTICSEARCH_HOSTS` 就是上方提到的 `kibana.yml` 中的參數, `elasticsearch.hosts`,透過環境變數可以直接修改他的值。而這裡將預設的 `http://elasticsearch:9200` 改設定成 `http://es001:9200` 應該就可以理解原因了吧。 這裡特別註明一下,在 Elasticsearch 6 (含) 以前 `elasticsearch.hosts` 是 `elasticsearch.url`,所以要注意一下版本上的問題。 ```yaml= version: '3' services: es001: image: elasticsearch:7.8.1 container_name: es01 environment: - node.name=es01 - cluster.name=es-docker-cluster - discovery.seed_hosts=es02,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data01:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - elastic es002: image: elasticsearch:7.8.1 container_name: es02 environment: - node.name=es02 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es03 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data02:/usr/share/elasticsearch/data networks: - elastic depends_on: - es001 es003: image: elasticsearch:7.8.1 container_name: es03 environment: - node.name=es03 - cluster.name=es-docker-cluster - discovery.seed_hosts=es01,es02 - cluster.initial_master_nodes=es01,es02,es03 - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 volumes: - data03:/usr/share/elasticsearch/data networks: - elastic depends_on: - es001 kibana: image: kibana:7.8.1 container_name: kibana ports: - 5601:5601 networks: - elastic environment: - ELASTICSEARCH_HOSTS=http://es001:9200 depends_on: - es001 volumes: data01: driver: local data02: driver: local data03: driver: local networks: elastic: driver: bridge ``` 除了上方介紹的改環境變數以外,也可以透過更改 kibana.yml 來達到一樣的目的。詳細 kibana.yml 可以設定哪些參數請參考 [Elasticsearch 官方](https://www.elastic.co/guide/en/kibana/7.8/settings.html)。 ## Summary 本篇是因為在嘗試自己建立 Elasticsearch Cluster + Kibana 時發現了很多問題再加上自己對於 Docker Compose 不夠熟練,所以也踩了很多坑。 ## 參考 [1] [Install Elasticsearch with Docker](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html) [2] [docker運行elasticse容器max virtual memory areas vm.max_map_count [65530] is too low](https://blog.csdn.net/JineD/article/details/107759975?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-3&spm=1001.2101.3001.4242) [3] [如何在docker中部署Elasticsearch叢集和kibana](https://www.itread01.com/content/1545894579.html) [4] [Day21 - Elasticsearch Cluster](https://ithelp.ithome.com.tw/articles/10188506) [5] [Docker-Compose 建立 Elasticsearch 與 Kibana 服務](https://kevintsengtw.blogspot.com/2018/07/docker-compose-elasticsearch-kibana.html) [6] [elasticsearch.hosts or elasticsearch.url are not picked up at all in version 6.7.0](https://github.com/elastic/kibana-docker/issues/140) [7] [ElasticSearch on Docker](http://john-cd.com/johnscloud/docker/elasticsearch_docker/) [8] [Kibana Error Connecting to ES on Docker - Cannot Revive Connection](https://discuss.elastic.co/t/kibana-error-connecting-to-es-on-docker-cannot-revive-connection/133335) [9] [Configure Kibana | Elasticsearch](https://www.elastic.co/guide/en/kibana/7.8/settings.html) [10] [Install Elasticsearch with docker | Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker) [11] [Kibana is unable to connect to ES](https://discuss.opendistrocommunity.dev/t/kibana-is-unable-to-connect-to-es/185/5) [12] [Kibana Error Connecting to ElasticSearch using Docker - Cannot Revive Connection](https://stackoverflow.com/questions/50532208/kibana-error-connecting-to-elasticsearch-using-docker-cannot-revive-connection) [13] [配置 Kibana](https://www.elastic.co/guide/cn/kibana/current/settings.html) [14] [ElasticSearch詳解(一)](https://blog.51cto.com/hmtk520/2367766) [15] [[ELK] Elasticsearch 安裝](https://ithelp.ithome.com.tw/articles/10189791) [16] [Docker上的Kibana無法連接到Elasticsearch](https://www.thinbug.com/q/40341346) [17] [How to setup ElasticSearch and Kibana using Docker](https://gunith.github.io/docker-kibana-elasticsearch/) ###### tags: `Elasticsearch` `NoSQL`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully