1. 程式人生 > >如何通過docker來搭建elasticsearch環境呢?

如何通過docker來搭建elasticsearch環境呢?

弄了好幾天也搞了好幾天,一直沒有找到一個好的辦法解決通過docker搭建elasticsearch叢集的問題,因為像es、

mysql這來都是有狀態的,如果要通過docker搭建叢集環境,必須讓想辦法讓容器之間能發現各自,下面是分享的一

GIThub上的例子,發發比較笨拙,並且相當於每個節點獨享了宿主機。其中很關鍵的一點是:-add-host,其實就

是在容器的hosts檔案中添加了一條記錄。還有就是export的操作可以不執行,只需要保證兩臺宿主機之間可以相互通

信就行。

Elasticsearch 2.3.1 cluster with docker

18 APRIL 2016 on elasticsearch
docker

How to deploy an Elasticsearch 2.3.1 cluster using docker

We will deploy:

  1. Two nodes
  2. Authentication enabled via NGINX proxy
  3. Persistent data to each node local file system

To follow this tutorial you must have docker installed on your servers or VMs. You can find instructions to do so here
I'll also assume you can 

run docker without sudo and that you are usingDebian or one of its derivatives.

Official Elasticsearch cluster documentation can be found here.

Step One:

Get the IPs of the two servers running the following command on each one:

ifconfig eth0:1 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'  

(If you are using a different network interface other than eth0:1, make sure to modify the above command accordingly

)

Then export them on every machine:

[email protected]:~$ export node1=192.168.206.177  
[email protected]:~$ export node2=192.168.207.165  

(Make sure to change the IP addresses, to match your servers ones, before exporting.)

In a production environment also make sure each of the servers is accessible by way of resolvable DNS or hostnames. Either set up'/etc/hosts' to reflect this configuration or configure your DNS names.

Step Two:

For this blog post I'll use /home/docker/elastic directory. Create the directory on both servers:

[email protected]:/mkdir -p ~/docker/elasticsearch  
[email protected]:/mkdir -p ~/docker/elasticsearch  
Step Three

On yourServerName1, start Elasticsearch docker container with:

docker run --name="esNode1" -p 9300:9300 --hostname="yourServerName1" \  
--add-host [email protected]:192.168.207.165 \
-v "$PWD/docker/elasticsearch/data":/usr/share/elasticsearch/data \
-v "$PWD/docker/elasticsearch/plugins":/usr/share/elasticsearch/plugins \
-d elasticsearch:2.3.1 \
-Des.node.name="esNode1" \
-Des.network.host=_eth0:ipv4_ \
-Des.network.bind_host=0.0.0.0 \
-Des.cluster.name=yourClusterName \
-Des.network.publish_host=192.168.206.177 \
-Des.discovery.zen.ping.multicast.enabled=false \
-Des.discovery.zen.ping.unicast.hosts=192.168.207.165 \
-Des.discovery.zen.ping.timeout=3s \
-Des.discovery.zen.minimum_master_nodes=1 \
--env="ES_HEAP_SIZE=8g" 

and on yourServerName2, start Elasticsearch docker container with:

docker run --name="esNode2" -p 9300:9300 --hostname="yourServerName2" \  
--add-host [email protected]:192.168.206.177 \
-v "$PWD/docker/elasticsearch/data":/usr/share/elasticsearch/data \
-v "$PWD/docker/elasticsearch/plugins":/usr/share/elasticsearch/plugins \
-d elasticsearch:2.3.1 \
-Des.node.name="esNode2" \
-Des.network.host=_eth0:ipv4_ \
-Des.network.bind_host=0.0.0.0 \
-Des.cluster.name=yourClusterName \
-Des.network.publish_host=192.168.207.165 \
-Des.discovery.zen.ping.multicast.enabled=false \
-Des.discovery.zen.ping.unicast.hosts=192.168.206.177 \
-Des.discovery.zen.ping.timeout=3s \
-Des.discovery.zen.minimum_master_nodes=1 \
--env="ES_HEAP_SIZE=8g"

The --add-host is used to edit /etc/hosts inside the mongoDB docker container, so we can use hostnames instead of IPs. In a production environment these entries can be resolved via DNS, so those lines could be skipped.

-v lines let us choose where to mount locally elasticsearch docker container data and plugin directories. Those are what give you persistence outside docker container.

-d line let us choose which image and which version to pull from Docker Hub.

-Des.* lines are all configuration options passed to Elasticsearch
Some are self explanatory, such as -Des.node.name="esNode2" and -Des.cluster.name=yourClusterName, but others might require further explanation. 
Check out the following links to learn more about network settings anddiscovery.

