Py学习  »  Elasticsearch

ELK教程1:ElasticSearch集群的部署

方志朋 • 4 年前 • 477 次点击  

点击上方“方志朋”,选择“设为星标

做积极的人,而不是积极废人


在分布式系统中,应用数量众多,应用调用链复杂,常常使用ELK作为日志收集、分析和展示的组件。本篇文章将讲讲解如何部署ELK,然后讲解如何 使用Filebeat采集Spring Boot的日志输出到Logstash上,logstash再将日志输出到Elasticsearch上,最后展示到kibana上面。整个日志采集流程如下图:

在传统的日志采集只会用ELK,那么为什么需要使用filebeat呢,因为 logstash是java应用,解析日志是非的消耗cpu和内存,logstash安装在应用部署的机器上显得非常的影响应用的性能。最常见的做法是用filebeat部署在应用的机器上,logstash单独部署,然后由 filebeat将日志输出给logstash解析,解析完由logstash再传给elasticsearch。

安装计划

本文主要讲解如何部署ElasticSearch 集群,部署的ElasticSearch的版本为7.2,计划用三台机器组成一个ElasticSearch集群,从而组成高可用,机器分配如下:

| 节点 | 规则 | 数量 | | -------- | -----: | :----: | | 192.168.1.1 | 2核4G | 1 | | 192.168.1.2 | 2核4G | 1 | | 192.168.1.3 | 2核4G | 1 |

安装

下载安装执行以下命令:

  1. # 下载elasticsearch-7.2.0-x86_64的rpm包

  2. wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-x86_64.rpm

  3. wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-x86_64.rpm.sha512

  4. # shasum 检查版本信息

  5. shasum -a 512 -c elasticsearch-7.2.0- x86_64.rpm.sha512

  6. # rpm本地安装

  7. sudo rpm --install elasticsearch-7.2.0-x86_64.rpm

安装成功ElasticSearch成功后,执行一下命令启动elasticSearch,并设置为开启自启动:

  1. sudo systemctl daemon-reload

  2. sudo systemctl enable elasticsearch.service

  3. sudo systemctl start elasticsearch.service

elasticSearch的默认端口为9200,启动成功后,执行以下命令:

  1. curl -X GET "localhost:9200/"

如果返回以下的信息,则证明安装成功:

  1. {

  2. "name" : "VM_0_5_centos",

  3. "cluster_name" : "elasticsearch",

  4. "cluster_uuid" : "gst98AuET6a648YmAkXyMw",

  5. "version" : {

  6. "number" : "7.2.0",

  7. "build_flavor" : "default",

  8. "build_type" : "rpm",

  9. "build_hash" : "508c38a",

  10. "build_date" : "2019-06-20T15:54:18.811730Z",

  11. "build_snapshot" : false,

  12. "lucene_version" : "8.0.0",

  13. "minimum_wire_compatibility_version" : "6.8.0",

  14. "minimum_index_compatibility_version" : "6.0.0-beta1"

  15. },

  16. "tagline" : "You Know, for Search"

  17. }

查看节点的健康状态,执行命令 curl localhost:9200/_cluster/health ,如果返回以下信息,则Elasticsearch则为监控状态。

  1. {

  2. "cluster_name" : "elasticsearch",

  3. "status" : "green",

  4. "timed_out" : false,

  5. "number_of_nodes" : 1,

  6. "number_of_data_nodes" : 1,

  7. "active_primary_shards" : 0,

  8. "active_shards" : 0,

  9. "relocating_shards" : 0,

  10. "initializing_shards" : 0,

  11. "unassigned_shards" : 0,

  12. "delayed_unassigned_shards" : 0,

  13. "number_of_pending_tasks" : 0,

  14. "number_of_in_flight_fetch" : 0,

  15. "task_max_waiting_in_queue_millis" : 0,

  16. "active_shards_percent_as_number" : 100.0

  17. }

可以执行以下的命令,查看es的 journal:

  1. sudo journalctl --unit elasticsearch

配置

以下的路径的配置文件可以配置es的javahome,esconfig_home :

  1. /etc/sysconfig/elasticsearch

es本身的一些配置在以下的路径,在这里可以配置elasticsearch的堆内存,数据保留天数等信息:

  1. /etc/elasticsearch

所有的配置文件描述和路径如下表所示:

配置类型描述路径
homeelasticsearch的home目录/usr/share/elasticsearch
binelasticsearch的bin目录/usr/share/elasticsearch/bin
confelasticsearch的配置文件 /etc/elasticsearch
confelasticsearch的环境变量配置/etc/sysconfig/elasticsearch
dataelasticsearch的数据目录/var/lib/elasticsearch
logselasticsearch的日志目录/var/log/elasticsearch
pluginselasticsearch的插件目录/usr/share/elasticsearch/plugins

集群搭建

