ELK基础搭建

一、认识ELK

ELK是一套开源的日志分析系统,由elasticsearch+logstash+Kibana组成。

官网说明:https://www.elastic.co/cn/products

首先: 先一句话简单了解 E,L,K 这三个软件

elasticsearch: 分布式搜索引擎

logstash: 日志收集与过滤,输出给elasticsearch

Kibana: 图形化展示

部署环境

192.168.100.115 vm1.example.com vm1

192.168.100.116 vm2.example.com vm2

192.168.100.114 vm3.example.com vm3

192.168.100.117 vm4.example.com vm4

vm1与vm2搭建elasticsearch集群,vm3部署logstash,vm4部署kibana

均使用Xshell进行远程连接

1、配置/etc/hosts文件

python 复制代码
[root@vm1 ~]# vim /etc/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.115  vm1.example.com  vm1   
192.168.100.116  vm2.example.com  vm2
192.168.100.114  vm3.example.com  vm3


[root@vm1 ~]# scp /etc/hosts root@vm2:/etc/hosts
[root@vm1 ~]# scp /etc/hosts root@vm3:/etc/hosts

2、查看四台主机的firewalld与selinux是否关闭

python 复制代码
[root@vm1 ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)
[root@vm1 ~]# getenforce
Disabled

3、安装工具包

四台主机都安装

python 复制代码
yum -y install chrony lrzsz tar net-tools wget

4、时钟同步

四台主机都需要以下操作

python 复制代码
systemctl restart chronyd
systemctl enable chronyd
hwclock -w
timedatectl
//验证System clock synchronized: yes这一行信息是否为yes

二、elasticsearch单实例部署

1、安装java

在vm1和mv2上安装Java-1.8.0

python 复制代码
yum -y install java-1.8.0*

//查询java版本
[root@vm2 ~]# java -version
openjdk version "1.8.0_422"
OpenJDK Runtime Environment (build 1.8.0_422-b05)
OpenJDK 64-Bit Server VM (build 25.422-b05, mixed mode)

2、安装elasticsearch

