前提條件:你的服務器上已經安裝了Docker和Docker Compose
一、安裝GrayLog(Docker方式)
version: '2' services: mongodb: image: mongo:3 volumes: # Persisting data - ./data/mongo:/data/db elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3 volumes: # Persisting data - ./data/elasticsearch:/usr/share/elasticsearch/data environment: - http.host=0.0.0.0 - transport.host=localhost - network.host=0.0.0.0 - xpack.security.enabled=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 mem_limit: 1g graylog: image: graylog/graylog:2.4.0-1 volumes: # Persisting data - ./data/graylog:/usr/share/graylog/data/journal # Mount local configuration directory into Docker container - ./config:/usr/share/graylog/data/config environment: # CHANGE ME! - GRAYLOG_PASSWORD_SECRET=somepasswordpepper # Password: admin - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 # 這里必須修改為你機器的外網地址 - GRAYLOG_WEB_ENDPOINT_URI=http://127.0.0.1:9000/api links: - mongodb:mongo - elasticsearch depends_on: - mongodb - elasticsearch ports: # Graylog web interface and REST API - 9000:9000 # Syslog TCP - 514:514 # Syslog UDP - 514:514/udp # GELF TCP - 12201:12201 # GELF UDP - 12201:12201/udp
二、自定義GrayLog配置文件
在與上面docker-compose.yml文件同級目錄下執行如下命令下載官方配置文件
mkdir -p config cd config wget https://raw.githubusercontent.com/Graylog2/graylog-docker/2.4/config/graylog.conf wget https://raw.githubusercontent.com/Graylog2/graylog-docker/2.4/config/log4j2.xml
(可選)如果需要修改GrayLog時區和搜索高亮顯示,則需要修改graylog.conf如下配置:
# 設置時區
root_timezone = Asia/Shanghai # 開啟高亮顯示
allow_highlighting = true
三、配置GrayLog日志接收器
瀏覽器訪問:http://127.0.0.1:9000(默認賬號是admin/admin),依次打開:System/Inputs
四、使用Logspout將Docker輸出發送到GrayLog
version: '3' services: #服務名稱 logspout: #服務使用的鏡像 image: micahhausler/logspout:gelf volumes: - "/var/run/docker.sock:/var/run/docker.sock" #需要改成你的GrayLog服務器所在的地址 command: gelf://127.0.0.1:12201 #服務端口號 ports: - "8000:80" bounter-docker: image: 127.0.0.1:5000/bounter/bounter-docker #修改容器時區,使它與宿主機保持一致 volumes: - "/etc/timezone:/etc/timezone" - "/etc/localtime:/etc/localtime" ports: - "8080:8080"
五、查看日志
是不是覺得挺簡單啊,那就趕快自己動手試試吧!