发表于 2019-1-28 10:43:10

搭建部署 分布式ELK平台 (一)

  搭建部署 分布式ELK平台

  ELK 是什么 ?
  ELK 其实并不是一款软件,而是一整套解决方案,是三个软件产品的首字母缩写
  – Elasticsearch:负责日志检索和储存
  – Logstash:负责日志的收集和分析、处理
  – Kibana:负责日志的可视化
  这三款软件都是开源软件,通常是配合使用,而且又先后归于 Elastic.co 公司名下,故被简称为 ELK
  ELK 能做什么?
  · ELK组件在海量日志系统的运维中,可用于解决:
  – 分布式日志数据集中式查询和管理
  – 系统监控,包含系统硬件和应用各个组件的监控
  – 故障排查
  – 安全信息和事件管理
  – 报表功能
  Elasticsearch
  · ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful API 的 web 接口。
  · Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便
  · 主要特点
  – 实时分析
  – 分布式实时文件存储,并将每一个字段都编入索引
  – 文档导向,所有的对象全部是文档
  – 高可用性,易扩展,支持集群(Cluster)、分片和复制(Shards 和 Replicas)
  – 接口友好,支持 JSON
  · Elasticsearch 没有典型意义的事务.
  · Elasticsearch 是一种面向文档的数据库。
  · Elasticsearch 没有提供授权和认证特性
  · 相关概念:
  – Node: 装有一个 ES 服务器的节点。
  – Cluster: 有多个Node组成的集群
  – Document: 一个可被搜素的基础信息单元
  – Index: 拥有相似特征的文档的集合
  – Type: 一个索引中可以定义一种或多种类型
  – Filed: 是 ES 的最小单位,相当于数据的某一列
  – Shards: 索引的分片,每一个分片就是一个 Shard
  – Replicas: 索引的拷贝
  · ES 与关系型数据库的对比
  – 在 ES 中,文档归属于一种 类型 (type) ,而这些类型存在于索引 (index) 中,类比传统关系型数据库
  – DB -> Databases -> Tables -> Rows -> Columns
  – 关系型   数据库         表         行         列
  – ES -> Indices -> Types -> Documents -> Fields
  – ES      索引         类型         文档         域(字段)
http://s1.运维网.com/images/20180125/1516844261727764.jpg
  Elasticsearch架构图