python 复制代码
//没有这个包文件的可以通过这个下载,只是会比较慢
[root@vm2 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.2.rpm

//我这里有这个包文件,我就直接拖进来了
[root@vm2 ~]# rz -E
rz waiting to receive.
[root@vm2 ~]# ls
anaconda-ks.cfg  -e  elasticsearch-6.5.2.rpm  -i.bak
//使用rpm进行安装
[root@vm2 ~]# rpm -ivh elasticsearch-6.5.2.rpm

3、修改es配置文件并启动

python 复制代码
[root@vm2 ~]# vim /etc/elasticsearch/elasticsearch.yml

//修改第一个地方
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: elk-cluster
//找到这一行,去掉#号,更改集群名称

//修改第二个地方
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0   //这里去掉#号,将ip改为全0
#
# Set a custom port for HTTP:
#
http.port: 9200   //这里直接去掉#号

//启动es
[root@vm2 ~]# systemctl restart elasticsearch.service 
[root@vm2 ~]# systemctl enable elasticsearch.service 
//查看端口验证服务是否启动,有9200和9300就代表启动成功(启动需要十到二十秒左右)
[root@vm2 ~]# ss -anlt
State          Recv-Q         Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              4096                                 *:9300                              *:*                            
LISTEN         0              128                               [::]:22                             [::]:*                            
LISTEN         0              4096                                 *:9200                              *:*                            

//9200则是数据传输端口
//9300端口是集群通信端口(我们暂时还没有配置集群,现在是单点elasticsearch)

4、验证

命令验证

python 复制代码
[root@vm2 ~]#  curl http://192.168.100.116:9200/_cluster/health?pretty
{
  "cluster_name" : "elk-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "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
}

web界面验证

三、elasticsearch集群部署

1、elasticsearch基础概念

主要的基础概念有:

Node, Index,Type,Document,Field,shard和replicas.

Node(节点):运行单个ES实例的服务器

Cluster(集群):一个或多个节点构成集群

Index(索引):索引是多个文档的集合

Type(类型):一个Index可以定义一种或多种类型,将Document逻辑分组

Document(文档):Index里每条记录称为Document,若干文档构建一个Index

Field(字段):ES存储的最小单元

Shards(分片):ES将Index分为若干份,每一份就是一个分片

Replicas(副本):Index的一份或多份副本

为了便于理解,我们和mysql这种关系型数据库做一个对比:

ES是分布式搜索引擎,每个索引有一个或多个分片(shard),索引的数据被分配到各个分片上。你可以看作是一份数据分成了多份给不同的节点。

当ES集群增加或删除节点时,shard会在多个节点中均衡分配。默认是5个primary shard(主分片)和1个replica shard(副本,用于容错)。

2、集群部署主要注意以下几个方面

1. 集群配置参数

discovery.zen.ping.unicast.hosts,Elasticsearch默认使用Zen Discovery来做节点发现机

制,推荐使用unicast来做通信方式,在该配置项中列举出Master节点。

discovery.zen.minimum_master_nodes,该参数表示集群中Master节点可工作Master的最

小票数,默认值是1。为了提高集群的可用性,避免脑裂现象。官方推荐设置为(N/2)+1,其中

N是具有Master资格的节点的数量。

discovery.zen.ping_timeout,表示节点在发现过程中的等待时间,默认值是30秒,可以根据

自身网络环境进行调整,一定程度上提供可用性。

2. 集群节点:

节点类型主要包括Master节点和data节点(client节点和ingest节点不讨论)。通过设置两个配

置项node.master和node.data为true或false来决定将一个节点分配为什么类型的节点。

尽量将Master节点和Data节点分开,通常Data节点负载较重,需要考虑单独部署。

3. 内存

Elasticsearch默认设置的内存是1GB,对于任何一个业务部署来说,这个都太小了。通过指定

ES_HEAP_SIZE环境变量,可以修改其堆内存大小,服务进程在启动时候会读取这个变量,并

相应的设置堆的大小。建议设置系统内存的一半给Elasticsearch,但是不要超过32GB。

4. 硬盘空间

Elasticsearch默认将数据存储在/var/lib/elasticsearch路径下,随着数据的增长,一定会出现

硬盘空间不够用的情形,大环境建议把分布式存储挂载到/var/lib/elasticsearch目录下以方便

扩容。

配置参考文档:

https://www.elastic.co/guide/en/elasticsearch/reference/index.html

四、两台ES做集群的配置

先在vm1主机上做一遍elasticsearch单实例部署(同样的操作这里就不详细写了)

1、修改vm1与vm2的es配置文件

vm1:

python 复制代码
[root@vm1 ~]# vim /etc/elasticsearch/elasticsearch.yml 
[root@vm1 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v "#"
cluster.name: elk-cluster
node.name: 192.168.100.115   //本机IP
node.master: false             //这一行没有需要手动添加进来,放在node。name下一行就可以   
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.100.115", "192.168.100.116"] //集群所有节点IP

vm2:

python 复制代码
[root@vm2 ~]# vim /etc/elasticsearch/elasticsearch.yml 
[root@vm2 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v "#"
cluster.name: elk-cluster
node.name: 192.168.100.116   //本机IP
node.master: true      //这一行没有需要手动添加进来,放在node。name下一行就可以
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.100.115", "192.168.100.116"]   //集群所有节点IP

2、重启服务并验证

重启服务

python 复制代码
[root@vm1 ~]# systemctl restart elasticsearch.service 

[root@vm2 ~]# systemctl restart elasticsearch.service

命令验证

python 复制代码
[root@vm1 ~]# curl http://192.168.100.115:9200/_cluster/health?pretty
{
  "cluster_name" : "elk-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "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
}


[root@vm2 ~]#  curl http://192.168.100.116:9200/_cluster/health?pretty
{
  "cluster_name" : "elk-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "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
}

web界面验证

vm1

vm2

五、elaticsearch基础API操作

前面我们通过

http://10.1.1.12:9200/_cluster/health?pretty查看ES集群状态,其实就是它的一API操作。

什么是API?

API(Application Programming Interface)应用程序编程接口,就是无需访问程序源码或理解内部工作机制就能实现一些相关功能的接口。

RestFul API 格式

python 复制代码
curl -X<verb> '<protocol>://<host>:<port>/<path>?<query_string>'-d '<body>'

elasticseearch的API很多, 我们运维人员主要用到以下几个要介绍的较简单的API。

更多API参考: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/index.html

1、查看节点信息

通过curl或浏览器访问 http://10.1.1.12:9200/_cat/nodes?v(ip为ES节点IP,如果有ES集群,则为ES任意节点IP)
命令查看

python 复制代码
[root@vm2 ~]# curl http://192.168.100.116:9200/_cat/nodes?v
ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.100.116           12          94   0    0.00    0.01     0.04 mdi       *      192.168.100.116
192.168.100.115           13          94   0    0.00    0.01     0.04 di        -      192.168.100.115

web界面查看

2、查看索引信息

命令查看

python 复制代码
[root@vm2 ~]#  curl http://192.168.100.116:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

web界面查看

默认现在没有任何索引

3、新增索引

python 复制代码
[root@vm2 ~]# curl -X PUT http://192.168.100.116:9200/nginx_access_log
{"acknowledged":true,"shards_acknowledged":true,"index":"nginx_access_log"}

//命令查看索引
[root@vm2 ~]# curl  http://192.168.100.116:9200/_cat/indices?v
health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   nginx_access_log yZVV-DWsRQuBHQuhYKl9VA   5   1          0            0      2.2kb          1.1kb

web查看索引

4、删除索引

python 复制代码
[root@vm2 ~]# curl -X DELETE http://192.168.100.116:9200/nginx_access_log
{"acknowledged":true}

六、ES查询语句

ES提供一种可用于执行查询JSON式的语言,被称为Query DSL。

针对elasticsearch的操作,可以分为增、删、改、查四个动作。

查询匹配条件:

match_all

from,size

match

bool

range

查询应用案例:

导入数据源

使用官方提供的示例数据:

1,、下载并导入进elasticsearch

python 复制代码
[root@vm2 ~]# rz -E
rz waiting to receive.
[root@vm2 ~]# ls
accounts.json  anaconda-ks.cfg  -e  elasticsearch-6.5.2.rpm  -i.bak

//导入进elasticsearch
[root@vm2 ~]#  curl -H "Content-Type: application/json" -XPOST "192.168.100.116:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"

//查询确认
[root@vm2 ~]# curl "192.168.100.116:9200/_cat/indices?v"
health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   bank  t6y8rlHyQDOgbnRxq558mg   5   1       1000            0    957.5kb        474.7kb

2、 查询bank索引的数据(使用查询字符串进行查询)

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?q=*&sort=account_number:asc&pretty"

//说明:
默认结果为10条
_search  属于一类API,用于执行查询操作
q=* ES批量索引中的所有文档
sort=account_number:asc 表示根据account_number按升序对结果排序
pretty调整显示格式

3,、查询bank索引的数据 (使用json格式进行查询)

python 复制代码
[root@vm2 ~]#  curl -X GET "192.168.100.116:9200/bank/_search" -H 'Content-Type:application/json' -d'
> {
> "query": { "match_all": {} },
>  "sort": [
>  { "account_number": "asc" }
> ]
> }
> '

//注意: 最后为单引号

4、查询匹配动作及案例

1)、match_all

匹配所有文档。默认查询

示例:查询所有,默认只返回10个文档

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match_all" : {} }
> }
> '

// query告诉我们查询什么
// match_all是我们查询的类型
// match_all查询仅仅在指定的索引的所有文件进行搜索

2)、from,size

