Top

NSD ARCHITECTURE DAY01

  1. 案例1:安装Elasticsearch
  2. 案例2:部署Elasticsearch集群
  3. 案例3:安装Elasticsearch插件
  4. 案例4:熟悉Elasticsearch的API调用
  5. 案例5:安装Kibana
  6. 案例6:导入日志并绘制图表

1 案例1:安装Elasticsearch

1.1 问题

本案例要求:

1.2 方案

1)ELK是日志分析平台,不是一款软件,而是一整套解决方案,是三个软件产品的首字母缩写,ELK分别代表:

Elasticsearch:负责日志检索和储存

Logstash:负责日志的收集和分析、处理

Kibana:负责日志的可视化

2) ELK组件在海量日志系统的运维中,可用于解决分布式日志数据集中式查询和管理系统监控等,故障排查,安全信息和事件管理,报表功能

部署Elasticsearch分布式集群安装,Kibana作为可视化平台,实时总结流量和数据的图表,Logstash用来收集处理日志

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:先准备一台虚拟机

1)更改主机名,配置IP,搭建第三方yum源

  1. # 在跳板机上把 elk 软件加入自定义 yum 仓库
  2. [root@ecs-proxy ~]# cp -a elk /var/ftp/localrepo/elk
  3. [root@ecs-proxy ~]# cd /var/ftp/localrepo/
  4. [root@ecs-proxy localrepo]# createrepo --update .

2)安装elasticsearch

  1. # 配置主机名解析
  2. [root@es-0001 ~]# vim /etc/hosts
  3. 192.168.1.41 es-0001
  4. [root@es-0001 ~]# yum makecache
  5. [root@es-0001 ~]# yum install -y java-1.8.0-openjdk elasticsearch
  6. [root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
  7. 55: network.host: 0.0.0.0
  8. [root@es-0001 ~]# systemctl enable --now elasticsearch
  9. [root@es-0001 ~]# curl http://192.168.1.41:9200/
  10. {
  11. "name" : "War Eagle",
  12. "cluster_name" : "elasticsearch",
  13. "version" : {
  14. "number" : "2.3.4",
  15. "build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f",
  16. "build_timestamp" : "2016-06-30T11:24:31Z",
  17. "build_snapshot" : false,
  18. "lucene_version" : "5.5.0"
  19. },
  20. "tagline" : "You Know, for Search"
  21. }

2 案例2:部署Elasticsearch集群

2.1 问题

本案例要求:

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:更改hosts文件,更改配置文件

1)更改hosts文件

  1. [root@es-0001 ~]# vim /etc/hosts
  2. 192.168.1.41 es-0001
  3. 192.168.1.42 es-0002
  4. 192.168.1.43 es-0003
  5. 192.168.1.44 es-0004
  6. 192.168.1.45 es-0005

2)更改配置文件

  1. [root@es-0001 ~]# yum install -y java-1.8.0-openjdk elasticsearch
  2. [root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
  3. 17: cluster.name: my-es
  4. 23: node.name: es-0001 # 本机主机名
  5. 55: network.host: 0.0.0.0
  6. 68: discovery.zen.ping.unicast.hosts: ["es-0001", "es-0002"]
  7. [root@es-0001 ~]# systemctl enable --now elasticsearch
  8. [root@es-0001 ~]# curl http://192.168.1.41:9200/_cluster/health?pretty
  9. {
  10. "cluster_name" : "my-es",
  11. "status" : "green",
  12. "timed_out" : false,
  13. "number_of_nodes" : 5,
  14. "number_of_data_nodes" : 5,
  15. ... ...
  16. }

3) 其他机器(1.42-1.45)一样操作,安装elasticsearch和java-1.8.0-openjdk,同步配置文件在步骤一已经安装了一台elasticsearch,这里只需再准备四台即可

4)访问测试,如图-1所示:

可以访问任意一台主机, 集群的节点都是5台,ES 集群验证:返回字段解析:

”status”: ”green“ 集群状态:绿色为正常、黄色表示有问题但不是很严重、红色表示严重故障

”number_of_nodes”: 5, 表示集群中节点的数量

图-1

3 案例3:安装Elasticsearch插件

3.1 问题

本案例要求:

实现此案例需要按照如下步骤进行。

步骤一:部署插件

安装 apache,并把 apache 和 es-0001 服务发布到互联网上

安装 apache

  1. [root@web ~]# yum install -y httpd
  2. [root@web ~]# tar zxf head.tar.gz
  3. [root@web ~]# mv elasticsearch-head /var/www/html/head
  4. [root@web ~]# systemctl enable --now httpd
  5. Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
  6. [root@web ~]#