http://s1.运维网.com/images/20180125/1516844537643217.jpg
  ES 集群安装
  准备集群 es1 es2 es3 es4 es5 五台主机 部署集群
  

  步骤1 设置ip 与主机名称对应关系
  # ssh -x root@192.168.4.11
  # vim /etc/hosts
  ...
  192.168.4.11    es1
  192.168.4.12    es2
  192.168.4.13    es3
  192.168.4.14    es4
  192.168.4.15    es5
  # for i in {12..15}; do rsync -a /etc/hosts 192.168.4.${i}:/etc/hosts ;done
  步骤2 安装 JDK

  – Elasticsearch 要求至少 Java 7
  – 一般推荐使用 OpenJDK 1.8
  – 配置好安装源以后,我们先解决依赖关系
  # yum install -y java-1.8.0-openjdk
  步骤 3 安装ES
  # rpm -ivhelasticsearch-2.3.4-1.noarch
  步骤 4 修改配置文件
  # ssh -x root@192.168.4.11
  # vim /etc/elasticsearch/elasticsearch.yml
  # grep -v "^#" /etc/elasticsearch/elasticsearch.yml
  cluster.name: es-test
  node.name: es1
  network.host: 0.0.0.0
  discovery.zen.ping.unicast.hosts: ["es1", "es2", "es3"]
  //其他主机可以使用如下方法 快速修改配置文件
  # for i in {12..15}; do rsync -a /etc/elasticsearch/elasticsearch.yml 192.168.4.${i}:/etc/elasticsearch/elasticsearch.yml ; done
  # for i in {2..5}; dossh es${i} 'sed -i "s/^\(node.name:\).*/\1 ${HOSTNAME}/" /etc/elasticsearch/elasticsearch.yml' ; done
  步骤5 启动服务,设置自启动
  # systemctl enable elasticsearch
  # systemctl start elasticsearch
  //其他主机可以使用如下方法 快速启动服务
  # for i in {12..15}; do ssh 192.168.4.${i} systemctl restart elasticsearch.service; done
  # for i in {12..15}; do ssh 192.168.4.${i} systemctl enable elasticsearch.service; done
  验证
  # nmap -n -sS -p 9200,9300 192.168.4.12-15
  Starting Nmap 6.40 ( http://nmap.org ) at 2018-01-24 02:36 EST
  Nmap scan report for 192.168.4.12
  Host is up (0.00037s latency).
  PORT   STATE SERVICE
  9200/tcp openwap-wsp
  9300/tcp openvrace
  MAC Address: 74:52:51:32:11:01 (Unknown)
  Nmap scan report for 192.168.4.13
  Host is up (0.00038s latency).
  PORT   STATE SERVICE
  9200/tcp openwap-wsp
  9300/tcp openvrace
  MAC Address: 74:52:51:32:12:01 (Unknown)
  Nmap scan report for 192.168.4.14
  Host is up (0.00036s latency).
  PORT   STATE SERVICE
  9200/tcp openwap-wsp
  9300/tcp openvrace
  MAC Address: 74:52:51:32:13:01 (Unknown)
  Nmap scan report for 192.168.4.15
  Host is up (0.00037s latency).
  PORT   STATE SERVICE
  9200/tcp openwap-wsp
  9300/tcp openvrace
  MAC Address: 74:52:51:32:14:01 (Unknown)
  Nmap done: 4 IP addresses (4 hosts up) scanned in 0.05 seconds
  · 通过浏览器或 curl 访问 9200 端口
  # curl http://es1:9200/_cluster/health?pretty
  {
  "cluster_name" : "es-test",      – 返回字段解析
  "status" : "green",                      – 集群状态,绿色为正常,×××表示有问题但不是很严重,红色表示严重故障
  "timed_out" : false,
  "number_of_nodes" : 5,            – 5, 表示集群中节点的数量
  "number_of_data_nodes" : 5,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "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
  }
  ES 常用插件
  · head 插件:
  – 它展现ES集群的拓扑结构,并且可以通过它来进行索引(Index)和节点(Node)级别的操作
  – 它提供一组针对集群的查询API,并将结果以json和表格形式返回
  – 它提供一些快捷菜单,用以展现集群的各种状态
  · kopf 插件
  – 是一个ElasticSearch的管理工具
  – 它提供了对ES集群操作的API
  · bigdesk 插件
  – 是elasticsearch的一个集群监控工具
  – 可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等
  ES 插件安装,查看
  插件可以安装在集群当中任意一台主机
  – 安装插件
  # cd /usr/share/elasticsearch/bin/
  # ./plugin installfile:///root/bigdesk-master.zip
  # ./plugin installfile:///root/elasticsearch-head-master.zip
  # ./plugin installfile:///root/elasticsearch-kopf-master.zip
  这里必须使用 url 的方式进行安装,如果文件在本地,我们也需要使用 file:// 的方式指定路径,
  例如文件在/tmp/xxx 下面,我们要写成 file:///tmp/xxx 删除使用remove 指令
  –查看安装的插件   

  # ./plugin list
  Installed plugins in /usr/share/elasticsearch/plugins:
  - bigdesk
  - head
  - kopf
  查看插件
  # firefox http://es1:9200/_plugin/head
http://s1.运维网.com/images/20180125/1516884379867583.jpg
  # firefox http://es1:9200/_plugin/kopf
http://s1.运维网.com/images/20180125/1516884402215639.jpg
  # firefox http://es1:9200/_plugin/bigdesk
http://s1.运维网.com/images/20180125/1516884419262732.jpg
  HTTP与 RESTful API
  · Elasticsearch提供了一系列RESTful的API
  – 检查集群、节点、索引的健康度、状态和统计
  – 管理集群、节点、索引的数据及元数据
  – 对索引进行CRUD操作及查询操作
  – 执行其他高级操作如分页、排序、过滤等
  · POST 或 PUT 数据使用 json 格式
  · json
  – JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式。
  – json 传输的就是一个字符串
  – python 中对应的 字符串,列表,字典都可以转换成对应的 json 格式
  · Rest API 的简单使用
  – _cat API 查询集群状态,节点信息
  # curl -X "GET" http://192.168.4.13:9200/_cat/
  =^.^=
  /_cat/allocation
  /_cat/shards
  /_cat/shards/{index}
  /_cat/master
  /_cat/nodes
  /_cat/indices
  /_cat/indices/{index}
  /_cat/segments
  /_cat/segments/{index}
  /_cat/count
  /_cat/count/{index}
  /_cat/recovery
  /_cat/recovery/{index}
  /_cat/health
  /_cat/pending_tasks
  /_cat/aliases
  /_cat/aliases/{alias}
  /_cat/thread_pool
  /_cat/plugins
  /_cat/fielddata
  /_cat/fielddata/{fields}
  /_cat/nodeattrs
  /_cat/repositories
  /_cat/snapshots/{repository}
– nodes 查询节点状态信息
# curl -X "GET" http://192.168.4.13:9200/_cat/nodes
192.168.4.12 192.168.4.12 5 89 0.00 d m es2
192.168.4.13 192.168.4.13 4 75 0.01 d m es3
192.168.4.11 192.168.4.11 5 85 0.01 d m es1
192.168.4.15 192.168.4.15 6 73 0.00 d * es5
192.168.4.14 192.168.4.14 5 76 0.00 d m es4
  – v 参数显示详细信息
  # curl -X "GET" http://192.168.4.13:9200/_cat/nodes?v
  – help 显示帮助信息
  # curl -X "GET" http://192.168.4.13:9200/_cat/nodes?help
  · HTTP Methods 和 RESTful API 设计
  – HTTP Methods 也叫 HTTP Verbs, 它们是 HTTP 协议的一部分, 主要规定了 HTTP 如何请求和操作服务器上的资源,常见的有GET, POST等
  – HTTP Methods 一共有九个,分别是 GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH
  · HTTP Methods
  – 在RESTful API 设计中,常用的有POST,GET,PUT,PATCH 和 DELETE。分别对应对资源的创建,获取,修改,部分修改和删除操作。
  – 我们默认访问 ES API 的方法是 GET,如果要对数据库增加、删除、修改数据我们还要使用对应的方法
  – GET 查询
  – PUT 增加
  – POST 更改
  – DELETE 删除
  · RESTful API 增加
  — 创建一个school 的(index) 和 一个students (Type)
  — 并增加两条信息
  # curl -X "PUT" 'http://192.168.4.11:9200/school/student/1' -d '{
  >   "title": "devops",
  >    "name":{
  > "first":"aa",
  > "last":"bb"
  >    },
  >   "age":25
  > }'
  {"_index":"school","_type":"student","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
  # curl -X "PUT" 'http://192.168.4.11:9200/school/student/2' -d '{
  >   "title": "devops",
  >    "name":{
  > "first":"AA",
  > "last":"XX"
  >    },
  >   "age":25
  > }'
  {"_index":"school","_type":"student","_id":"2","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true}
http://s1.运维网.com/images/20180125/1516885505995134.jpg
http://s1.运维网.com/images/20180125/1516885533149283.jpg
  · RESTful API 更改
  — 修改school 下面students 的第一个文档中的age 信息,从25修改30
  # curl -X "POST" 'http://192.168.4.11:9200/school/student/1/_update' -d '{
  >    "doc":{
  >"age":30
  >   }
  >}'
  {"_index":"school","_type":"student","_id":"1","_version":2,"_shards":{"total":2,"successful":2,"failed":0}}