python 复制代码
//查询1条数据
[root@vm2 ~]#  curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match_all": {} },
> "size":1
> }
> '
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "25",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 25,
          "balance" : 40540,
          "firstname" : "Virginia",
          "lastname" : "Ayala",
          "age" : 39,
          "gender" : "F",
          "address" : "171 Putnam Avenue",
          "employer" : "Filodyne",
          "email" : "[email protected]",
          "city" : "Nicholson",
          "state" : "PA"
        }
      }
    ]
  }
}

3)、指定位置与查询条数

python 复制代码
[root@vm2 ~]#  curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match_all": {} },
>  "from": 0,
> "size": 2
> }
> '

//from 0表示从第1个开始
//size 指定查询的个数

示例: 查询account_number从第501条到510条的数据

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match_all": {} },
> "from": 500,
> "size": 10,
> "sort": [
>  { "account_number": "asc" }
> ]
> }
> '

5、匹配查询字段

返回_source字段中的片段字段

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
>  "query": { "match_all": {} },
>  "_source": ["account_number", "balance"]
> }
> '

1)、match

基本搜索查询,针对特定字段或字段集合进行搜索

查询编号为20的账户
python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match": { "account_number": 20 } }
> }
> '
返回地址有包含mill或lane的所有账户
python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
>  "query": { "match": { "address": "mill" } }
> }
> '
返回地址有包含mill或lane的所有账户
python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": { "match": { "address": "mill lane" } } //空格就是或的关系
> }
> '

