联系我们

公司地址: 上海市沪宜公路1188号4号楼
     一层
联系电话:021-31080981
电子邮箱:soline@soline.com.cn
邮政编码:201802

利用ELK+Kafka解决方案

ELK 是三款软件的组合。是一整套完整的解决方案。分别是由 Logstash(收集+分析)、ElasticSearch(搜索+存储)、Kibana(可视化展示)三款软件。ELK主要是为了在海量的日志系统里面实现分布式日志数据集中式管理和查询,便于监控以及排查故障。

Elasticsearch 部署安装

ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful API 的 web 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

[root@seichung ] wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.rpm    # 官网下载软件包[root@seichung ] yum -y install java-1.8.0     # 相关依赖包[root@seichung ] yum -y install epel-release[root@seichung ] yum localinstall -y elasticsearch-6.4.2.rpm

安装完成之后,修改 ElasticSearch 的配置文件,然后启动 ElasticSearch 服务。

[root@seichung ] vim /etc/elasticsearch/elasticsearch.yml            # 将如下的内容的注释取消,并将相关信息改成你在配置时的实际参数
            ***注意:配置文件的参数,在冒号后都有一个空格,如没有空格,服务会启动失败,并且无任何提示
              cluster.name: ES-cluster   # 集群名称,可更改
              node.name: es1             # 为本机的主机名
              network.host: 192.168.4.1  # 为本机的IP
              http.port: 9200            # elasticsearch服务端口,可更改
              discovery.zen.ping.unicast.hosts: ["es1", "es2","es3"]    # 集群内的主机(主机名,不一定全部都要写)
              node.master: true                                # 节点是否被选举为 master
              node.data: true                                  # 节点是否存储数据
              index.number_of_shards: 4                        # 索引分片的个数
              index.number_of_replicas: 1                      # 分片的副本个数
              path.conf: /etc/elasticsearch/                   # 配置文件的路径
              path.data: /data/es/data                         # 数据目录路径
              path.work: /data/es/elasticsearch                # 工作目录路径
              path.logs:  /data/es/logs/elasticsearch/logs/    # 日志文件路径
              path.plugins:  /data/es/plugins                  # 插件路径
              bootstrap.mlockall: true                         # 内存不向 swap 交换
              http.enabled: true                               # 启用http
              # 如果目录路径不存在的,需要先创建[root@seichung ] /etc/init.d/elasticsearch start    # 启动服务[root@seichung ] systemctl enable elasticsearch     # 开机自启

一个简单的 ElasticSearch 就搭建好了,如果要部署一个ES集群,那么只需要在所有主机上部署好 Java 环境,以及在所有主机上的 /etc/hosts 解析主机,如下:

此操作是在ES集群主机上必做

[root@seichung ] yum -y install java-1.8.0[root@seichung ] vim /etc/hosts            192.168.4.1 es1            192.168.4.2 es2            192.168.4.3 es3

将已经完成 elasticsearch 服务安装的主机,将 hosts 文件、elasticsearch.yml 配置文件拷贝一份到集群主机上,只要修改 node.name: 为本机的主机名 即可,然后将服务启动起来

最后来查看下 Es 集群是否部署好,不成功则 number_of_nodes 永远为 1 ,成功会有下图信息,:

[root@seichung ] curl -i http://192.168.4.1:9200/_cluster/health?pretty
    # 返回的信息包括集群名称、集群数量等,如果 number_of_nodes 显示的是实际得集群数量,则说明集群部署成功
    {
      "cluster_name" : "ES-cluster",
      "status" : "green",
      "timed_out" : false,
      "number_of_nodes" : 3,
      "number_of_data_nodes" : 3,
      "active_primary_shards" : 26,
      "active_shards" : 52,
      "relocating_shards" : 0,
      "initializing_shards" : 0,
      "unassigned_shards" : 0,
      "delayed_unassigned_shards" : 0,
      "number_of_pending_tasks" : 0,
      "number_of_in_flight_fetch" : 0,
      "task_max_waiting_in_queue_millis" : 0,
      "active_shards_percent_as_number" : 100.0
    }

ElasticSearch 插件安装

# 采用本地安装,也可以采用远程安装[root@seichung ] /usr/share/elasticsearch/bin/plugin install file:///data/es/plugins/elasticsearch-head-master.zip    [root@seichung ] /usr/share/elasticsearch/bin/plugin install file:///data/es/plugins/elasticsearch-kopf-master.zip[root@seichung ] /usr/share/elasticsearch/bin/plugin install file:///data/es/plugins/bigdesk-master.zip[root@seichung ] /usr/share/elasticsearch/bin/plugin list     # 查看已安装的插件

插件安装完成之后,访问对应插件的 Url则成功