http://s1.运维网.com/images/20180125/1516886013880556.jpg
  · RESTful API 查询
  — 查询刚刚创建的文挡信息
  # curl -X "GET" 'http://192.168.4.11:9200/school/student/1'
  {"_index":"school","_type":"student","_id":"1","_version":2,"found":true,"_source":{
  "title":"devops",
  "name":{
  "first":"aa",
  "last":"bb"
  },
  "age":30
  }}
  # curl -X "GET" 'http://192.168.4.11:9200/school/student/2'
  {"_index":"school","_type":"student","_id":"2","_version":1,"found":true,"_source":{
  "title": "devops",
  "name":{
  "first":"AA",
  "last":"XX"
  },
  "age":25
  }}
  · RESTful API 删除
  # curl -X "DELETE" 'http://192.168.4.11:9200/school/student/2'
  {"found":true,"_index":"school","_type":"student","_id":"2","_version":2,"_shards":{"total":2,"successful":2,"failed":0}}
  # curl -X "DELETE" 'http://192.168.4.11:9200/school/student/1'
  {"found":true,"_index":"school","_type":"student","_id":"1","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}
  # curl -X "DELETE" 'http://192.168.4.11:9200/school'
  {"acknowledged":true}
http://s1.运维网.com/images/20180125/1516887012644645.jpg
  kibana
  · kibana是什么
  – 数据可视化平台工具
  · 特点:
  – 灵活的分析和可视化平台
  – 实时总结和流数据的图表
  – 为不同的用户显示直观的界面
  – 即时分享和嵌入的仪表板
  Kibana 安装
  – kibana 的安装非常简单,我们使用 rpm 方式安装
  # rpm -ivh kibana-4.5.2-1.x86_64.rpm
  – kibana 默认安装在 /opt/kibana 下面,配置文件在/opt/kibana/config/kibana.yml
  # rpm -qc kibana
  /opt/kibana/config/kibana.yml
  · kibana.yml 的配置
  # vim /opt/kibana/config/kibana.yml
  # grep -Pv '^(#|$)' /opt/kibana/config/kibana.yml
  server.port: 5601
  server.host: "0.0.0.0"
  elasticsearch.url: "http://192.168.4.11:9200"
  kibana.index: ".kibana"
  kibana.defaultAppId: "discover"
  elasticsearch.pingTimeout: 1500
  elasticsearch.requestTimeout: 30000
  elasticsearch.startupTimeout: 5000
  — 启动服务 设置开机启动

  # systemctl start kibana.service
  # systemctl enable kibana.service
  # netstat -pantu | grep 5601

  tcp      0      0 0.0.0.0:5601            0.0.0.0:*               LISTEN      10011/node
  —web 访问kibana
  http://192.168.4.10:5601/
http://s1.运维网.com/images/20180125/1516888361246791.jpg
http://s1.运维网.com/images/20180125/1516888391461096.jpg
  http://192.168.4.11:9200/_plugin/head
http://s1.运维网.com/images/20180126/1516936688520447.jpg
  数据批量导入
  # ls json/
  accounts.json   logs.jsonlshakespeare.json
  · 使用 _bulk 批量导入数据
  – 批量导入数据使用 POST 方式,数据格式为 json,url编码使用 data-binary
  – 导入含有 index 配置的 json 文件
  # curl -X "POST" 'http://192.168.4.11:9200/_bulk' --data-binary @logs.jsonl
  # curl -X "POST" 'http://192.168.4.11:9200/_bulk' --data-binary @shakespeare.json
  · 使用 _bulk 批量导入数据
  – 导入没有有 index 配置的 json 文件
  – 我们需要在 uri 里面制定 index 和 type
  # curl -X "POST" 'http://192.168.4.11:9200/accounts/act/_bulk' --data-binary @accounts.json
  数据批量查询
  数据批量查询使用 GET
  # curl -X 'GET' "http://192.168.4.11:9200/_mget?pretty" -d'{
  > "docs":[
  >   {
  >       "_index":"accounts",
  >       "_type":"act",
  >       "_id":3
  >   }
  >]
  > }'
  {
  "docs" : [ {
  "_index" : "accounts",
  "_type" : "act",
  "_id" : "3",
  "_version" : 1,
  "found" : true,
  "_source" : {
  "account_number" : 3,
  "balance" : 44947,
  "firstname" : "Levine",
  "lastname" : "Burks",
  "age" : 26,
  "gender" : "F",
  "address" : "328 Wilson Avenue",
  "employer" : "Amtap",
  "email" : "levineburks@amtap.com",
  "city" : "Cochranville",
  "state" : "HI"
  }
  } ]
  }
  # curl -X 'GET' "http://192.168.4.11:9200/accounts/act/3?pretty"       //另一种查询方法 结果一样
  //也可以同时查询多个
  # curl -X 'GET' "http://192.168.4.11:9200/_mget?pretty" -d '{
  > "docs":[
  > {
  > "_index":"accounts",
  > "_type":"act",
  > "_id":3
  > },
  > {
  > "_index":"accounts",
  > "_type":"act",
  > "_id":5
  > },
  > {
  > "_index":"shakespeare",
  > "_type":"line",
  > "_id":110
  > }
  > ]
  > }'
  数据导入以后查看logs 是否导入成功