A good rule of thumb is to set heap size to half of your memory, but don't cross 32GB if you are lucky enough to have that many. Also disableswap on your servers. Learn why from the official Elasticsearch documentation about heap and swap.

To disable swap:

sudo swapoff -a  

and also edit /etc/fstab/ and comment out all lines where swap is present. 
If disabling swap completely is not an option, there are other techniques described in the link above, that might work for you.

We have now a fully working Elasticsearch 2.3.1, but it is totally exposed and unprotected, meaning that everyone can, not only, access your data, but also erase them all with ease. 
In the next steps we are going see how to set up access control for our cluster, using NGINX as a proxy with basic authentication.

Step Four

If you don't already have nginx installed, do it now on both server:

sudo apt-get install nginx  

We need to generate 2 password files one for standard users and another one for administrators. We can do this wiht openssl, but we are limited to 8 characters passwords, or we accomplish this with apache2-utils and have no such limit. Choose what's best for you. I used the latter. 
Also remember to pick two meaningful usernames, for example stdusersand admins.

If you went the openssl route:

printf "stduser:$(openssl passwd -crypt sup3rs3cr3t)" > users_password  
printf "admin:$(openssl passwd -crypt ub3rs3cr3t)" > admins_password  

change user and group to root and move them to /etc/nginx/conf.d/:

sudo chown root:root users_password admins_password  
sudo mv users_password admins_password /etc/nginx/conf.d/  

else you'll need to install apache2-utils first, if it's not already installed:

sudo apt-get install apache2-utils  

and then generate the password files:

sudo htpasswd -c /etc/nginx/conf.d/search_users.htpasswd user  
sudo htpasswd -c /etc/nginx/conf.d/search_admins.htpasswd admin  
Step Five

Let's create on each server an NGINX configuration file and open it with an editor. I use vim:

sudo vim /etc/nginx/sites-available/elastic  

on yourServerName1 then insert those lines:

upstream elasticsearch {  
  server 172.17.0.2:9200;
  server 192.168.207.165:9200;
  keepalive 15;
}

server {  
    listen 8081 default_server;
    listen [::]:8081 default_server ipv6only=on;

    server_name yourServerName1.yourDomain.com;

    location / {
      return 403;
    }

    location ~* /[a-zA-Z0-9_]*[a-zA-Z0-9,_]*/(health|_health|state|stats) {
      return 405;
    }

    location ~* (/_search|/_analyze|_mget)$ {
      if ( $request_method !~ ^(GET|HEAD)$ ) {
        return 405;
      }

      if ( $request_uri = /_health ) {
        return 405;
      }


      if ( $request_uri = /_bulk ) {

        return 405;
      }

      auth_basic "Elasticsearch Users";
      auth_basic_user_file /etc/nginx/conf.d/search_users.htpasswd;
      proxy_pass http://elasticsearch;
      proxy_redirect off;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }
}

server {  
    listen 8082 default_server;
    listen [::]:8082 default_server ipv6only=on;

    server_name yourServerName1.yourDomain.com;

    location / {
      auth_basic "Elasticsearch Admins";
      auth_basic_user_file /etc/nginx/conf.d/search_admins.htpasswd;
      proxy_pass http://elasticsearch;
      proxy_redirect off;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }
}

on yourServerName2 insert those lines instead:

upstream elasticsearch {  
  server 172.17.0.2:9200;
  server 192.168.207.165:9200;
  keepalive 15;
}

server {  
    listen 8081 default_server;
    listen [::]:8081 default_server ipv6only=on;

    server_name yourServerName2.yourDomain.com;

    location / {
      return 403;
    }

    location ~* /[a-zA-Z0-9_]*[a-zA-Z0-9,_]*/(health|_health|state|stats) {
      return 405;
    }

    location ~* (/_search|/_analyze|_mget)$ {
      if ( $request_method !~ ^(GET|HEAD)$ ) {
        return 405;
      }

      if ( $request_uri = /_health ) {
        return 405;
      }


      if ( $request_uri = /_bulk ) {

        return 405;
      }


      auth_basic "Elasticsearch Users";
      auth_basic_user_file /etc/nginx/conf.d/search_users.htpasswd;
      proxy_pass http://elasticsearch;
      proxy_redirect off;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }
}

server {  
    listen 8082 default_server;
    listen [::]:8082 default_server ipv6only=on;

    server_name yourServerName2.yourDomain.com;

    location / {
      auth_basic "Elasticsearch Admins";
      auth_basic_user_file /etc/nginx/conf.d/search_admins.htpasswd;
      proxy_pass http://elasticsearch;
      proxy_redirect off;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }
}

