一、Garylog平臺相關組件簡介
- Graylog-server:Graylog接收來自後端各種應用程序的日誌並提供Web訪問接口
- Graylog Collector Sidecar:負責收集應用程序日誌併發送至Graylog-server
- Elasticsearch:用於索引和保存接收到的日誌,性能依賴內存及硬盤IO
- MongoDB:負責保存 Graylog 自身的配置信息,負載不高
- GeoIP_CityDatabase:通過Graylog來分析Ngnix日誌,獲取訪問者IP,然後使用GeoIP2數據庫分析IP的地理位置
二、Garylog和ELK對比
整體架構對比
- Graylog:Graylog Collector Sidecar -> Graylog Server(封裝ElstaicSearch) -> Graylog Web
- ELK:Logstash -> ElasticSearch -> Kibana
Graylog優點:
- 部署維護簡單,上手難度小,一體化解決方案
- 對比ES的json語法,搜索語法相對簡單,搜索結果可高亮顯示
- 內置簡單告警可以通過網頁API或郵件提醒
- 可直接導出搜索json文件,方便開發調用rest api的搜索腳本
三、Garylog架構圖解
單節點安裝
適用於小型日誌收集,測試環境安裝,沒有冗餘,但安裝部署快速,本文采用此安裝方法
集羣安裝
適用於生產環境,在多個Graylog節點前增加負載均衡,ElasticSearch及MongoDB均採用集羣部署,負載均衡器可以檢查節點是否正常,如果節點連接斷開,可將節點移除
四、Graylog安裝及配置
1.jdk安裝
安裝版本爲jdk1.8.0_161,安裝過程(略)
2.ElasticSearch安裝
安裝版本爲elasticsearch-5.6.11,安裝插件x-pack,管理工具elasticsearch-head
中文分詞插件elasticsearch-analysis-ik-5.6.11,elasticsearch-analysis-pinyin-5.6.11
注意Graylog2.4對應的ElasticSearch版本不能高於5.x,否則無法安裝
注意Graylog2.3及之前版本對應的ElasticSearch版本不能高於4.x,否則無法安裝
(1)安裝elasticsearch-5.6.11
解壓elasticsearch-5.6.11.tar.gz,tar -zxf elasticsearch-5.6.11.tar.gz -C /usr/local
修改elasticsearch.yml配置如下
path.data:
- /data/elasticsearch_1
- /data/elasticsearch_2
- /data/elasticsearch_3
path.logs: /data/elasticsearch_logs
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
xpack.security.audit.enabled: true
xpack.security.authc.accept_default_password: false
indices.store.throttle.max_bytes_per_sec: 150mb
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
http.cors.enabled: true
http.cors.allow-origin: "*"
Elasticsearch不能用root啓動,需要新建用戶elastic,切換到elastic下啓動
./elasticsearch -d
通過瀏覽器訪問http://10.10.10.1:9200/,返回elasticsearch相關信息說明啓動成功
(2)安裝x-pack
bin/elasticsearch-plugin install x-pack,按提示點擊下一步安裝
(3)安裝中文分詞器ik及pinyin
進入/usr/local/elasticsearch-5.6.11/plugins,解壓elasticsearch-analysis-ik-5.6.11.zip到ik目錄
進入/usr/local/elasticsearch-5.6.11/plugins,解壓elasticsearch-analysis-pinyin-5.6.11.zip到pinyin目錄,重啓elasticsearch
在瀏覽器輸入下面地址,如果出現分詞說明配置成功
http://192.168.0.100:9200/_analyze?analyzer=ik&pretty=true&text=helloworld,%E6%AC%A2%E8%BF%8E%E6%82%A8
(4)安裝管理工具elasticsearch-head
解壓elasticsearch-head-master.zip到/usr/local/下
chown -R elastic:elastic /usr/local/elasticsearch-head-master
cd /usr/local/elasticsearch-head-master
npm install && nohup npm run start &
訪問http://10.10.10.1:9100,如果出現以下頁面說明安裝成功
3.MongoDB安裝
使用阿里雲MongoDB版RDS集羣,版本3.4,官方建議採用最新的版本,安裝過程(略)
4.Graylog-server安裝
安裝版本爲graylog-2.4.6
rpm -Uvh https://packages.graylog2.org/repo/packages/graylog-2.4-repository_latest.rpm
通過yum安裝yum -y install graylog-server
修改配置文件vim /etc/graylog/server/server.conf,修改內容如下:
使用pwgen生成 password_secret加密碼 pwgen -N 1 -s 96
U9oMoxRYJ0DB4K30SsQPhAeL7237aTTy0kSpLiEbrNNSqGp6sQaJO4zq9fI2I6FEVkpIomGNQMrssj8IQqmgIhTDNrUbQbnk
將上面這段祕鑰添加到password_secret =
使用echo -n 123456 | sha256sum生成登錄密碼,賬號爲admin
8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
將上面這段祕鑰添加到root_password_sha2 =
修改時區root_timezone = UTC改爲root_timezone = Asia/Shanghai
修改web及api監聽地址
rest_listen_uri = http://127.0.0.1:9000/api/ 改爲 http://0.0.0.0:9000/graylog/api/
rest_transport_uri= http://192.168.1.1:9000/api/ 改爲 https://graylog.example.com/graylog/api/
web_listen_uri = http://127.0.0.1:9000/ 改爲 http://0.0.0.0:9000/graylog/
rest_enable_cors = false
修改ElasticSearch連接地址
elasticsearch_hosts = http://elastic:[email protected]:9200
修改MongoDB連接地址
mongodb_uri = mongodb://graylog:[email protected]:3717/graylog
添加開機自啓動chkconfig --add graylog-server
添加graylog-server到系統服務systemctl enable graylog-server.service
搜索結果高亮allow_highlighting = true
5.Graylog-collector-sidecar安裝
到collector-sidecar的git項目下https://github.com/Graylog2/collector-sidecar/releases下載最新的穩定版
安裝rpm -i collector-sidecar-0.0.9-1.x86_64.rpm
修改配置文件collector_sidecar.yml,改server_url: http://10.10.10.1:9000/graylog/api/,添加tags
檢查配置文件是否正確
graylog-collector-sidecar -configtest -c /etc/graylog/collector-sidecar/collector_sidecar.yml
安裝服務graylog-collector-sidecar -service install
啓動服務systemctl start collector-sidecar
6.配置Nginx
server_name graylog.example.com;
location /graylog/ {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Graylog-Server-URL http://$server_name/api;
proxy_pass http://127.0.0.1:9000/graylog/;
}
location /graylog/api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9000/graylog/api/;
}
配置完成後,重啓nginx和graylog-server