2)、bool

bool must 查询的字段必须同时存在

查询包含mill和lane的所有账户

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
>  "query": {
>  "bool": {
>  "must": [
> { "match": { "address": "mill" } },
>  { "match": { "address": "lane" } }
> ]
> }
> }
> }
> '
bool should 查询的字段仅存在一即可

查询包含mill或lane的所有账户

python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query": {
> "bool": {
> "should": [
>  { "match": { "address": "mill" } },
>  { "match": { "address": "lane" } }
> ]
> }
> }
> }
> '

3)、range

指定区间内的数字或者时间

操作符:gt大于,gte大于等于,lt小于,lte小于等于

查询余额大于或等于20000且小于等于30000的账户
python 复制代码
[root@vm2 ~]# curl -X GET "192.168.100.116:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
>  "query": {
>  "bool": {
> "must": { "match_all": {} },
>  "filter": {
>  "range": {
>  "balance": {
>  "gte": 20000,
> "lte": 30000
> }
> }
> }
> }
> }
> }
> '

七、elasticsearch-head安装

elasticsearch-head介绍

elasticsearch-head是集群管理、数据可视化、增删改查、查询语句可视化工具。从ES5版本后安装方式和ES2以上的版本有很大的不同,在ES2中可以直接在bin目录下执行plugin install xxxx 来进行安装,但是在ES5中这种安装方式变了,要想在ES5中安装Elasticsearch Head必须要安装NodeJs,然后通过NodeJS来启Head。

官网地址: https://github.com/mobz/elasticsearch-hea

1、下载相关软件并上传到服务器

官网有安装说明,可以通过git安装,也可以下载zip包解压安装下载相应的软件包,并拷贝到ES集群的一个节点上(我这里拷贝到10.1.1.12这台,也就是vm2上)

nodejs下载页面: https://nodejs.org/en/download/

我自己已经下载好了,我就直接拖进去了

python 复制代码
[root@vm2 ~]# rz -E
rz waiting to receive.
[root@vm2 ~]# ls
accounts.json  anaconda-ks.cfg  -e  elasticsearch-6.5.2.rpm  -i.bak  node-v10.24.1-linux-x64.tar.xz

2、安装nodejs

python 复制代码
[root@vm2 ~]# tar -xf node-v10.24.1-linux-x64.tar.xz -C /usr/local/
[root@vm2 ~]# mv /usr/local/node-v10.24.1-linux-x64/ /usr/local/nodejs
[root@vm2 ~]# ls /usr/local/nodejs/bin/npm 
/usr/local/nodejs/bin/npm
[root@vm2 ~]# ln -s /usr/local/nodejs/bin/npm /bin/npm
[root@vm2 ~]# ln -s /usr/local/nodejs/bin/node /bin/node

3、安装es-head

方法一

python 复制代码
 [root@vm2 ~]# git clone git://github.com/mobz/elasticsearch-head.git
 [root@vm2 ~]# cd elasticsearch-head
