zoukankan      html  css  js  c++  java
  • elastucasearch基础理论以及安装

    一、elasticasearch核心概念

    Near Realtime(NRT 近实时)

    Elasticsearch 是一个近实时的搜索平台。您索引一个文档开始直到它被查询时会有轻微的延迟时间(通常为1秒)。

    Cluster(集群)

    一个或者多个节点的集合,它们一起保存数据并且提供所有节点联合索引以及搜索功能。集群存在一个唯一的名字身份且默认为 “elasticsearch”。

    Node(节点)

    node(节点)是一个单独的服务器,它是集群的一部分,存储数据,参与集群中的索引和搜索功能。像一个集群一样,一个节点通过一个在它启动时默认分配的一个随机的 UUID(通用唯一标识符)名称来识别。节点可以通过配置 cluster name 来加入到指定的集群中。默认情况下,每个节点安装时都会加入到名为 elasticsearch 的集群中,这也就意味着如果您在网络中启动许多节点--假设它们可以发现彼此–它们全部将自动的构成并且加入到一个名为 elasticsearch 的单独的集群中。

    此外,如果在当前网络中没有其它 elasticsearch 结点在运行,则启动一个结点将会默认形成一个叫 elasticsearch 的单结点集群。

    Index(索引)

    index(索引)是具有稍微类似特征文档的集合。例如,您有一个消费者数据的索引,一个产品目录的索引,和另一个是订单数据的索引。一个索引通过名字(必须全部是小写)来标识,并且该名字在对 document(文档)执行 indexing(索引),search(搜索),update(更新)和 delete(删除)操作时会涉及到。

    在一个单独的集群中,您可以定义您想要的许多索引。

    Type(类型)

    在 Index(索引)中,可以定义一个或多个类型。一个类型是索引中一个逻辑的种类/分区,它的语义完全取决于您自己。一般情况下,一个类型被定义成一组常见字段的文档。例如,假设您运行着一个博客平台并且在一个单独的索引中存储了所有的数据。在这个索引中,您也许定义了一个用户数据类型,博客数据类型,和评论数据类型。

    Document(文档)

    document(文档)是索引信息的基本单位。例如,您有一存储 customer(客户)数据的文档,另一个是存储 product(产品)数据的文档,还有一个是存储 order(订单)数据的文档。该文档可以使用 JSON 来表示,它是一种无处不在的互联网数据交换格式。

    在索引/类型中,您可以存储许多文档。注意,尽管一个文档物理的存在于索引中,实际上一个文档必须被 索引/分配 给索引内的类型。

    Shards & Replicas(分片 & 副本)

    索引可以存储大量数据,可以超过单个节点的硬件限制。例如,十亿个文档占用了 1TB 的磁盘空间的单个索引可能不适合放在单个节点的磁盘上,并且从单个节点服务请求会变得很慢。

    为了解决这个问题,Elasticsearch 提供了把 Index(索引)拆分到多个 Shard(分片)中的能力。在创建索引时,您可以简单的定义 Shard(分片)的数量。每个 Shard 本身就是一个 fully-functional(全功能的)和独立的 “Index(索引)”,(Shard)它可以存储在集群中的任何节点上。

    Sharding(分片)非常重要两个理由是 : 

    • 水平的拆分/扩展。
    • 分布式和并行跨 Shard 操作(可能在多个节点),从而提高了性能/吞吐量。

    Shard 的分布式机制以及它的文档是如何聚合支持搜索请求是完全由 Elasticsearch 管理的,并且是对用户透明的。

    在 网络/云 环境中可能随时会故障,无论出于何种原因,在 shard/node 不知何故会离线或者消失的情况下强烈建议设置故障转移是非常有效的。为了达到这个目的,Elasticsearch 可以让您设置一个或多个索引的 Shard 副本到所谓的副本分片,或者副本中去。

    副本非常重要的两个理由是 : 

    • 在 shard/node 故障的情况下提供了高可用性。为了达到这个目的,需要注意的是在原始的/主 Shard 被复制时副本的 Shard 不会被分配到相同的节点上。
    • 它可以让你水平扩展搜索量/吞吐量,因为搜索可以在所有的副本上并行执行。

    总而言之,每个索引可以被拆分成多个分片,一个索引可以设置 0 个(没有副本)或多个副本。开启副本后,每个索引将有主分片(被复制的原始分片)和副本分片(主分片的副本)。分片和副本的数量在索引被创建时都能够被指定。在创建索引后,您也可以在任何时候动态的改变副本的数量,但是不能够改变分片数量。

    默认情况下,Elasticsearch 中的每个索引分配了 5 个主分片和 1 个副本,这也就意味着如果您的集群至少有两个节点的话,您的索引将会有5 个主分片和另外 5 个副本分片(1 个完整的副本),每个索引共计 10 个分片。

    注意 :

      每个Elasticsearch分片是一个Lucene索引。在单个Lucene索引中有一个最大的文档数量限制。从LUCENE-5843的时候开始,该限制为2,147,483,519(=Interger.MAX_VALUE-128)个文档。您可以使用cat/shards api来监控分片大小。

    二、安装部署ELK

    2.1 安装elasticasearch

    准备工作如下:

    # 禁用selinux

    setenforce 0

    # 关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld

    # 添加elasticsearch用户

    useradd -s /sbin/nologin -M elasticsearch

    # 更改系统配置/etc/security/limits.conf文件

    elasticsearch soft memlock unlimited
    elasticsearch hard memlock unlimited

    * soft nofile 65536

    * hard nofile 131072

    * soft nproc 2048

    * hard nproc 4096

    提示:这里的elasticsearch表示的是我需要通过那个用户来启动elasticsearch进程,不同的环境根据自己的实际情况变更即可;

    # 更改系统内核参数

    更改/etc/sysctl.conf文件

    vm.max_map_count=655360

    2.1.1 安装java jdk

    java下载页面:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    yum -y install jdk-8u141-linux-x64.rpm
    java -version


    ###添加至/etc/profile文件中;

    export JAVA_HOME=/usr/java/jdk1.8.0_141
    export PATH=$JAVA_HOME/bin:$PATH

    2.1.2 部署elasticasearch多节点集群(3台主机)

    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.zip
    unzip elasticsearch-5.5.0.zip
    ln -s /usr/local/elasticsearch-5.5.0 /usr/local/elasticsearch/

    提示:本环境为3台主机,IP地址分别为192.168.58.128、192.168.58.129、192.168.58.130,以上操作分别在3台主机操作;

    2.1.3 修改配置文件

    内容如下所示:

    [root@localhost ~]# cat /usr/local/elasticsearch/config/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 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-elk.test
    #
    # ------------------------------------ Node ------------------------------------
    #
    # Use a descriptive name for the node:
    #
    node.name: node01-128
    #
    # 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: /data/elk/es/data
    #
    # Path to log files:
    #
    path.logs: /data/elk/es/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.58.128
    #
    # 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: ["192.168.58.128", "192.168.58.129","192.168.58.130"]
    #
    # 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

    创建相关目录并授权

    chown -R elasticsearch.elasticsearch /usr/local/elasticsearch
    chown -R elasticsearch.elasticsearch /data

    提示:需要变更的地方已经标红,配置的内容也很好理解,将该文件分发到其他主机并变更部分内容即可;

    2.1.4 启动集群并验证

    sudo -u elasticsearch -s
    cd /usr/local/elasticsearch/bin/
    ./elasticsearch

    提示:elastucasearch相关消耗资源,所以资源一定要给充足。其他两台主机也做类似操作,但是需要注意elasticsearch的jvm内存配置参数(默认是1G配置);

    elasticsearch启动的其他方式,前面的那种方式启动之后是在前台运行,如果想要停止通过Ctrl + C即可。如果想让让elasticsearch后台运行,可以通过如下操作实现:

    cd /usr/local/elasticsearch/
    .bin/elasticsearch -d
    或
    cd /usr/local/elasticsearch/bin/
    .bin/elasticsearch -d -p /data/elk/logs/elasticsearch.pid
    
    如果想停止就可以:
    kill `cat /data/elk/logs/elasticsearch.pid`
  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/yangxiaoyi/p/7219046.html
Copyright © 2011-2022 走看看