设为首页 收藏本站
查看: 755|回复: 0

[经验分享] (实际应用)ELK环境搭建

[复制链接]

尚未签到

发表于 2019-1-28 11:19:54 | 显示全部楼层 |阅读模式
今天给大家带来的是开源实时日志分析 ELK , ELK 由 ElasticSearch 、 Logstash 和 Kiabana 三个开源工具组成。官方网站:https://www.elastic.co

其中的3个软件是:

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

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

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

系统系统需要安装的软件ip描述
centos6.5Elasticsearch/test5192.168.253.210搜索存储日志
centos6.5Elasticsearch/test4192.168.253.200搜索存储日志
centos6.5Logstash/nginx/test1192.168.253.150用来收集日志给上面
centos6.5Kibana/nginx/test2192.168.253.100用来后端的展示
架构原理图:


  

  一、先安装elasticsearch集群,并测试通过再进行其他软件安装。
  在test5,test4上安装分别安装elasticsearch-2.3.3.rpm  前提要安装java1.8 步骤如下:
  yum remove java-1.7.0-openjdk
  rpm -ivh jdk-8u51-linux-x64.rpm
  java -version
  yum localinstall elasticsearch-2.3.3.rpm -y
  service elasticsearch start
  cd /etc/elasticsearch/
  vim elasticsearch.yml


  

  修改如下配置
  cluster.name: myelk  #设置集群的名称,在一个集群里面都是这个名称,必须相同
  node.name: test5    #设置每一个节点的名,每个节点的名称必须不一样。
  path.data: /path/to/data    #指定数据的存放位置,线上的机器这个要放到单一的大分区里面。
  path.logs: /path/to/logs    #日志的目录
  bootstrap.mlockall: true  #启动最优内存配置,启动就分配了足够的内存,性能会好很多,测试我就不启动了。
  network.host: 0.0.0.0  #监听的ip地址,这个表示所有的地址。
  http.port: 9200      #监听的端口号
  discovery.zen.ping.unicast.hosts: ["hostip", "hostip"]  #知道集群的ip有那些,没有集群就会出现就一台工作
  mkdir -pv /path/to/{data,logs}
  chown elasticsearch.elasticsearch /path -R
  启动服务器  service elasticsearch start  并查看监控端口启动
  

  [root@test4 ~]# ss -tln
  State       Recv-Q Send-Q                         Local Address:Port                           Peer Address:Port
  LISTEN      0      128                                       :::54411                                                   :::*
  LISTEN      0      128                                       :::111                                                       :::*
  LISTEN      0      128                                        *:111                                                      *:*
  LISTEN      0      50                                            :::9200                                                     :::*
  LISTEN      0      50                                        :::9300                                                     :::*
  LISTEN      0      128                                       :::22                                                           :::*
  LISTEN      0      128                                        *:22                                                       *:*
  LISTEN      0      128                                        *:51574                                                 *:*
  LISTEN      0      128                                        127.0.0.1:631                                                *:*
  LISTEN      0      128                                          ::1:631                                                      :::*
  LISTEN      0      100                                          ::1:25                                                       :::*
  LISTEN      0      100                                    127.0.0.1:25                                                    *:*

  

  两台的配置都一样就是上面的IP和note名称要配置不一样就行


  

  [root@test5 ~]# ss -tln
  State       Recv-Q Send-Q                         Local Address:Port                           Peer Address:Port
  LISTEN      0      128                                        *:45822                                     *:*
  LISTEN      0      128                                       :::39620                                    :::*
  LISTEN      0      128                                       :::111                                      :::*
  LISTEN      0      128                                        *:111                                       *:*
  LISTEN      0      50                                        :::9200                                     :::*
  LISTEN      0      50                                        :::9300                                     :::*
  LISTEN      0      128                                       :::22                                       :::*
  LISTEN      0      128                                        *:22                                        *:*
  LISTEN      0      128                                127.0.0.1:631                                       *:*
  LISTEN      0      128                                      ::1:631                                      :::*
  LISTEN      0      100                                      ::1:25                                       :::*
  LISTEN      0      100                                127.0.0.1:25                                        *:*
  安装插件 head和kopf 之后访问 ip:9200/_plugin/head  和ip:9200/_plugin/kopf  (插件可以图形查看elasticsearch的状态和删除创建索引)
  /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
  /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
  [root@test5]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
  -> Installing lmenezes/elasticsearch-kopf...
  Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip ...
  Downloading ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
  Verifying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip checksums if available ...
  NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
  Installed kopf into /usr/share/elasticsearch/plugins/kopf
  [root@test5 ]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
  -> Installing mobz/elasticsearch-head...
  Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
  Downloading ........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
  Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
  NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
  Installed head into /usr/share/elasticsearch/plugins/head


  

  二、安装nginx和logstash软件
  yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
  nginx-1.8.1-1.el6.ngx.x86_64.rpm
  logstash-2.3.3-1.noarch.rpm
  jdk-8u51-linux-x64.rpm
  

  在test1上安装好nginx服务 就是收集它的日志呢
  日志在/var/log/nginx/access.log
  然后在test1上安装logstash-2.3.3-1.noarch.rpm
  yum remove java-1.7.0-openjdk
  rpm -ivh jdk-8u91-linux-x64.rpm
  rpm -ivh logstash-2.3.3-1.noarch.rpm
  /etc/init.d/logstash start  #启动服务
  

  /opt/logstash/bin/logstash -e "input {stdin{}} output{stdout{ codec=>"rubydebug"}}"  #检测环境  执行这个命令检测环境正常否,启动完成后 直接输入东西就会出现
  Settings: Default pipeline workers: 1
  Pipeline main started
  hello world
  {
  "message" => "hello world",
  "@version" => "1",
  "@timestamp" => "2017-05-24T08:04:46.993Z",
  "host" => "0.0.0.0"
  }
  之后输入/opt/logstash/bin/logstash -e 'input {stdin{}} output{ elasticsearch { hosts => ["192.168.253.200:9200"] index => "test"}}'
  就是输入东西到253.200的elasticsearch上 会在/path/to/data/myelk/nodes/0/indices 生成你名称test索引文件目录 可以多输入几个到253.200的目录看看有没有文件有就证明正常。
  

  [root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/
  test
  之后在test1的/etc/logstash/conf.d 建立以.conf结尾的配置文件,我收集nginx就叫nginx.conf了内容如下
  

  [root@test1 nginx]# cd /etc/logstash/conf.d/
  [root@test1 conf.d]# ls
  nginx.conf
  [root@test1 conf.d]# cat nginx.conf
  input {
  file {
  type => "accesslog"
  path => "/var/log/nginx/access.log"
  start_position => "beginning"
  }
  }
  output {
  if [type] == "accesslog" {
  elasticsearch  {
  hosts => ["192.168.253.200"]
  index => "nginx-access-%{+YYYY.MM.dd}"
  }
  }
  }
  /etc/init.d/logstash configtest
  ps -ef |grep java
  /opt/logstash/bin/logstash -f nginx.conf
  之后在elasticearch查看有没有索引生成。多访问下nginx服务
  如果没有就修改这个文件
  vi /etc/init.d/logstash
  LS_USER=root    ###把这里换成root或者把访问的日志加个权限可以让logstash可以读取它 重启服务就会生成索引了
  LS_GROUP=root
  LS_HOME=/var/lib/logstash
  LS_HEAP_SIZE="1g"
  LS_LOG_DIR=/var/log/logstash
  LS_LOG_FILE="${LS_LOG_DIR}/$name.log"
  LS_CONF_DIR=/etc/logstash/conf.d
  LS_OPEN_FILES=16384
  LS_NICE=19
  KILL_ON_STOP_TIMEOUT=${KILL_ON_STOP_TIMEOUT-0} #default value is zero to this variable but could be updated by user request
  LS_OPTS=""
  test4查看:
  [root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/
  nginx-access-2017.05.23  test
  [root@test1 logstash]# cat logstash.log
  {:timestamp=>"2017-05-24T16:05:19.659000+0800", :message=>"Pipeline main started"}
  

  三、安装kibana软件
  上面的都安装完成后在test2上面安装kibana
  rpm -ivh kibana-4.5.1-1.x86_64.rpm
  编辑配置文件在这里/opt/kibana/config/kibana.yml 就修改下面几项就行
  server.port: 5601  端口
  server.host: "0.0.0.0"  监听
  elasticsearch.url: "http://192.168.48.200:9200" elasticsearch地址

  

  /etc/init.d/kibana start 启动服务
  访问kibana  http://ip:5601

  

  添加展示的索引,就是在上面定义的  nginx-access-2016.07.03




  配置kibana上面的收集Nginx日志的logstash
  在kibana那台服务器上面安装logstash(按照之前的步骤安装)
  然后再logstash的/etc/logstash/conf.d/下面
  写一个配置文件:
  [root@test2 conf.d]# vim nginx.conf
  input {
  file {
  type => "accesslog"
  path => "/var/log/nginx/access.log"
  start_position => "beginning"
  }
  }
  output {
  if [type] == "accesslog" {
  elasticsearch  {
  hosts => ["192.168.253.200"]
  index => "nginx-access-%{+MM.dd.YYYY}"
  }
  }
  }
  /opt/logstash/bin/logstash -f nginx.conf
  查看Elasticsearch中多出了一个以月-日-年的Nginx访问日志索引
  [root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/
  nginx-access-05.23.2017  nginx-access-2017.05.23  test
  然后在kibana,浏览器上面按照之前的创建,生成一个新的日志文件

  

  四、其他的一些配置。
  kibana是直接访问的比较不安全,我们需要用nginx访问代理,并设置权限用户名和密码访问
  先在kibana服务器上安装nginx 不介绍了
  在nginx里面配置
  #############################################################################
  server
  {
  listen            80;
  server_name    localhost;
  auth_basic "Restricted Access";
  auth_basic_user_file /usr/local/nginx/conf/htpasswd.users;  #密码和用户
  location / {
  proxy_pass    http://localhost:5601;  #代理kibana的5601之后就可以直接80访问了
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header REMOTE-HOST $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  }
  #############################################################################
  创建密码和用户文件:htpasswd.users
  需要安装httpd-tool包先安装它
  htpasswd -bc /usr/local/nginx/conf/htpasswd.users admin paswdadmin  #前面是用户后面是密码

  之后通过访问需要密码和用户并且是80端口了






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668647-1-1.html 上篇帖子: ELK安装配置之集群相关介绍及搭建 下篇帖子: elk入门概念详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表