elk搭建实战

作者: 错位的季节 | 来源:发表于2017-10-09 13:04 被阅读42次

    elk搭建实战

    1 安装elasticsearch

    1.1安装elasticsearch

    ES中文文档地址:https://es.xiaoleilu.com

    ES下载地址:https://www.elastic.co/downloads/elasticsearch

    解压:tar -zxvf elasticsearch-5.6.2.tar.gz

    cd elasticsearch-5.6.2/    

    运行

    ./bin/elasticsearch

    ./bin/elasticsearch -d          #后台运行

    tail -f logs/elasticsearch.log           #查看日志

    注意:

    elasticsearch5.0启动失败,出现如下提示:

    1、Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)

    由于elasticsearch5.0默认分配jvm空间大小为2g,修改jvm空间分配

    # vim config/jvm.options

    -Xms2g

    -Xmx2g

    修改为

    -Xms512m

    -Xmx512m

    验证:elasticsearch --version

    注:ES有执行脚本的能力,因安全因素,不能在root用户下运行,强行运行会报如下错误:

    org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root

    解决方案:

    groupadd es          #增加es组

    useradd es -g es -p es123456          #增加es用户并附加到es组

    chown -R es:es elasticsearch-5.6.2   #给目录权限

    su es          #使用es用户

    ./bin/elasticsearch -d          #后台运行es

    外网访问

    vi conf/elasticsearch.yml

    修改network.host: 0.0.0.0

    再次启动linux可能出现如下类似错误

    bootstrap checks failed

    max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

    解决方案

    1、vi /etc/sysctl.conf

    设置fs.file-max=655350

    保存之后sysctl -p使设置生效

    2、vi /etc/security/limits.conf 新增

    * soft nofile 655350

    * hard nofile 655350

    3、重新使用SSH登录,再次启动elasticsearch即可。

    外网访问:serverip:9200/

    2、max number of threads [1024] for user [elasticsearch] is too low, increase to at least [2048]

    修改 /etc/security/limits.d/90-nproc.conf

    *          soft    nproc     1024

    *          soft    nproc     2048

    3、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

    修改/etc/sysctl.conf配置文件,

    cat /etc/sysctl.conf | grep vm.max_map_count

    vm.max_map_count=262144

    如果不存在则添加

    echo "vm.max_map_count=262144" >>/etc/sysctl.conf

    默认端口:9200

    配置elasticsearch:

    打开config/elasticsearch.yml

    修改以下配置:

    path.data: /Users/chenshunyang/tools/elasticsearch/data

    path.logs: /Users/chenshunyang/tools/elasticsearch/logs

    cluster.name: elktest //更改默认的名称为我们想要的名称

    node.name: node-elktest  //更改节点默认的名称

    network.host: localhost

    1.2 安装插件:elasticsearch-head

    elasticsearch 2.x 版本

    elasticsearch 2.x版本中,直接使用/bin/plugin install mobz/elasticsearch-head安装即可

    验证:通过访问http://localhost:9200/_plugin/head/

    elasticsearch 5.1 版本

    安装:elasticsearch 5.1中,elasticsearch-head插件不能放在elasticsearch的 plugins、modules 目录下,不能使用 elasticsearch-plugin install,直接启动elasticsearch即可

    git clone https://github.com/mobz/elasticsearch-head.git

    cd elasticsearch-head/

    npm install grunt

    配置:修改elasticsearch/config/elasticsearch.yml

    添加

    http.cors.enabled: true

    http.cors.allow-origin: "*"

    打开elasticsearch-head-master/Gruntfile.js,找到下面connect属性,新增hostname: ‘0.0.0.0’:

    connect: {

    server: {

    options: {

    hostname: '0.0.0.0',

    port: 9100,

    base: '.',

    keepalive: true

    }

    }

    }

    启动插件:

    elasticsearch-head/ && ./node_modules/grunt/bin/grunt server

    查看

    http://localhost:9100

    2 安装kibana

    下载:从https://www.elastic.co/downloads/kibana下载对应操作系统版本的kibana

    解压: tar -xzvf kibana-5.2.0-darwin-x86_64.tar.gz

    配置kibana:进入kibana/config

    打开kibana.yml,设置elasticsearch.url    为es实例的地址

    eg:elasticsearch.url: "http://localhost:9200"

    启动:./bin/kibana.sh

    默认端口:5601

    访问:http://localhost:5601

    3 安装logstash

    下载:从https://www.elastic.co/downloads/logstash下载对应操作系统版本的logstash

    解压:tar -zxvf logstash-5.2.0.tar.gz

    启动:./bin/logstash -f /home/push/logstash/conf/logstash.conf (logstash.conf为logstash的配置文件)

    验证:logstah --version

    配置logstash:

    生成一个logstash.conf文件,配置示例内容如下

    input {

    kafka {

    zk_connect => "192.168.248.230:2181,192.168.248.175:2181,192.168.249.238:2181/kafka"

    group_id => "sms-service3"

    topic_id => "sms-service"

    codec => "json"

    reset_beginning => false

    consumer_threads => 5

    decorate_events => true

    auto_offset_reset => "smallest"

    }

    }

    output {

    elasticsearch {

    hosts => ["localhost:9200"]

    index => "logstash-smsservice-%{+YYYY.MM.dd}"

    document_type => "sms-service"

    workers => 1

    flush_size => 20000

    idle_flush_time => 10

    template_overwrite => true

    }

    }

    关键配置词语解释:

    Input 解释

    作为 Consumer 端,插件使用的是 High-level-consumer API,请结合上述 kafka 基本概念进行设置:

    group_id

    消费者分组,可以通过组 ID 去指定,不同的组之间消费是相互不受影响的,相互隔离。

    topic_id

    指定消费话题,也是必填项目,指定消费某个 topic ,这个其实就是订阅某个主题,然后去消费。

    reset_beginning

    logstash 启动后从什么位置开始读取数据,默认是结束位置,也就是说 logstash 进程会以从上次读取结束时的偏移量开始继续读取,如果之前没有消费过,那么就开始从头读取.如果你是要导入原有数据,把这个设定改成 "true", logstash 进程就从头开始读取.有点类似 cat ,但是读到最后一行不会终止,而是变成 tail -F ,继续监听相应数据。

    decorate_events

    在输出消息的时候会输出自身的信息包括:消费消息的大小, topic 来源以及 consumer 的 group 信息。

    rebalance_max_retries

    当有新的 consumer(logstash) 加入到同一 group 时,将会 reblance ,此后将会有 partitions 的消费端迁移到新的 consumer 上,如果一个 consumer 获得了某个 partition 的消费权限,那么它将会向 zookeeper 注册, Partition Owner registry 节点信息,但是有可能此时旧的 consumer 尚没有释放此节点,此值用于控制,注册节点的重试次数。

    consumer_timeout_ms

    指定时间内没有消息到达就抛出异常,一般不需要改。

    以上是相对重要参数的使用示例,更多参数可以选项可以跟据https://github.com/joekiller/logstash-kafka/blob/master/README.md查看 input 默认参数。

    output解释:

    Logstash 会努力攒到 20000 条数据一次性发送出去,但是如果 10 秒钟内也没攒够 20000 条,Logstash 还是会以当前攒到的数据量发一次。

    作者:shunyang

    链接:http://www.jianshu.com/p/6b7f0488ddff

    來源:简书

    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    相关文章

      网友评论

        本文标题:elk搭建实战

        本文链接:https://www.haomeiwen.com/subject/ysmuyxtx.html