ElasticSearch在Azure中的集群配置和Auto-Scale

最近在项目中ElasticSearch的使用愈来愈多,最新的项目中要求ES使用集群,在啥都不知道的状况下弄了两天后,终于搞定,所以写个笔记记录下。html

 

1.首先咱们须要建立一个Virtual network,输入name和选择location后,就是一直下一步,直到完成。java

虽然这个Virtual network不是必须的,可是这会使整个配置简单不少。这个Virtual network的做用是是在其下面的virtual machina能够互相通讯,若是没有这个你须要配置的就不仅是防火墙这么简单了。node

2.咱们须要建立一个Cloud service, 这个Cloud service会包含咱们全部的集群的Virtual machine,而且为其提供Auto scale, Cloud service能够在建立Virtual machine的时候一块儿建立,就不在这里单首创建了git

如下是Cloud service的一些认知github

  • Cloud Service是一个容器,它能够包括托管服务或者虚拟机。
  • 部署在Cloud Service中的托管服务虽然支持Java,PHP,Python、Ruby、Node.js等开源技术,但用来host托管服务的虚拟机运行的是Windows Server操做系统,一个托管服务的instance就是一个运行Windows Server操做系统的虚拟机,一个托管服务能够包含多个instance。
  • 一个虚拟机必须放置在一个Cloud Service中。一个Cloud Service能够包含多个虚拟机,但一个虚拟机只能隶属于一个Cloud Service。
  • 一个Cloud Service默认分配一个VIP,即:动态分配的公网IP地址。

3.接下来咱们就能够开始建立最关键的Virtual machine了,ElasticSearch等都是安装在Azure的Virtual Machine上的。shell

  1)输入virtual machine的name和登录远程用的user name和passwordwindows

  2)在第一次建立的时候选择建立新的Cloud service, 新的Availability set安全

  3)接下来就是一路next,直到建立完成。oracle

直到如今咱们已经完成了azure的一部分建立,接着咱们能够在azure的面板选择建立完成的VM(Virtual Machine,接下来简称VM),点击connect,会下载一个RDP的文件,使用这个咱们能够远程到VM上进行咱们的Elastic search安装elasticsearch

我建议先在VM安装一个Chrome,刚刚建立的VM自带的IE因为有不少安全的设置,因此很难用。另外因此安装都要在C盘进行,D盘是临时盘,在VM重启的时候会被覆盖。