As you can see the NGINX configuration file are pretty similar. They only differ in server_name and in the upstream section. 
Now we need to enable on both servers the configurations we just created:

sudo ln -s /etc/nginx/sites-available/elastic /etc/nginx/sites-enabled/elastic  

and then reload the NGINX configuration, again on both server:

sudo service nginx reload  

We did just set up a simple load balancer, thaks to the upstreamdirective, and allowing access only to authenticated users, even with different roles and permissions.

On port 8081 we are only allowing GET and HEAD requests to endpoint containing: _search, _analyze, _mget. In other words we are only allowing methods to retrieve data, but not to modify existing, deleting or inserting new data. That's what regular entitled users will use.

On port 8082 we are allowed to do anything we'd like to. That's, after all, the admin account we'll use to manage our cluster.

Step Six

It is usually handy to have an upstart script or something equivalent to manage your docker container instances.

On node1 (the one running on yourServerName1):

sudo vim /etc/init/esNode1.conf  

and insert those lines:

description "Elasticsearch 2.3.1 node 1"  
author "[email protected]"  
start on filesystem and started docker  
stop on runlevel [!2345]  
respawn  
script  
    /usr/bin/docker start -a es1Node1
end script  

and on node2 (the one running on yourServerName2):

sudo vim /etc/init/esNode2.conf  

and insert those lines:

description "Elasticsearch 2.3.1 node 2"  
author "[email protected]"  
start on filesystem and started docker  
stop on runlevel [!2345]  
respawn  
script  
    /usr/bin/docker start -a esNode2
end script  

With those upstart scripts in place, you can issue commands in the form:

sudo service serviceName status|start|stop|restart  

So, for example, if we would like to know whether or not the Elasticsearch is up and running on yourServerName1, we'd type:

sudo service esNode1 status  

and if it is up and running it will output somethinng like:

esNode1 start/running, process 23163  

Note that if you already had your docker container running when you created the upstart scripts, you will need to manually stop the docker containers on with:

[email protected]:~$ docker stop es1Node  
[email protected]:~$ docker stop es2Node  

and then starting them with the upstart script:

[email protected]:~$ sudo service esNode1 start  
[email protected]:~$ sudo service esNode2 start  

From this moment on, upstart will be responsible, to keep your docker container running, and restarting them on server restarts.

Conclusion

We have now a fully operational Elasticsearch 2.3.1 cluster running withdocker! Take a tour of the official documentation to learn how to createindexes and mappings and then import or insert some data.

In an upcoming post we'll explore how to create a very fast autocomplete box using Elasticsearch.


相關推薦

如何通過docker搭建elasticsearch環境

弄了好幾天也搞了好幾天,一直沒有找到一個好的辦法解決通過docker搭建elasticsearch叢集的問題,因為像es、 mysql這來都是有狀態的,如果要通過docker搭建叢集環境,必須讓想辦法讓容器之間能發現各自,下面是分享的一 個GIThub上的例子,發發比較笨拙

【Rocketmq】通過 docker 快速搭建 rocketmq 環境