http://s1.运维网.com/images/20180126/1516945205512548.jpg
  修改kibana 的配置文件后启动 kibana ,然后查看ES集群,如果出现 .kibana Index 表示kibana 与ES集群连接成功
http://s1.运维网.com/images/20180126/1516945219163624.jpg

  kibana 里选择日志
  — 支持通配符 *
  — 我们这里选择 logstash-*
  – 在下面的 Time-field 选择 @timestramp 作为索引
  – 然后点 create 按钮
http://s1.运维网.com/images/20180126/1516966971667083.jpg
http://s1.运维网.com/images/20180126/1516967184423781.jpg
  这里显示数据没有找到 由上角可以看见 系统默认选择的是 最近15分钟

http://s1.运维网.com/images/20180126/1516967427301928.jpg
  原因是我们刚刚导入的日志是2015-05-10至 2015-05-20 时间段的,这里我们修改一下时间显示就可以看见数据展示了
http://s1.运维网.com/images/20180126/1516968067957342.jpg
  数据展示

http://s1.运维网.com/images/20180126/1516968295612099.jpg
  除了柱状图,kibana 还支持很多种展示方式
http://s1.运维网.com/images/20180126/1516968459910184.jpg
http://s1.运维网.com/images/20180126/1516968831541468.jpg
http://s1.运维网.com/images/20180126/1516968848134765.jpg
http://s1.运维网.com/images/20180126/1516968878450048.jpghttp://s1.运维网.com/images/20180126/1516969134239858.jpg
  这里选项就是日志文件的字段类型,每个字段类型代表不同的数据
http://s1.运维网.com/images/20180126/1516969172743127.jpg
  多种维度自定义统计分析
http://s1.运维网.com/images/20180126/1516969581861703.jpg
  保存后可以在 Dashboard 查看
http://s1.运维网.com/images/20180126/1516970156543549.jpg
  




页: [1]
查看完整版本: 搭建部署 分布式ELK平台 (一)