4.如今开始安装Elastic search

  1)安装JDK,能够直接在VM进行下载(不得不说Azure上面的下载速度简直飞快),而且写入系统环境变量(这个能够自行百度)

  2)下载Elastic search,而且解压到C盘

  3)  要使ES的集群能使用,咱们须要安装一个插件,https://github.com/elastic/elasticsearch-cloud-azure, 这个插件将使用Azure Api,获取某个cloud service下面的全部VM,进行unicast discovery

    (1) 生成证书,若是你安装了git,可使用git的命令行工具进行生成,如下是生成所须要的命令(注意要将[Your password here]改为本身的密码

    1. openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout azure-private.key -out azure-certificate.pem
      chmod 600 azure-private.key azure-certificate.pem
      openssl x509 -outform der -in azure-certificate.pem -out azure-certificate.cer
    2. # Generate a keystore (azurekeystore.pkcs12)
      # Transform private key to PEM format openssl pkcs8 -topk8 -nocrypt -in azure-private.key -inform PEM -out azure-pk.pem -outform PEM # Transform certificate to PEM format openssl x509 -inform der -in azure-certificate.cer -out azure-cert.pem cat azure-cert.pem azure-pk.pem > azure.pem.txt # You MUST enter a password! openssl pkcs12 -export -in azure.pem.txt -out azurekeystore.pkcs12 -name azure -noiter -nomaciter -password pass:[Your password here]
    3. 把azure-certificate.cer文件上传到azure,在Settings -> Manage Certificates.
    4. 将 azurekeystore.pkcs12文件拷贝到解压出来的ES文件夹的根目录

      (2)下载这个压缩包,修改elasticsearch.yml文件,并将其拷贝到ES的Config目录下,覆盖两个文件

    1. subscription.id -> 你azure的subscription id,能够在Settings -> Subscriptions下面能够找到
    2. cloud.service.name -> 上面建立的Cloud service的名字
    3. keystore.path -> 若是你的文件是放在ES的根目录,path的值是:azurekeystore.pkcs12,若是你放在下面的文件夹中,path: /[你的文件夹名字]/azurekeystore.pkcs12
    4. keystore.password ->你上面输入的密码
    5. deployment.name -> cloud service的deployment name,能够在Cloud service的Dashboard右边的属性中找到

      (3)安装插件,在VM上面打开CMD,cd到你的ES的文件夹下的bin目录,好比个人cd C:\elasticsearch-1.7.3\bin,

        而且执行plugin install elasticsearch/elasticsearch-cloud-azure/2.7.1(注意,后面的版本号要对应你的ES的版本,对应的版本能够在这里找到)PS:这里的cmd别关

  4)接下来能够安装Elastic search了,若是上面安装插件的时候,cmd没有关闭,能够直接接下去执行,若是关闭了也能够再次打开bin

    (1)依次执行下面的命令:

    1. service.bat install       --> 将ES安装为service
    2. service.bat start     --> 启动service
    3. plugin -install mobz/elasticsearch-head      --> 安装head插件

    (2)到windows services将Elastic search的service设置为Auto start(Delayed start)

  5)设置防火墙,执行下面的power shell命令,第一个ES对外的Port,第二个是内部通信用的Port

    1. New-NetFirewallRule -DisplayName "Allow Elasticsearch Outbound Port 9200" -Direction Outbound -Protocol TCP -RemotePort 9200 -Action Allow
      New-NetFirewallRule -DisplayName "Allow Elasticsearch Inbound Port 9200" -Direction Inbound -Protocol TCP -LocalPort 9200 -Action Allow
    2. New-NetFirewallRule -DisplayName "Allow Elasticsearch Outbound Port 9300" -Direction Outbound -Protocol TCP -RemotePort 9300 -Action Allow
      New-NetFirewallRule -DisplayName "Allow Elasticsearch Inbound Port 9300" -Direction Inbound -Protocol TCP -LocalPort 9300 -Action Allow

  6)到这里ES的设置已经完成,在VM上面输入localhost:9200和http://localhost:9200/_plugin/head/能够看到,若是有什么错误的话,能够在logs里面找到你须要的信息

    

5. 设置VM的Endpoints,咱们须要在这里设置VM的对外开放的端口,这里没啥难度,在azure management里面找到刚才建立的VM,进入到详细页面,有一个endpoints的tab,添加端口就行,主要是添加Laod balance,只有Basic的VM是不能添加load balance的,这个要注意。

   

完成后,咱们就能够经过外网访问ES了,在cloud service里面找到PUBLIC VIRTUAL IP (VIP) ADDRESS,替换掉上面的localhost,试试是否能够访问。

6. 到这里,须要的设置已经都完成了,接下来咱们就须要copy多个VM,造成集群

  1)在VM的页面,下面有个,咱们能够把VM整个capture成一个Image

  2)接着建立咱们的第二个VM,在建立页面咱们能够选择My images,能够看到咱们刚才建立的那个Image,使用这个image建立VM,接下来的步奏和上面的基本一致,只是cloud service和availability set须要选择咱们第一次建立的。

    

  3) 建立完成后,咱们再去看head,就能够看到

    

    之后用一样方式建立的VM都会自动加入到集群里面来。

7)设置Auto-scale,这个在Cloud service设置里面能够找到,具体的能够参考这个博客

  PS:被Auto-scale关掉的VM是不算钱的,能够省下不少的费用

大功告成,我目前在项目里面添加了5个VM,目前的状况

相关文章
相关标签/搜索