跳至主要內容

安装ELK

soulballad环境配置CentOSCentOS约 1186 字大约 4 分钟

安装ELK

ELK是一个成熟的日志系统,主要功能有收集、分析、检索,详细见 elastic官网open in new window

本文主要介绍如何在CentOS7下安装最新版本的ELK,当然现在dockeropen in new window已经有完全配置成功的elk容器open in new window,安装配置非常方便,但是如果你想自己从安装jdk开始,那这篇博客将能够帮助到你。 安装前了解一下

测试环境:

centos7 2G 2核;(内存不能再低了) 
hostname:localhost
ip:192.168.1.2

域名:
#绑定hosts
192.168.1.2 elk-stack.co

操作系统及各组件版本

centos-7-x86_64
java8
elasticsearch-6.2.4
kibana-6.2.4
logstash-6.2.4

禁用SELinux

[root@localhost ~]# vim /etc/sysconfig/selinuxSELINUX=enforcing 改为 SELINUX=disabled

安装java

# rpm -ivh jdk-8u191-linux-x64.rpm
查看java是否安装成功

# java -version
java version "1.8.0_191"

安装 elasticsearch

  1. 导入秘钥

    rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
    
  2. 下载elasticsearch

    # wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm
    

    elasticsearch,kibana,logstash的最新下载地址都在这里,当然如果你愿意使用elk6.2.4并且这个的链接可以使用,那么就不需要到官网找下载链接了

  3. 安装 elasticsearch

    # rpm -ivh elasticsearch-6.2.4.rpm
    
  4. 配置 elasticsearch

    # vim /etc/elasticsearch/elasticsearch.yml
    取消43、59行的注释,行号不一定准确,但一定是下面这几项
    
    bootstrap.memory_lock: true
    http.port: 9200
    
  5. 启动elasticsearch

    # systemctl daemon-reload
    # systemctl enable elasticsearch
    # systemctl start elasticsearch
    # netstat -plntu |grep 9200
    tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      1850/java           
    tcp6       0      0 ::1:9200                :::*                    LISTEN      1850/java  
    
    最后通过 netstat -plntu 查看是否elasticsearch进程是否启动成功,如果有9200的端口这一行输出了那就代表启动成功,如果没有可能需要重启下虚拟机。
    

安装nginx

  1. 安装nginx

    # yum install epel-release -y
    # yum install nginx httpd-tools -y
    

    安装Nginx之前要先安装epel源,可能以后在使用linux的时候你还会添加各种源,就和在nuget添加源的目的一样配置

  2. 删除默认配置

    # vim /etc/nginx/nginx.conf
    
    这里把位于36、59行之间的Server块删掉,更多的配置项在 /etc/nginx/conf.d/*.conf中
    
    36 include /etc/nginx/conf.d/*.conf;
    Server { }
    59# Settings for a TLS enabled server.
    
  3. 给kibana添加代理

    粘贴下面内容,注意粘贴之前先输入 i 进入insert模式,这里域名open in new windowelk-stack.coopen in new window,然后使用http协议的basic认证,密码稍后添加,然后看location块,当Nginx监听到域名为elk-stack.coopen in new window,端口为80的请求时,就会转发给本地监听端口5601的进程,这个进程就是kibana,下一步安装它

    # vim /etc/nginx/conf.d/kibana.conf
    粘贴一下内容
    server {
    listen 80;
    
    server_name elk-stack.co;
    
    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/.kibana-user;
    
    location / {
        proxy_pass http://localhost:5601;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        } 
    }
    
  4. 给elasticsearch添加代理

    [root@localhost ~]# vim /etc/nginx/conf.d/elasticsearch.conf
    
    server {
        listen 81;
    
        server_name elk-stack.co;
    
        location / {
            proxy_pass http://localhost:9200;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        } 
    }
    
  5. 添加一个basic认证

    [root@localhost ~]# htpasswd -c /etc/nginx/.kibana-user admin
    

    然后输入你的密码,记住最后要通过这个来登录kibana的 测试Nginx配置并启动

    # nginx -t
    

    如果出现 test failed,就回过头检查/etc/nginx/conf.d/kibana.conf和/etc/nginx/nginx.conf这两个文件,肯定是不小心弄错了。 如果输出 test is successful,那么就可以启动 nginx 了 5.启动nginx并且加入开机启动

    # systemctl enable nginx
    # systemctl start nginx
    

安装kibana

  1. 下载kibana

    # wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-x86_64.rpm
    
  2. 安装kibana

    # rpm -ivh kibana-6.2.4-x86_64.rpm
    
  3. 配置kibana

    # vim /etc/kibana/kibana.yml
    
    取消2、7、21行的注释,行号不一定准确,但一定是下面这几项
    
    server.port: 5601
    server.host: "localhost"
    elasticsearch.url: "http://localhost:9200"
    
  4. 启动kibana

    # systemctl enable kibana
    # systemctl start kibana
    # netstat -tunpl|grep 5601
    

和elasticsearch一样,最后通过netstat -plntu查看kibana是否启动成功,如果有端口号为5601的输出那就代表kibana启动成功了

安装 logstash

  1. 下载 logstash

    # wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.rpm
    
  2. 安装logstash

    # rpm -ivh logstash-6.2.4.rpm
    
  3. 启动 logstash

    # systemctl enable logstash
    # systemctl start logstash
    

浏览器访问kibana

配置本地host

如果你的电脑是win10,并且安装目录在C盘,编辑文件C:\Windows\System32\drivers\etc\hosts添加这一行

192.168.1.2 elk-stack.coopen in new window 打开端口

# firewall-cmd --zone=public --add-port=80/tcp --add-port=81/tcp --permanent
# firewall-cmd --reload
或者
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 81 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart

现在就可以本地通过浏览输入elk-stack.co,访问kibana了! 但是如果出现 lookup elk-stack.co on 127.0.0.1:53: no such host,可能是host文件的修改还没有生效,重启一下电脑就可以,如果重启还是不行参考这里,实在不行就通过虚拟机的ip地址直接访问就行了

查看es

上次编辑于:
贡献者: soulballad