**前置文章:**[CentOS 7.6 安装 Logstash](https://www.xiaoleizhang.com/index.php/archives/82/ "CentOS 7.6 安装 Logstash") **环境配置** |服务器| IP地址| | ------------ | ------------ | |Logstash+Nginx服务器|10.60.60.60| |Elasticsearch服务器|10.60.60.9| |Kibana服务器|10.60.60.27| ------------ ------------ # 一、Elasticsearch简介 ## 1、Elasticsearch 是什么? Elasticsearch 是一个分布式的免费开源搜索和分析引擎,适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。Elasticsearch 在 Apache Lucene 的基础上开发而成,由 Elasticsearch N.V.(即现在的 Elastic)于 2010 年首次发布。Elasticsearch 以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名,是 Elastic Stack 的核心组件;Elastic Stack 是一套适用于数据采集、扩充、存储、分析和可视化的免费开源工具。人们通常将 Elastic Stack 称为 ELK Stack(代指 Elasticsearch、Logstash 和 Kibana),目前 Elastic Stack 包括一系列丰富的轻量型数据采集代理,这些代理统称为 Beats,可用来向 Elasticsearch 发送数据。 Elasticsearch 教程 ## 2、Elasticsearch 的用途是什么? Elasticsearch 在速度和可扩展性方面都表现出色,而且还能够索引多种类型的内容,这意味着其可用于多种用例: - 应用程序搜索 - 网站搜索 - 企业搜索 - 日志处理和分析 - 基础设施指标和容器监测 - 应用程序性能监测 - 地理空间数据分析和可视化 - 安全分析 - 业务分析 ## 3、Elasticsearch 的工作原理是什么? 原始数据会从多个来源(包括日志、系统指标和网络应用程序)输入到 Elasticsearch 中。数据采集指在 Elasticsearch 中进行索引之前解析、标准化并充实这些原始数据的过程。这些数据在 Elasticsearch 中索引完成之后,用户便可针对他们的数据运行复杂的查询,并使用聚合来检索自身数据的复杂汇总。在 Kibana 中,用户可以基于自己的数据创建强大的可视化,分享仪表板,并对 Elastic Stack 进行管理。 ## 4、Elasticsearch 索引是什么? Elasticsearch 索引指相互关联的文档集合。Elasticsearch 会以 JSON 文档的形式存储数据。每个文档都会在一组键(字段或属性的名称)和它们对应的值(字符串、数字、布尔值、日期、数值组、地理位置或其他类型的数据)之间建立联系。 Elasticsearch 使用的是一种名为倒排索引的数据结构,这一结构的设计可以允许十分快速地进行全文本搜索。倒排索引会列出在所有文档中出现的每个特有词汇,并且可以找到包含每个词汇的全部文档。 在索引过程中,Elasticsearch 会存储文档并构建倒排索引,这样用户便可以近实时地对文档数据进行搜索。索引过程是在索引 API 中启动的,通过此 API 您既可向特定索引中添加 JSON 文档,也可更改特定索引中的 JSON 文档。 ## 5、为何使用 Elasticsearch? **Elasticsearch 很快。**由于 Elasticsearch 是在 Lucene 基础上构建而成的,所以在全文本搜索方面表现十分出色。Elasticsearch 同时还是一个近实时的搜索平台,这意味着从文档索引操作到文档变为可搜索状态之间的延时很短,一般只有一秒。因此,Elasticsearch 非常适用于对时间有严苛要求的用例,例如安全分析和基础设施监测。 **Elasticsearch 具有分布式的本质特征。**Elasticsearch 中存储的文档分布在不同的容器中,这些容器称为分片,可以进行复制以提供数据冗余副本,以防发生硬件故障。Elasticsearch 的分布式特性使得它可以扩展至数百台(甚至数千台)服务器,并处理 PB 量级的数据。 **Elasticsearch 包含一系列广泛的功能。**除了速度、可扩展性和弹性等优势以外,Elasticsearch 还有大量强大的内置功能(例如数据汇总和索引生命周期管理),可以方便用户更加高效地存储和搜索数据。 **Elastic Stack 简化了数据采集、可视化和报告过程。**通过与 Beats 和 Logstash 进行集成,用户能够在向 Elasticsearch 中索引数据之前轻松地处理数据。同时,Kibana 不仅可针对 Elasticsearch 数据提供实时可视化,同时还提供 UI 以便用户快速访问应用程序性能监测 (APM)、日志和基础设施指标等数据。 # 二、安装 Elasticsearch ## 1、Elasticsearch 下载 可以去官网下载 Elasticsearch ,下载后上传至服务器内 下载链接: https://www.elastic.co/cn/downloads/elasticsearch ![Elasticsearch官网下载.png](https://www.xiaoleizhang.com/usr/uploads/2022/01/2341748436.png) ```shell [root@10-60-60-9 ~]# rz [root@10-60-60-9 ~]# ll total 335616 -rw-r--r-- 1 root root 343664171 Jan 5 11:08 elasticsearch-7.16.2-linux-x86_64.tar.gz [root@10-60-60-9 ~]# ``` ## 2、解压并重命名 ```shell tar -zxvf elasticsearch-7.16.2-linux-x86_64.tar.gz mv elasticsearch-7.16.2 /usr/local/elasticsearch ``` ## 3、修改配置文件 ```shell vim /usr/local/elasticsearch/config/elasticsearch.yml ``` 修改如下的配置,前面有#的去掉注释 ```shell node.name: xiaolei #节点名称 path.data: /usr/local/elasticsearch/data #存储数据的路径 path.logs: /usr/local/elasticsearch/logs #存储日志的路径 network.host: 0.0.0.0 #默认为:127.0.0.1,但仅限本地主机节点能访问,为了使其他主机节点也能访问,可以修改为:0.0.0.0 http.port: 9200 #设定监听端口 cluster.initial_master_nodes: ["xiaolei"] #这里设置的要和node.name一致,不然在kibana那会报错 ``` cluster.initial_master_nodes没配置的话会报错如下,这里一定是要配置cluster.initial_master_nodes不然kibana启动会报错 ```shell [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured ``` 看提示可知:缺少默认配置,至少需要配置discovery.seed_hosts/discovery.seed_providers/cluster.initial_master_nodes中的一个参数。 - discovery.seed_hosts: 集群主机列表 - discovery.seed_providers: 基于配置文件配置集群主机列表 - cluster.initial_master_nodes: 启动时初始化的参与选主的node,生产环境必填 接着使用root用户修改sysctl.conf ```shell vim /etc/sysctl.conf ``` 在最底下增加如下,或者有配置的话修改下 ```shell vm.max_map_count = 655360 ``` 并执行命令 ```shell sysctl -p ``` 如果这个值没有高于262144,则会有如下报错 ```shell max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] ``` max_map_count文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量 默认情况下,Elasticsearch使用混合mmapfs/niofs目录来存储其索引。默认操作系统对mmap计数的限制可能太低,这可能导致内存不足异常 上述的修改完成后,需要创建一个新用户,然后使用新用户去启动Elasticsearch ```shell adduser elasticsearch passwd elasticsearch chown -R elasticsearch /usr/local/elasticsearch/ su elasticsearch ``` ## 4、启动Elasticsearch 启动elasticsearch,后台启动-d,前台启动就不需要-d了。 ```shell [elasticsearch@10-60-60-9 root]$ /usr/local/elasticsearch/bin/elasticsearch -d ``` 可以通过:curl 127.0.0.1:9200,验证,如果返回以下信息,说明启动成功 ```shell [root@10-60-60-9 ~]# curl 127.0.0.1:9200 { "name" : "10-60-60-9", "cluster_name" : "elasticsearch", "cluster_uuid" : "_na_", "version" : { "number" : "7.16.2", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "2b937c44140b6559905130a8650c64dbd0879cfb", "build_date" : "2021-12-18T19:42:46.604893745Z", "build_snapshot" : false, "lucene_version" : "8.10.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" } [root@10-60-60-9 ~]# ``` ## 5、Elasticsearch做成Service自启 也可以把elasticsearch做成Service自启,比较方便 ```shell vim /etc/systemd/system/elasticsearch.service ``` 内容如下 ```shell [Unit] Description=elasticsearch [Service] User=elasticsearch #nofile : 一个进程最多能打开的的文件数 LimitNOFILE=100000 #nproc : 一个用户最多能创建的进程数 LimitNPROC=100000 # elasticsearch安装路径 (指定自己的安装路径) ExecStart=/usr/local/elasticsearch/bin/elasticsearch [Install] WantedBy=multi-user.target ``` 启动服务以及设置开机自启(注意start服务之前把先前启动的进程先关了) ```shell systemctl daemon-reload systemctl start elasticsearch.service systemctl enable elasticsearch.service ``` ```shell [root@10-60-60-9 ~]# systemctl status elasticsearch.service ● elasticsearch.service - elasticsearch Loaded: loaded (/etc/systemd/system/elasticsearch.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2022-01-07 12:21:23 CST; 2h 27min ago Main PID: 10307 (java) CGroup: /system.slice/elasticsearch.service ├─10307 /usr/local/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -D... └─10512 /usr/local/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller Jan 07 12:21:43 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:43,870][INFO ][o.e.i.g.DatabaseNodeService] [xiaolei] successfully reloaded changed geoip databas...-ASN.mmdb] Jan 07 12:21:46 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:46,046][INFO ][o.e.i.g.DatabaseNodeService] [xiaolei] downloading geoip database [GeoLite2-City.m...db.tmp.gz] Jan 07 12:21:46 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:46,076][INFO ][o.e.i.g.GeoIpDownloader ] [xiaolei] updated geoip database [GeoLite2-City.mmdb] Jan 07 12:21:46 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:46,077][INFO ][o.e.i.g.GeoIpDownloader ] [xiaolei] updating geoip database [GeoLite2-Country.mmdb] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,032][INFO ][o.e.i.g.DatabaseNodeService] [xiaolei] downloading geoip database [GeoLite2-Countr...db.tmp.gz] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,058][INFO ][o.e.i.g.GeoIpDownloader ] [xiaolei] updated geoip database [GeoLite2-Country.mmdb] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,122][INFO ][o.e.i.g.DatabaseReaderLazyLoader] [xiaolei] evicted [0] entries from cache after r...ntry.mmdb] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,123][INFO ][o.e.i.g.DatabaseNodeService] [xiaolei] successfully reloaded changed geoip databas...ntry.mmdb] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,218][INFO ][o.e.i.g.DatabaseReaderLazyLoader] [xiaolei] evicted [0] entries from cache after r...City.mmdb] Jan 07 12:21:47 10-60-60-9 elasticsearch[10307]: [2022-01-07T12:21:47,219][INFO ][o.e.i.g.DatabaseNodeService] [xiaolei] successfully reloaded changed geoip databas...City.mmdb] Hint: Some lines were ellipsized, use -l to show in full. [root@10-60-60-9 ~]# ``` 就这样Elasticsearch安装完成了!!! 最后修改:2022 年 01 月 25 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