2)授权访问head插件访问 es-0001,如图-2所示:

  1. [root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
  2. # 配置文件最后追加
  3. http.cors.enabled : true
  4. http.cors.allow-origin : "*"
  5. http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
  6. http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type,Content-Length
  7. [root@es-0001 ~]# systemctl restart elasticsearch.service

4 案例4:熟悉Elasticsearch的API调用

4.1 问题

本案例要求:

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建 tedu 索引使用 PUT 方式

  1. [root@es-0005 bin]# curl -XPUT -H "Content-Type: application/json" 'http://es-0001:9200/tedu' -d '{
  2. "settings":{
  3. "index":{
  4. "number_of_shards": 5,
  5. "number_of_replicas": 1
  6. }
  7. }
  8. }'
  9.  
  10. {"acknowledged":true}

步骤二:增加数据

  1. [root@es-0005 bin]# curl –XPUT -H "Content-Type: application/json" 'http://es-0001:9200/tedu/teacher/1' -d \
  2. > '{
  3. > "职业": "诗人",
  4. > "名字": "李白",
  5. > "称号": "诗仙",
  6. > "年代": "唐"
  7. > }'
  8. {"_index":"tedu","_type":"teacher","_id":"1","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true}

步骤三:查询数据

  1. [root@es-0005 bin]# curl -XGET http://es-0001:9200/tedu/teacher/1?pretty
  2. {
  3. "_index" : "tedu",
  4. "_type" : "teacher",
  5. "_id" : "1",
  6. "_version" : 1,
  7. "found" : true,
  8. "_source" : {
  9. "职业" : "诗人",
  10. "名字" : "李白",
  11. "称号" : "诗仙",
  12. "年代" : "唐"
  13. }
  14. }

步骤四:修改数据

  1. [root@es-0005 bin]# curl -XPOST -H "Content-Type: application/json" http://es-0001:9200/tedu/teacher/1/_update -d \
  2. > '{
  3. > "doc": {
  4. > "年代": "公元701"
  5. > }
  6. > }'
  7. {"_index":"tedu","_type":"teacher","_id":"1","_version":2,"_shards":{"total":2,"successful":2,"failed":0}}

步骤五:删除数据

注:删除时候可以是文档,也可以是索引,但不能是类型

  1. [root@es-0005 bin]# curl –XDELETE -H "Content-Type: application/json" http://es-0001:9200/tedu/teacher/1
  2. {"found":true,"_index":"tedu","_type":"teacher","_id":"1","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}
  3.  
  4. [root@es-0005 bin]# curl -XDELETE -H "Content-Type: application/json" http://es-0001:9200/tedu
  5. {"acknowledged":true}[root@es-0005 bin]#
  6.  
  7. [root@es-0005 bin]#

5 案例5:安装Kibana

5.1 问题

本案例要求批量导入数据:

5.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装kibana

1)在另一台主机,配置ip为192.168.1.46,配置yum源,更改主机名

2)安装kibana

  1. 更改hosts文件
  2. [root@kibana ~]# vim /etc/hosts
  3. 192.168.1.41 es-0001
  4. 192.168.1.42 es-0002
  5. 192.168.1.43 es-0003
  6. 192.168.1.44 es-0004
  7. 192.168.1.45 es-0005
  8. 192.168.1.46 kibana
  9. [root@kibana ~]# yum -y install kibana

3)更改配置文件

  1. [root@kibana ~]# yum install -y kibana
  2. [root@kibana ~]# vim /etc/kibana/kibana.yml
  3. 02 server.port: 5601
  4. 07 server.host: "0.0.0.0"
  5. 28 elasticsearch.hosts: ["http://es-0002:9200", "http://es-0003:9200"]
  6. 37 kibana.index: ".kibana"
  7. 40 kibana.defaultAppId: "home"
  8. 113 i18n.locale: "zh-CN"
  9. [root@kibana ~]# systemctl enable --now kibana

步骤二:web 页面访问,如图-5所示:

  1. Firefox http://192.168.1.46:5601

图-5

6 案例6:导入日志并绘制图表

6.1 问题

本案例要求批量导入数据:

6.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:导入数据

  1. [root@localhost ~]# scp /var/ftp/localrepo/elk/*.gz root@192.168.1.46:/root/
  2. [root@kibana ~]# gzip -d logs.jsonl.gz
  3. [root@kibana ~]# curl -XPOST -H "Content-Type: application/json" http://es-0001:9200/_bulk --data-binary @logs.jsonl

图-6

3)kibana导入数据,如图-7所示:

  1. firefox http://192.168.1.46:5601

3)成功创建会有logstash-*,如图-7所示:

图-7

注意: 这里没有数据的原因是导入日志的时间段不对,默认配置是最近15分钟,在这可以修改一下时间来显示

5)kibana修改时间,选择Lsat 15 miuntes

图-8

7)选择时间2015-5-15到2015-5-22,如图-12所示:

8)查看结果,如图-9所示:

图-9

9)除了柱状图,Kibana还支持很多种展示方式 ,如图-10所示:

图-10

10)做一个饼图,选择Pie chart,如图-11所示:

11)结果,如图-11所示:

图-11