运维之道 | Elasticsearch 安装部署

在这里插入图片描述
Filebeat+ELK是四个开源工具组成,简单解释如下:

Filebeat:ELK 协议栈的新成员,一个轻量级开源日志文件数据搜集器,基于 Logstash-Forwarder 源代码开发,是对它的替代。在需要采集日志数据的 server 上安装 Filebeat,并指定日志目录或日志文件后,Filebeat就能读取数据,迅速发送到 Logstash 进行解析,亦或直接发送到 Elasticsearch 进行集中式存储和分析。

Elasticsearch:是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

Logstash:是一个完全开源的工具,它可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。

Kibana:也是一个开源和免费的工具,它可以为 Logstash 和 ElasticSearch 提供的日志分析友好的Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

这里之所以用Filebeat+ELK是因为Filebeat相对于logstash而言,更轻量级,占用系统资源少。


一、配置Java环境

安装elasticsearch的时候需要有Java环境,jdk可以去oracle官方网站下载,也可以使用下面安装包

jdk-8u241-linux-x64.tar.gz - 密码:qgg8
1、对JAVA安装包进行解压,并移动至usr/ 目录下
[root@localhost java]# tar -zxvf jdk-8u41-linux-x64.tar.gz
[root@localhost java]# mkdir -p /usr/java/
[root@localhost java]# mv jdk1.8.0_231 /usr/java/
2、修改环境配置
[root@localhost java]# vim /etc/profile

JAVA_HOME=/usr/java/jdk1.8.0_241
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAME_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
3、重载profile配置文件
[root@localhost ~]# source /etc/profile    ///重启profile
[root@localhost ~]# javac				   ///测试java环境是否配置成功
用法: javac <options> <source files>
其中, 可能的选项包括:
  -g                         生成所有调试信息
  -g:none                    不生成任何调试信息
  -g:{lines,vars,source}     只生成某些调试信息
  -nowarn                    不生成任何警告
  -verbose                   输出有关编译器正在执行的操作的消息
  -deprecation               输出使用已过时的 API 的源位置
  ......   配置成功!!!协议,转载请附上原文出处链接及本声明。

二、Elasticsearch 安装

1、解压Elasticsearch 安装包
[root@localhost ~]# tar -xzvf elasticsearch-6.3.0.tar.gz
2、修改Elasticsearch配置文件
[root@localhost config]# egrep -v "#|^$" elasticsearch.yml 

bootstrap.memory_lock: yes
bootstrap.system_call_filter: yes
network.host: 192.168.182.11
http.port: 9200
3、添加elastic用户,tar包启动必须使用普通用户运行
[root@localhost ~]# useradd elastic
[root@localhost ~]# chown -R elastic. /usr/local/src/elasticsearch-6.3.0
4、 修改sysctl.conf文件,添加如下内容:
[root@localhost ~]# vim /etc/sysctl.conf
 vm.max_map_count = 655360
 
[root@localhost ~]# sysctl -p /etc/sysctl.conf 		///重载配置文件
5、修改/etc/security/limits.conf文件,修改打开文件句柄数
[root@localhost ~]# vim /etc/security/limits.conf  --添加如下内容
 * soft nofile 65536
 * hard nofile 65536
 * soft nproc 65536
 * hard nproc 65536
6、切换到elastic普通用户启动Elasticsearch
[root@localhost ~]# su - elastic
[elk@localhost ~]$ cd /usr/local/src/elasticsearch-6.3.0/bin/
[elk@localhost bin]$ ./elasticsearch

在这里插入图片描述

7、查看进程

在这里插入图片描述

8、curl简单的测试
[elk@localhost bin]$ curl 192.168.182.11:9200
{
  "name" : "B6awqaD",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "6KOkBvARTmqXV2S3eScceQ",
  "version" : {
    "number" : "6.3.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "424e937",
    "build_date" : "2018-06-11T23:38:03.357887Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}


三、Filebeat 安装部署

1、解压Filebeat 安装包
[root@localhost ~]# tar -xzvf filebeat-6.3.0-linux-x86_64.tar.gz
2、编辑filebeat.yml文件,内容如下:
[root@localhost filebeat-6.3.0-linux-x86_64]# egrep -v "#|^$" filebeat.yml
filebeat.inputs:
- type: log
  enabled: false
  paths:
    - /var/log/messages
  fields:
     service_name: dxm-beta
     log_type: log
     service_id: 127.0.0.1
  scan_frequency: 60
  multiline.pattern: ^\{4}
  multiline.negate: true
  multiline.match: after
     
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
output.elasticsearch:
  hosts: ["192.168.182.11:9200"]
3、启动filebeat服务
[root@localhost ~]# cd /usr/local/src/filebeat-6.3.0-linux-x86_64/
[root@localhost filebeat-6.3.0-linux-x86_64]# ./filebeat &

注意:filebeat没有监听端口,主要看日志和进程,filebeat监听的文件记录信息在/usr/local/src/filebeat-6.3.0/data/registry里面

[root@localhost filebeat-6.3.0-linux-x86_64]# ps -ef | grep filebeat
root      10803  10172  0 11:42 pts/2    00:00:00 ./filebeat
root      10810  10172  0 11:43 pts/2    00:00:00 grep --color=auto filebeat


四、Logstash 安装

1、安装logstash
[root@localhost ~]# tar -xzvf logstash-6.3.0.tar.gz
2、测试标准输入输出
[root@localhost ~]# /usr/local/src/logstash-6.3.0/bin/logstash -e 'input { stdin {} } output { stdout { codec => rubydebug} }'
hello world  ########### 输入

在这里插入图片描述

3、测试输出到文件
  • 新建一个logstash启动指定logstash.conf的配置文件,内容如下:
[root@localhost config]# cat logstash.conf 
input {
        beats {
                port => "5044"
        }
}

output {
  stdout {
        codec => rubydebug
}
#输出到elasticsearch,提供给kibana进行搜索
elasticsearch {
        hosts => [ "192.168.182.11:9200" ]
        index => "%{[fileds][service_name]}-%{+YYYY.MM.dd}"
                #在es中存储的索引格式,按照"服务名-日期"进行索引  
        }
}
  • 指定配置文件logstash.conf进行测试:
[root@localhost logstash-6.3.0]# ./bin/logstash -f config/logstash.conf

在这里插入图片描述
这个启动完一会后会输出如下数据,这些数据就是filebeat从access.log里采集过来的日志,其实这些数据也输入到了elasticsearch当中,可以用curl http://ip:9200/_search?pretty,进行验证(ip为elasticsearch的ip)

[root@localhost config]# curl http://192.168.182.11:9200/_search?pretty
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : 0.0,
    "hits" : [ ]
  }
}
  • 这时候我们也可以看到对应的端口9600和5044是开启的

在这里插入图片描述



五、elasticsearch-head 插件安装

通过head插件来查看集群信息

[root@localhost src]# tar xvzf elasticsearch-head.tar.gz
[root@localhost src]# cd elasticsearch-head/
[root@localhost elasticsearch-head]# npm run start &
  • 修改elasticsearch服务配置文件,开启跨域访问支持,然后重启elasticsearch服务
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml

http.cors.enabled: true     #最下方添加
http.cors.allow-origin: "*"

PS:否则elasticsearch无法连接

测试

9、浏览器访问9100端口,将连接地址修改为elasticsearch地址9200
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章