//先使用npm安装grunt
 //npm(node package manager):node包管理工具,类似yum
 //Grunt是基于Node.js的项目构建工具
 
[root@vm2 elasticsearch-head]# npm install -g grunt-cli
//安装时间较久,还会在网上下载phantomjs包

[root@vm2 elasticsearch-head]# npm install

安装可能有很多错误,我这里出现了下面的错误(重点是注意红色的ERR!,黄色的WARN不用管)

解决方法
python 复制代码
[root@vm2 elasticsearch-head]# npm install [email protected] --ignore-script
此命令执行后不用再返回去执行npm install了,直接开始启动
[root@vm2 elasticsearch-head]# nohup npm run start &

方法二

(网速特别慢导致安装时间过长的话可以尝试以下方法)

python 复制代码
git clone慢的话就使用下载好的zip压缩包解压安装
[root@vm2 ~]# rz -E
rz waiting to receive.
[root@vm2 ~]# ls
accounts.json    -e                       elasticsearch-head-master.zip  node-v10.24.1-linux-x64.tar.xz
anaconda-ks.cfg  elasticsearch-6.5.2.rpm  -i.bak
[root@vm2 ~]# yum -y install unzip
[root@vm2 ~]# unzip elasticsearch-head-master.zip -d /usr/local/
[root@vm2 ~]# mv /usr/local/elasticsearch-head-master/ /usr/local/es-head/
[root@vm2 ~]# cd /usr/local/es-head/

[root@vm2 es-head]# npm install --registry=http://registry.npm.taobao.org
[root@vm2 es-head]# npm install -g grunt-cli --registry=http://registry.npm.taobao.org
//出现报错后执行下面一条命令
[root@vm2 es-head]# npm install [email protected] --ignore-script --registry=http://registry.npm.taobao.org
//然后回过头再装一遍
[root@vm2 es-head]# npm install --registry=http://registry.npm.taobao.org

//启动
//运行nohup npm run start &必须要先cd到es-head的目录//
[root@vm2 es-head]# nohup npm run start &
//查看端口验证,9100端口启动,服务启动成功
[root@vm2 es-head]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*                            
LISTEN         0              511                          0.0.0.0:9100                      0.0.0.0:*                            
LISTEN         0              128                             [::]:22                           [::]:*                            
LISTEN         0              4096                               *:9300                            *:*                            
LISTEN         0              4096                               *:9200                            *:*                           

4、浏览器访问

浏览器访问 http://es-head节点IP:9100

//此时连接不上

5、修改ES集群配置文件,并重启服务

vm1 ES配置文件

