Errno :: EADDRNOTAVAIL - LogStashLogger没有连接到Logstash - Ruby on Rails

时间:2018-03-30 23:13:36

标签: ruby-on-rails ruby docker docker-compose logstash

我一直在努力让日志在使用ELK(Elasticsearch Logstash Kibana)堆栈(也是Dockerized)的Dockerized Rails 5.1.5应用程序上运行。我在设置时遇到了问题。我还没有能够从Rails发送一个日志到logstash。

目前的问题是:

  

错误 - :[LogStashLogger :: Device :: TCP] Errno :: EADDRNOTAVAIL - 不能   分配请求的地址 - 连接(2)for" localhost" 5228港口

起初,我认为我目前的ELK配置存在问题。花了几天之后,我终于通过使用Ubuntu for Win10通过nc命令发送一个虚拟的.log文件来确定ELK是否正常工作,并且它工作了!!!

现在我知道问题出在Rails上,我一直在尝试一组不同的组合,但我仍然无法让它发挥作用:

  1. 我检查过Logstash的配置是否正确接受TCP,并且是正确的端口。
  2. 在Logstash和Rails中更改为其他端口。
  3. 我目前正在使用 Docker-Compose V3 。我首先初始化了ELK,然后是Rails(但问题仍然存在)
  4. 在UDP和TCP之间切换。
  5. 未在logstash.conf中指定编解码器。
  6. 在logstash.conf
  7. 中指定了json_lines编解码器
  8. 我尝试在docker-compose.yml中的logstash和rails之间指定link(即使它已被docker-compose v3弃用)
  9. 我尝试通过network在docker-compose中将它们组合在一起。
  10. 我已尝试在docker-compose的rails应用中指定depends_on logstash。
  11. 我的想法用完了:

    这里是日志配置(现在它在development.rb中):

      config.log_level = :debug
      config.lograge.enabled = true
      config.lograge.formatter = Lograge::Formatters::Logstash.new
      config.logger = LogStashLogger.new(type: :tcp, host: 'localhost', port: 5228)
    

    logstash conf:

    input {
     tcp {
        port => 5228
        codec => json_lines
      }
    }
    
    ## Add your filters / logstash plugins configuration here
    
    output {
        elasticsearch {
            hosts => ["elasticsearch:9200"]
        }
    }
    

    最后,但并非最不重要的,这是Docker-compose:

    version: '3'
    services:
      db:
        # This specifies a MySQL image that will be created with root privileges 
        # Note that MYSQL_ROOT_PASSWORD is Mandatory!
        # We specify the 5.7.21'th version of MySQL from the docker repository.
        # We are using mariadb because there's an apparent problem with permissions. 
        # See: https://github.com/docker-library/mysql/issues/69
        image: mariadb:10.3.5
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: "rootPassword"
          MYSQL_USER: "ruby"
          MYSQL_PASSWORD: "userPassword"
          MYSQL_DATABASE: "dev"
        ports:
        - "3306:3306"
        volumes:
          - db-data:/var/lib/mysql/data
          - ./db/rails_.sql:/docker-entrypoint-initdb.d/rails_.sql
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.2.3
        container_name: elasticsearch
        environment:
          - cluster.name=docker-cluster
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        ulimits:
          memlock:
            soft: -1
            hard: -1
        volumes:
          - elasticsearch:/usr/share/elasticsearch/data
        ports:
          - 9200:9200
        networks:
          - elk
      logstash:
        container_name: logstash
        image: docker.elastic.co/logstash/logstash:6.2.3
        volumes:
          - ./elk/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
          - ./elk/logstash/pipeline/logstash.conf:/etc/logstash/conf.d/logstash.conf
        command: logstash -f /etc/logstash/conf.d/logstash.conf
        ports:
          - "5050:5050"
          - "5228:5228"
        environment:
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        networks:
          - elk
        depends_on:
          - elasticsearch
      kibana:
        image: docker.elastic.co/kibana/kibana:6.2.3
        volumes:
          - ./elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
        ports:
          - "5601:5601"
        networks:
          - elk
        depends_on:
          - elasticsearch
      app:
        depends_on:
          - db
        environment:
          RAILS_ENV: development
          LOGSTASH_HOST: localhost
          SECRET_MYSQL_HOST: 'db'
          SECRET_MYSQL_DATABASE: 'dev'
          SECRET_MYSQL_USERNAME: 'ruby'
          SECRET_MYSQL_PASSWORD: 'userPassword'
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'"
        stdin_open: true
        tty: true
        links:
          - db
          - logstash
        volumes:
          - "./:/var/www/rails"
        ports:
          - "3001:3001"
        expose:
          - "3001"
        networks:
          - elk
      db-data:
        driver: local
      elasticsearch:
        driver: local
    
    networks:
        elk:
          driver: bridge
    

    非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

  

默认情况下,Compose会为您的应用设置单个网络。每   服务容器加入默认网络,两者都是   该网络上的其他容器可以访问,并且可以被发现   它们的主机名与容器名相同。

根据docker-compose.yaml文件,logstash容器可以从logstash:5228从其他容器访问,因此app容器上的日志配置应更改为:

config.log_level = :debug
  config.lograge.enabled = true
  config.lograge.formatter = Lograge::Formatters::Logstash.new
  config.logger = LogStashLogger.new(type: :tcp, host: 'logstash', port: 5228, formatter: :json_lines, sync: true))

检查logstash.conf是这样的:

input {
 tcp {
    port => 5228
    codec => json_lines
  }
}

## Add your filters / logstash plugins configuration here

output {
    elasticsearch {
        hosts => ["elasticsearch:9200"]
    }
}