在三台机器上分别装完elasticsearch,在主节点vim /etc/elasticsearch/,配置一下的信息:

主节点的配置如下:

  1. #三个集群需要同样的集群名

  2. cluster.name: my-application

  3. # 每个node的名字需要唯一

  4. node.name: node-1


  5. node.master: true

  6. #允许该节点存储数据(默认开启)

  7. node.data: true

  8. #注意一定要是路径后面加上/var/lib/elasticsearch/nodes,要不然无法加入集群

  9. path.data: /var/lib/elasticsearch/nodes

  10. path.logs: /var/log/elasticsearch

  11. network.host: 0.0.0.0

  12. http.port: 9200

  13. transport.tcp.port: 9300

  14. http.cors.enabled : true

  15. http.cors.allow-origin: "*"

  16. xpack.security.enabled: false

  17. discovery.seed_hosts: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

  18. cluster.initial_master_nodes: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

同理从节点的配置:

  1. cluster.name : my-application

  2. # 每个node的名字需要唯一,两个从节点的名字不能相同

  3. node.name: node-2

  4. node.master: true

  5. #允许该节点存储数据(默认开启)

  6. node.data: true

  7. path.data: /var/lib/elasticsearch/nodes

  8. path.logs: /var/log/elasticsearch

  9. network.host: 0.0.0.0

  10. http.port: 9200

  11. transport.tcp.port: 9300

  12. xpack.security.enabled: false

  13. http.cors.enabled: true

  14. http.cors.allow-origin: "*"

  15. discovery.seed_hosts: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

  16. cluster.initial_master_nodes: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

配置完主从节点,需要重启三台elasticsearch,重启命令如下:

  1. sudo systemctl restart elasticsearch.service

重启三台Elasticsearch后,执行curl http://localhost:9200/_cat/master,如果响应如下,则证明安装成功

  1. SHBUDVUAQhi7FauSoI8bMg 192.168.1.1 192.168.1.1 node-1

也路执行命令curl -XGET 'http://127.0.0.1:9200/_cat/nodes?pretty',返回类型如下的数据则安装成功:

  1. 192.168.1.2 9 97 1 0.00 0.03 0.05 mdi - node-2

  2. 192.168.1.3 97 0 0.01 0.07 0.07 mdi - node-3

  3. 192.168.1.1 18 97 2 0.05 0.05 0.05 mdi * node-1

也可以通过命令curl localhost:9200/_cluster/health?pretty,查看集群的健康状态:

  1. {

  2. "cluster_name" : "my-application",

  3. "status" : "green",

  4. "timed_out" : false,

  5. "number_of_nodes" : 3,

  6. "number_of_data_nodes" : 3,

  7. "active_primary_shards" : 5,

  8. "active_shards" : 10,

  9. "relocating_shards" : 0,

  10. "initializing_shards" : 0,

  11. "unassigned_shards" : 0,

  12. "delayed_unassigned_shards" : 0,

  13. "number_of_pending_tasks" : 0,

  14. "number_of_in_flight_fetch" : 0,

  15. "task_max_waiting_in_queue_millis" : 0,

  16. "active_shards_percent_as_number" : 100.0

  17. }

Elasticsearch中信息很多,同时ES也有很多信息查看命令,可以帮助开发者快速查询Elasticsearch的相关信息。

  1. $ curl localhost:9200/_cat

  2. =^.^=

  3. /_cat/allocation

  4. /_cat/shards

  5. /_cat/shards/{index}

  6. /_cat/master

  7. /_cat/nodes

  8. /_cat/indices

  9. /_cat/indices/{index}

  10. /_cat/segments

  11. /_cat/segments/{index}

  12. /_cat/count

  13. /_cat/count/{index}

  14. /_cat/recovery

  15. /_cat/recovery/{index}

  16. /_cat/health

  17. / _cat/pending_tasks

  18. /_cat/aliases

  19. /_cat/aliases/{alias}

  20. /_cat/thread_pool

  21. /_cat/plugins

  22. /_cat/fielddata

  23. /_cat/fielddata/{fields}

head安装

在elasticsearch的高级版本之后,head不在是一个插件,而是一个独立的应用来部署,源码地址在https://github.com/mobz/elasticsearch-head。

需要提前安装node,安装命令如下:

  1. git clone git://github.com/mobz/elasticsearch-head.git

  2. cd elasticsearch- head

  3. npm install

  4. npm run start

安装成功后,打开 http://localhost:9100/,浏览器显示如下的界面,则安装成功:

如上图所示,在Head组件的界面上,可以很方便的查看集群的状态和数据。

参考资料

https://www.elastic.co/guide/en/elasticsearch/reference/7.2/rpm.html https://www.elastic.co/guide/index.html https://www.elastic.co/guide/en/elasticsearch/plugins/current/installation.html

热门内容:


喜欢就点个"在看"呗^_^

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/36632
 
477 次点击