python 复制代码
[root@vm1 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v "#"
 cluster.name: elk-cluster
 node.name: 10.1.1.11
 node.master: false
 path.data: /var/lib/elasticsearch
 path.logs: /var/log/elasticsearch
 network.host: 0.0.0.0
 http.port: 9200
 discovery.zen.ping.unicast.hosts: ["10.1.1.11", "10.1.1.12"]
 //在这一行下面加上以下两行信息
 http.cors.enabled: true
 http.cors.allow-origin: "*"                 

//重启es服务,查看端口验证
[root@vm1 ~]# systemctl restart elasticsearch.service 
[root@vm1 ~]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*                            
LISTEN         0              128                             [::]:22                           [::]:*                            
LISTEN         0              4096                               *:9300                            *:*                            
LISTEN         0              4096                               *:9200                            *:*        

vm2 es配置文件

python 复制代码
[root@vm2 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v "#"
 cluster.name: elk-cluster
 node.name: 10.1.1.11
 node.master: false
 path.data: /var/lib/elasticsearch
 path.logs: /var/log/elasticsearch
 network.host: 0.0.0.0
 http.port: 9200
 discovery.zen.ping.unicast.hosts: ["10.1.1.11", "10.1.1.12"]
 //在这一行下面加上以下两行信息
 http.cors.enabled: true
 http.cors.allow-origin: "*"    
 
 //重启es服务,查看端口验证
 [root@vm2 ~]# systemctl restart elasticsearch.service 
[root@vm2 ~]# ss -nalt
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*                            
LISTEN         0              511                          0.0.0.0:9100                      0.0.0.0:*                            
LISTEN         0              128                             [::]:22                           [::]:*                            
LISTEN         0              4096                               *:9300                            *:*                            
LISTEN         0              4096                               *:9200                            *:*        

6、再次连接就可以看到信息了

删除索引

新建索引

7、es-head查询验证




八、logstash部署

logstash简介

logstash是一个开源的数据采集工具,通过数据源采集数据.然后进行过滤,并自定义格式输出到目的地。

数据分为:

  1. 结构化数据 如:mysql数据库里的表等
  2. 半结构化数据 如: xml,yaml,json等
  3. 非结构化数据 如:文档,图片,音频,视频等

logstash可以采集任何格式的数据,当然我们这里主要是讨论采集系统日志,服务日志等日志类型数据。

1、安装logstash

在logstash服务器上确认openjdk安装

python 复制代码
[root@vm3 ~]# yum -y install java-1.8.0*
[root@vm3 ~]# java -version
openjdk version "1.8.0_422"
OpenJDK Runtime Environment (build 1.8.0_422-b05)
OpenJDK 64-Bit Server VM (build 25.422-b05, mixed mode)

没有包文件通过以下命令下载

python 复制代码
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.2.rpm
python 复制代码
//有包文件可以直接拖进来
[root@vm3 ~]# rz -E
rz waiting to receive.
[root@vm3 ~]# ls
anaconda-ks.cfg  logstash-6.5.2.rpm

//使用rpm安装
[root@vm3 ~]# rpm -ivh logstash-6.5.2.rpm 

2、配置logstash主配置文件并启动测试

python 复制代码
[root@vm3 ~]# cat /etc/logstash/logstash.yml |grep -v '#' |grep -v '^$'
 path.data: /var/lib/logstash
 path.config: /etc/logstash/conf.d/                  
打开注释,并加上配置目录路径
http.host: "10.1.1.13"  打开注释,并改为本机IP
path.logs: /var/log/logstash

[root@vm3 bin]# ./logstash -e 'input {stdin {} } output { stdout {} }'

测试1

关闭启动

测试能启动成功后,ctrl+c取消,则关闭了

测试2

python 复制代码
[root@vm3 bin]# vim /etc/logstash/conf.d/test.conf
input {  
         stdin { 
        }
}
filter {
}
output {
        stdout {
                codec => rubydebug
        }
}

[root@vm3 bin]# ./logstash --path.settings /etc/logstash -f /etc/logstash/conf.d/test.conf -t
 ......
 Config Validation Result: OK. Exiting Logstash


--path.settings 指定logstash主配置文件目录-f 指定片段配置文件-t 测试配置文件是否正确
codec => rubydebug这句可写可不定,默认就是这种输出方式

3、日志采集

这里以/var/log/messages为例,只定义input输入和output输出,不考虑过滤

python 复制代码
[root@vm3 bin]# vim /etc/logstash/conf.d/test.conf
input {
        file {
                path => "/val/log/messages"
                start_position => "beginning"
        }
}

output {
        elasticsearch{
                hosts => ["192.168.100.116:9200"]
                index => "test-%{+YYYY.MM.dd}"
        }
}

启动验证

python 复制代码
[root@vm3 bin]# ./logstash --path.settings /etc/logstash/ -r -f /etc/logstash/conf.d/test.conf  &

//后台运行如果要杀掉,请使用pkill java或ps查看PID再kill -9清除

查看信息

九、kibana部署

Kibana是一个开源的可视化平台,可以为ElasticSearch集群的管理提供友好的Web界面,帮助汇总,分析和搜索重要的日志数据。

1、安装kibana

python 复制代码
[root@vm1 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.2-x86_64.rpm
//我这里有包文件,就直接拖进来了
[root@vm4 ~]# rz -E
rz waiting to receive.
[root@vm4 ~]# ls
anaconda-ks.cfg  -e  -i.bak  kibana-6.5.2-x86_64.rpm

[root@vm4 ~]# rpm -ivh kibana-6.5.2-x86_64.rpm

2、配置kibana并启动

python 复制代码
 [root@vm1 ~]# cat /etc/kibana/kibana.yml |grep -v '#' |grep -v '^$'
 server.port: 5601                    //端口           
server.host: "0.0.0.0"                //监听所有,允许所有人能访问           
elasticsearch.url: "http://192.168.100.116:9200"     //ES集群的路径 
logging.dest: /var/log/kibana.log               
//我这里加了kibana日志,方便排错与调试

//日志要自己建立,并修改owner和group属性
[root@vm4 ~]# touch /var/log/kibana.log
[root@vm4 ~]# chown kibana.kibana /var/log/kibana.log 
[root@vm4 ~]# ll /var/log/kibana.log 
-rw-r--r-- 1 kibana kibana 0 Aug 20 17:55 /var/log/kibana.log

启动

python 复制代码
[root@vm4 ~]# systemctl restart kibana.service 
[root@vm4 ~]# systemctl enable kibana.service 

//验证
[root@vm4 ~]# ps -ef | grep kibana
kibana      1268       1 13 17:57 ?        00:00:16 /usr/share/kibana/bin/../node/bin/node --no-warnings /usr/share/kibana/bin/../src/cli -c /etc/kibana/kibana.yml
root        1313    1131  0 17:59 pts/0    00:00:00 grep --color=auto kibana
[root@vm4 ~]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                 Peer Address:Port         Process         
LISTEN         0              511                          0.0.0.0:5601                      0.0.0.0:*                            
LISTEN         0              128                          0.0.0.0:22                        0.0.0.0:*                            
LISTEN         0              128                             [::]:22                           [::]:*                 

//这里unzip一起装了,后面用的上
[root@vm4 ~]# yum -y install lsof unzip
[root@vm4 ~]# lsof -i:5601
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    1268 kibana   11u  IPv4  26209      0t0  TCP *:esmagent (LISTEN)

访问通过浏览器访问 http:// kibana服务器IP :5601

3、kibana汉化

我这里找了挺久的,只找到一个繁体的汉化包,将就着用

python 复制代码
[root@vm4 ~]# rz -E
rz waiting to receive.
[root@vm4 ~]# ls
anaconda-ks.cfg  -e  -i.bak  kibana-6.5.2-x86_64.rpm  kibana-6.5.4_hanization-master.zip

[root@vm4 ~]# unzip kibana-6.5.4_hanization-master.zip -d /usr/local/
[root@vm4 ~]# cd /usr/local/kibana-6.5.4_hanization-master/
[root@vm4 kibana-6.5.4_hanization-master]# python main.py /usr/share/kibana/
[root@vm4 kibana-6.5.4_hanization-master]# systemctl stop kibana.service 
[root@vm4 kibana-6.5.4_hanization-master]# systemctl restart kibana.service 

这就好了,将web界面刷新以下

4、通过kibana查看集群信息

打开系统监控

加载完成后就是这样

5、通过kibana查看logstash收集的日志索引



6、创建图表



相关推荐
爱吃龙利鱼2 天前
elk中kibana一直处于可用和降级之间且es群集状态并没有问题的解决方法
大数据·elk·elasticsearch
yuzhangfeng7 天前
【日志体系】ELK Stack与云原生日志服务
elk·云原生·云计算
小黑蛋学java14 天前
Elasticsearch 官网阅读学习笔记01
elk·学习笔记
码农不屈的一生16 天前
docker部署elk
运维·elk
MarkHD17 天前
第十八天 - ELK日志体系集成 - 自定义Logstash插件 - 练习:分布式日志分析平台
分布式·elk
小黑蛋学java17 天前
Elasticsearch 学习规划
elk
小黑蛋学java19 天前
Elasticsearch中的基本全文搜索和过滤
elk
小黑蛋学java20 天前
Elasticsearch单节点安装手册
elk
小黑蛋学java20 天前
Elasticearch数据流向
elk
butiehua020222 天前
PHP 项目搭建 ELK 日志监控体系完整指南
elk