1. 安裝 Namesrv 拉取映象 docker pull rocketmqinc/rocketmq:4.4.0` 啟動容器 docker run -d -p 9876:9876 -v {RmHome}/data/namesrv/logs:/root/logs -v {RmHome}/data/name

一文教您如何通過 Docker 快速搭建各種測試環境(Mysql, Redis, Elasticsearch, MongoDB) | 建議收藏

歡迎關注個人微信公眾號: 小哈學Java, 文末分享阿里 P8 高階架構師吐血總結的 《Java 核心知識整理&面試.pdf》資源連結!! 個人網站: https://www.exception.site 小哈今天給大家分享的主題是,如何通過 Docker 快速搭建各種測試環境,本文列舉的,也

使用docker高效搭建開發環境

搭建開發環境 docker 作為一個平時喜歡折騰的開發人員,我喜歡嘗試各種環境,使用感興趣的各種開源軟件。同時,我也是有一些相對的小潔癖,很喜歡linux中權限最小化原則,我也不喜歡自己的環境中有太多不知道的東西。做了多年的web開發,我接觸到的環境大致如下:操作系統從centos5到centos7;

docker簡易搭建ElasticSearch叢集

寫在前面:為什麼要用ElasticSearch?我們的應用經常需要新增檢索功能,開源的Elastic Search是目前全文檢索引擎的首選。它可以快速的儲存、搜尋和分析海量資料。ElasticSearch是一個分散式搜尋框架,提供RestfulAPI,底層基於Lucene,採用多shard(

Elk日誌採集分析系統 搭建elasticsearch環境 6.4 環境

https://www.elastic.co/cn/blog/elasticsearch-6-4-0-released 1 官網下載 elasticsearch 安裝包 https://artifacts.elastic.co/downloads/elasticsearch/elasti

使用Docker快速搭建生產環境

Docker安裝 CentOS 6.5: 1 2 yum install -y docker-io service docker start 下載映象 配置使用國內映象:修改Docker配置檔案/etc/default/

linux中利用dockerdocker-compose搭建lnmp環境詳解 10分鐘快速完成

本文主要包括部分 注意事項 重要資訊提示 快速執行安裝的純命令 相關介紹 配置檔案,參考地址   1.要求說明:    linux, 安裝了docker和docker compose 特別注意: 本文中提及的密碼與本文的配置檔案可能不一致(與新詳

1分鐘,通過docker-compose 搭建zookeeper 叢集

一、建立三節點 zookeeper 叢集 將 docker-compose.yml 儲存到當前命令列目錄下 docker-compose.yml 檔案 version: '2' networks: zk: services: zookeeper1: image: zook

第一個 spring Boot 應用通過Docker 實現構建、執行、釋出

1. Docker 簡介 Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何介面。Docker image 是

使用docker快速搭建執行環境

最近在搭建虛擬機器上的執行環境,最開始是按照傳統方法一個一個安裝軟體,一大堆軟體裝下確實是挺費時間的。現在容器技術非常的火,於是就去看了一下docker,完全能夠滿足我的需求,省時省力, 何樂而不為? 什麼是docker? 簡單的說,Docker是一個基

使用Docker搭建gitlab(備份)

所有的團隊都面臨同樣一個問題,程式碼庫的版本管理。 gitlab可以說是開源的github,可以自行部署在任何地方。 那對於團隊來講,我要怎麼去部署、升級、備份、遷移才是最關鍵的地方。這個時候有了Docker這麼牛逼的東西出現了。 最最最根本的就是它不會影響你現有的環境

基於docker-compose搭建laravel環境(nodejs+npm+bower)

laradock資源連結:https://github.com/laradock/laradock laradock基於docker-compose實現了整合化的laravelve環境搭建,提供了nginx, hhvm,php-fpm, mysql, redis, pos

Docker搭建開發環境(執行Eclipse等圖形化介面程式)

Docker搭建開發環境 基本說明 兩個月前的時候自己提出想通過Docker來搭建開發環境(http://blog.csdn.net/zhaodedong/article/details/46549279),能方便地供實驗室的其他同學使用。我所謂的開發環

如何在docker搭建lvs_nat環境

什麼是LVS?怎麼在docker中構建lvs?需要什麼環境?首先需要一個docker 環境,如何構建docker環境請查閱我的另一篇文章然後需要一個centos7的基礎映象。開始:第一步:執行映象:命令:docker run --privileged -d -i -t dao

TensorFlow(1):使用docker映象搭建TensorFlow環境

1,關於TensorFlow TensorFlow 隨著AlphaGo的勝利也火了起來。 google又一次成為大家膜拜的大神了。google大神在引導這機器學習的方向。 同時docker 也是一個非常好的工具,大大的方便了開發環境的構建,之前需要配置

Docker筆記(十):使用Docker搭建一套ELK日誌分析系統

一段時間沒關注ELK(elasticsearch —— 搜尋引擎,可用於儲存、索引日誌, logstash —— 可用於日誌傳輸、轉換,kibana —— WebUI,將日誌視覺化),發現最新版已到7.4了。所以別問程式設計師為什麼這麼忙?因為不是在加班就是在學習新框架中。 本文整理了使用Docker來快速搭

Mac上通過docker配置PHP開發環境

這篇文章介紹的內容是關於Mac上通過docker配置PHP開發環境,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下 更多PHP相關知識請關注我的專欄PHP​zhuanlan.zhihu.com 安裝docker 關於MAC上安裝docker網上有很多文章介紹,這裡我就不在做描述了,順便提一句,建

使用docker快速搭建hive環境

> 記錄一下使用docker快速搭建部署hive環境 [toc] ## 寫在前面 想練練Hive SQL,但是沒有hive shell環境。現在只有一臺**空的CentOS 7**機子,一想要弄jdk、hadoop、mysql、hive就頭疼。 於是在網上找了找,發現用docker部署hive會

Linux環境通過docker搭建PHP的LAMP開發環境

想必作為一個Web開發的程式設計師,近些年對docker一定不會陌生,Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何介面。使用dock