ES Cluster Set Up

  • ES集群服务器通用设置
def setUpESServerEV(heapSize,versionNum):
    sudo("echo 'export ES_HEAP_SIZE="+heapSize+"G\n' >> /etc/profile")
    sudo("echo 'export ES_VERSION="+versionNum+"\n' >> /etc/profile")
    sudo("echo 'export ES_CLUSTER_NAME=vc_cluster\n' >> /etc/profile")
    #/etc/fstab distable swap
    sudo("sed -i 's/.*swap.*//' /etc/fstab")
    sudo("sysctl -w vm.max_map_count=262144")
    sudo("echo 'vm.max_map_count=262144\n' >> /etc/sysctl.conf")
    #vm.swappiness=0
    sudo("echo 'vm.swappiness=0\n' >> /etc/sysctl.conf")
    #fs.file-max = 2097152
    sudo("echo 'fs.file-max=2097152\n' >> /etc/sysctl.conf")
    sudo("sysctl -p"

def createEsUser():
    env.warn_only=True
    sudo("mkdir -p /data/es/plugins_$ES_VERSION/")
    sudo("mkdir -p data/es/data/")
    sudo("mkdir -p data/es/logs/")
    sudo("mkdir -p data/es/es/")
    sudo("groupadd vc")
    sudo("chgrp -R vc  /data/")
    sudo("chmod -R g+w /data/")
    sudo("useradd zhaoyufei")
    sudo("passwd zhaoyufei")
    sudo("usermod -g vc zhaoyufei")
  • ES安装

sudo wget -P /data/apps/  https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/$ES_VERSION/elasticsearch-$ES_VERSION.tar.gz
sudo tar zxvf /data/apps/elasticsearch-$ES_VERSION.tar.gz -C /data/apps/

cd /data/apps/elasticsearch-$ES_VERSION

bin/plugin install license
bin/plugin install marvel-agent
bin/plugin install lang-javascript

cp -r /data/apps/elasticsearch-$ES_VERSION /data/apps/elasticsearch-${ES_VERSION}_1
cp -r /data/apps/elasticsearch-$ES_VERSION /data/apps/elasticsearch-${ES_VERSION}_2
cp -r /data/apps/elasticsearch-$ES_VERSION /data/apps/elasticsearch-${ES_VERSION}_3
cp -r /data/apps/elasticsearch-$ES_VERSION /data/apps/elasticsearch-${ES_VERSION}_master

slave elasticsearch.yml:

# ======================== 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 see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.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.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.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable 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
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Elasticsearch nodes will find each other via unicast, by default.
#
# 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 nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

cluster.name: ${ES_CLUSTER_NAME}
node.name: ${HOSTNAME}

indices.fielddata.cache.size:  20%
indices.breaker.fielddata.limit: 20%
indices.breaker.request.limit: 20%
bootstrap.mlockall: true
MAX_LOCKED_MEMORY: unlimited
MAX_OPEN_FILES: 6553500
cluster.routing.allocation.disk.watermark.low: 80%
cluster.routing.allocation.disk.watermark.high: 90%


node.master: false

node.data: true

path.data: /data/es/data
path.plugins: /data/es/plugins_${ES_VERSION}
path.logs: /data/es/logs
index.mapper.dynamic: true
network.host: 0.0.0.0

script.inline: on
script.indexed: on
http.port: 9200
transport.tcp.port: 9300

action.disable_delete_all_indices: true
discovery.zen.ping.multicast.enabled: false

discovery.zen.ping.unicast.hosts: ["172.16.3.167:9300","172.16.4.140:9300","172.16.3.200:9300"]

master elasticsearch.yml:

# ======================== 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 see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.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.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.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable 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
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# --------------------------------- Discovery ----------------------------------
#
# Elasticsearch nodes will find each other via unicast, by default.
#
# 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 nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Various -----------------------------------
#
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

indices.fielddata.cache.size:  20%
indices.breaker.fielddata.limit: 20%
indices.breaker.request.limit: 20%

cluster.name: ${ES_CLUSTER_NAME}

node.name: ${HOSTNAME}
bootstrap.mlockall: true
MAX_LOCKED_MEMORY: unlimited


MAX_OPEN_FILES: 6553500
cluster.routing.allocation.disk.watermark.low: 80%
cluster.routing.allocation.disk.watermark.high: 90%
script.inline: on
script.indexed: on


node.master: true

node.data: false

path.data: /data/es/data
path.plugins: /data/es/plugins_${ES_VERSION}
path.logs: /data/es/logs
index.mapper.dynamic: true
network.host: 0.0.0.0

action.disable_delete_all_indices: true

http.port: 9200
transport.tcp.port: 9300

将上述两个文件分别替换服务器上的es原配置文件.

rsync -avP -e "ssh -i /data/ssh/common_ssh_key/id_rsa" /data/apps/scripts/es/master_elasticsearch.yml zhaofeiyu@60.205.191.184:/data/apps/elasticsearch-2.3.1_master/config/elasticsearch.yml
rsync -avP -e "ssh -i /data/ssh/common_ssh_key/id_rsa" /data/apps/scripts/es/slave_elasticsearch.yml zhaofeiyu@60.205.191.184:/data/apps/elasticsearch-2.3.1_1/config/elasticsearch.yml

将所有es的配置文件更新完之后,在运行下面的脚本:

def updateEsConfFile(versionNum):
    env.warn_only=True
    run("sed -i 's/^discovery.zen.ping.unicast.hosts.*//' /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")
    # run("sed -i 's/^path.logs.*//' /data/apps/elasticsearch-2.2.0/config/elasticsearch.yml")
    master_ip="\"172.16.1.2:9308\",\"172.16.1.3:9308\",\"172.16.1.4:9308\""
    run("echo 'discovery.zen.ping.unicast.hosts: ["+master_ip+"]\n' >> /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")

    run("sed -i 's/^node.name.*//' /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")
    run("sed -i 's/^http.port.*//' /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")
    run("sed -i 's/^transport.tcp.port.*//' /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")


    run("echo 'http.port: 9200\n' >> /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")
    run("echo 'transport.tcp.port: 9300\n' >> /data/apps/elasticsearch-"+versionNum+"/config/elasticsearch.yml")
    # run("echo 'path.logs: /data/es/logs/\n' >> /data/apps/elasticsearch-2.2.0/config/elasticsearch.yml")
    for i in range(1,5):
        path="/data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml"
        run("sed -i 's/^http.port.*//' "+path+"")
        run("sed -i 's/^transport.tcp.port.*//' "+path+"")
        run("sed -i 's/^path.logs.*//' "+path+"")
        run("echo 'path.logs: /data/es/logs_"+str(i)+"\n' >> "+path+"")

        run("sed -i 's/^discovery.zen.ping.unicast.hosts.*//' "+path+"")
    #    run("sed -i 's/^path.logs.*//' /data/apps/elasticsearch-2.2.0_"+str(i)+"/config/elasticsearch.yml")
    #    run("echo 'path.logs: /data/es/logs/"+str(i)+"\n' >> /data/apps/elasticsearch-2.2.0_"+str(i)+"/config/elasticsearch.yml")
        run("echo 'discovery.zen.ping.unicast.hosts: ["+master_ip+"]\n' >> /data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml")
        run("sed -i 's/^node.name.*//' /data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml")
        run("echo 'node.name: ${HOSTNAME}_"+str(i)+"\n' >> /data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml")
        run("echo 'http.port: 920"+str(i)+" \n' >> /data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml")
        run("echo 'transport.tcp.port: 930"+str(i)+" \n' >> /data/apps/elasticsearch-"+versionNum+"_"+str(i)+"/config/elasticsearch.yml")
    master_path="/data/apps/elasticsearch-"+versionNum+"_master/config/elasticsearch.yml"
    run("sed -i 's/^http.port.*//' "+master_path+"")
    run("sed -i 's/^transport.tcp.port.*//' "+master_path+"")
    run("sed -i 's/^discovery.zen.ping.unicast.hosts.*//' "+master_path+"")
    run("echo 'discovery.zen.ping.unicast.hosts: ["+master_ip+"]\n' >> "+master_path+"")

    run("echo 'http.port: 9208\n' >> "+master_path+"")
    run("echo 'transport.tcp.port: 9308\n' >> "+master_path+"")
    run("sed -i 's/^path.logs.*//' "+master_path+"")
    run("echo 'path.logs: /data/es/logs_master\n' >> "+master_path+"")

以上完毕之后, 就可以启动ES集群了。
查看各个ES实例的日志:
tail -f /data/es/logs/1/*.log
tail -f /data/es/logs/2/*.log
tail -f /data/es/logs/master/*.log

ps -ef|grep elasticsearch |grep -v grep|awk {'print $2'}|xargs  kill -9 > /dev/null
首先启动master
nohup /data/apps/elasticsearch-"+versionNum+"_master/bin/elasticsearch > /dev/null &
接着启动slave
nohup /data/apps/elasticsearch-"+versionNum+"/bin/elasticsearch > /dev/null &

以上操作都是批量对所有服务器进行从操作, 所以最好使用Linux服务器远程批量交互工具比如: fabric
给大家一个自己写的例子(批量设置服务器ES相关系统变量):

from fabric.api import run
from fabric.api import env
from fabric.api import sudo
from fabric.api import local
from fabric.api import settings
from fabric.api import *





env.roledefs = {
    'test':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]'],
    'es_master': ['[email protected]','[email protected]'],
    'es_slave':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]'],
    'newesserver':['[email protected]','[email protected]','[email protected]','[email protected]'],
    'kibanaServers':['[email protected]'],
     'tmp':['[email protected]']




    }
    env.key_filename = '/data/ssh/common_ssh_key/id_rsa'
def setUpESServerEV(heapSize,versionNum):
    sudo("echo 'export ES_HEAP_SIZE="+heapSize+"G\n' >> /etc/profile")
    sudo("echo 'export ES_VERSION="+versionNum+"\n' >> /etc/profile")
    sudo("echo 'export ES_CLUSTER_NAME=vc_cluster\n' >> /etc/profile")
    #/etc/fstab distable swap
    sudo("sed -i 's/.*swap.*//' /etc/fstab")
    sudo("sysctl -w vm.max_map_count=262144")
    sudo("echo 'vm.max_map_count=262144\n' >> /etc/sysctl.conf")
    #vm.swappiness=0
    sudo("echo 'vm.swappiness=0\n' >> /etc/sysctl.conf")
    #fs.file-max = 2097152
    sudo("echo 'fs.file-max=2097152\n' >> /etc/sysctl.conf")
    sudo("sysctl -p")
    控制台运行命令:
    fab -f ./esAdmin.py -R tmp setUpESServerEV:15,2.3.1
    这条命令就会在es_slave对应的所有服务器上执行相应的脚本

fabric安装命令(仅仅使用Linux以及mac系统):

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python get-pip.py

yum install gcc python-devel

sudo pip install fabric
发布了17 篇原创文章 · 获赞 4 · 访问量 5650

猜你喜欢

转载自blog.csdn.net/jasstion/article/details/52767367