環境介紹html
elasticsearch:6.0.1 下載地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.tar.gz前端
logstash:6.0.1 下載地址:https://artifacts.elastic.co/downloads/logstash/logstash-6.0.1.tar.gzjava
kibana:6.0.1 下載地址:https://artifacts.elastic.co/downloads/kibana/kibana-6.0.1-linux-x86_64.tar.gznode
Filebeat:6.0.1 下載地址:https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.0.1-linux-x86_64.tar.gzlinux
咱們按照順序安裝nginx
192.168.1.1:git
1. 安裝JDK1.8以上,去官網複製連接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlgithub
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" 官網複製到的安裝包連接npm
(例如個人:wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u171-linux-x64.tar.gz)vim
tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local //解壓到指定目錄
sed -i '$a\ \n###JAVA###\nexport JAVA_HOME=/usr/local/jdk1.8.0_171\nexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar\nexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile //添加環境變量
source /etc/profile //使環境變量生效
java -version //查看是否成功
2. 安裝ElasticSearch
tar -zxvf elasticsearch-6.0.1.tar.gz -C /usr/local
修改elasticsearch/config/elasticsearch.yml:
cluster.name: server-logs //自定義集羣名稱 node.name: node-001 //自定義節點名稱 path.data: /usr/local/elasticsearch-6.0.1/data //自定義數據目錄 沒有則建立 path.logs: /usr/local/elasticsearch-6.0.1/logs //自定義服務日誌目錄 沒有則建立 network.host: 192.168.1.1 //主機地址
elasticsearch不容許以root用戶啓動,建立新的用戶
useradd elkuser
chown elkuser:elkuser /usr/local/elasticsearch-6.0.1 -R //修改es目錄屬主
echo "* soft nofile 65536" >> /etc/security/limits.conf //修改最大文件數 重啓生效
echo "* hard nofile 131072" >> /etc/security/limits.conf //同上
ulimit -n 65536 //當前終端臨時生效
echo "elkuser soft nproc 4096" >> /etc/security/limits.conf //修改最大進程數 使用elkuser用戶啓動即指定修改elkuser用戶 重啓生效
echo "elkuser hard nproc 4096" >> /etc/security/limits.conf //同上
ulimit -u 4096 //當前終端臨時生效
ulimit -a //查看
echo "vm.max_map_count=262144" >> /etc/sysctl.conf //修改最大虛擬內存
sysctl -p //生效
sysctl -w vm.max_map_count=262144 //同上 當前終端臨時生效
su - elkuser
/usr/local/elasticsearch-6.0.1/bin/elasticsearch -d && tailf /usr/local/elasticsearch6.0.1/logs/server-logs.log //切換至elkuser啓動 -d 後臺啓動 追蹤日誌信息中有無ERROR 日誌文件名爲自定義集羣名稱
ps -ef |grep elastic //查看進程是否存在
curl -XGET '192.168.1.1:9200/?pretty' //請求查看返回數據是否與修改的一致
3. 爲Elasticsearch安裝一個前端頁面幫助咱們更好的管理服務:elasticsearch-head
此服務須要一些必要的環境運行,先安裝nvm (項目地址:https://github.com/creationix/nvm)
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash //腳本安裝
source ~/.nvm/nvm.sh //激活nvm
nvm install node //激活後安裝node
nvm use node //安裝完成後切換到該版本
npm install -g grunt-cli //安裝grunt grunt是基於Node.js的項目構建工具,能夠進行打包壓縮、測試、執行等等的工做,head插件就是經過grunt啓動 執行後會勝場node_modules目錄 grunt -version檢查是否安裝成功
以上必要的環境安裝完成後則開始安裝elasticsearch-head,項目地址:https://github.com/mobz/elasticsearch-head
cd /usr/local/elasticsearch-6.0.1 && git clone https://github.com/mobz/elasticsearch-head.git //拉取項目
cd elasticsearch-head/ && npm install 安裝服務
sed -i "93a\ \t\t\t\t\thostname: '*'," elasticsearch-head/Gruntfile.js //增長服務器監聽地址屬性
sed -i 's/localhost/192.168.1.1/g' elasticsearch-head/_site/app.js //修改鏈接地址 將localhost替換爲本身的es服務器ip
sed -i 's/en,fr,pt,zh,zh-TW,tr,ja/zh/g' elasticsearch-head/index.html //漢化
nohup npm run start & //後臺啓動服務 須要在服務所在目錄下執行
vim /usr/local/elasticsearch-6.0.1/config/elasticsearch.yml底部添加幾條屬性
http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-credentials: true
重啓es後訪問http://192.168.1.1:9100 出現如下畫面即表明成功
4. 安裝Logstash
tar zxvf logstash-6.0.1.tar.gz -C /usr/local
vim /usr/local/logstash-6.0.1/config/logstash-test.conf //建立一個配置文件
input { //數據來源
file { //文件方式 此爲了收集本機的日誌
path => /usr/local/nginx/logs/*.log //路徑
type => "nginx-logs" //事務類型
start_position => "beginning" //從文件開頭開始讀取 end則爲從尾部開始讀取
} beats { //filebeat port => 5044 //端口 } }
filter {
過濾規則 \\此處較爲複雜 略過 根據本身需求百度學習
} output { //輸出到es elasticsearch { hosts => ["192.168.1.1:9200"] //es地址 index => "%{[fields][log_source]}-%{+YYYY.MM.dd}" //使用filebeat中配置的field字段來區分不一樣的索引 } }
nohup /usr/local/logstash-6.0.1/bin/logstash -f config/logstash-test.conf & //指定配置文件後臺啓動
5. 安裝Kibana
tar zxvf kibana-6.0.1-linux-x86_64.tar.gz -C /usr/local
cp -dr /usr/local/kibana-6.0.1-linux-x86_64 /usr/local/kibana-6.0.1 //爲後期漢化作備份,漢化不可逆
vim /usr/local/kibana-6.0.1/config/kibana.yml
server.host: "192.168.1.1" //主機地址 elasticsearch.url: "http://192.168.1.1:9200" //es主機地址
nohup /usr/local/kibana-6.0.1/bin/kibana & //後臺啓動
打開瀏覽器訪問 http://192.168.1.1:5601 驗證
6. 安裝X-Pack
如今咱們已經將所需的服務都安裝完畢了,可是咱們的頁面只要連接就能夠訪問並操做未免也太不安全了,這時候咱們就須要再安裝一個插件 X-Pack.
X-Pack是ES的一個擴展,它將安全性,警報,監控,報告和圖形功能捆綁在一塊兒,並能夠輕鬆管理要使用的功能
在安裝以前咱們須要肯定咱們的ELK版本,X-Pack的版本必需要和其相匹配.
包下載地址:https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-6.0.1.zip (這個在我安裝時我是下載到本地上傳到服務器上的,由於不知道爲何wget下載這個包速度極慢!)
①. es安裝
su elkuser //es是非root啓動的 安裝他的插件也要切到對應用戶,若是後續忘記這一步能夠elasticsearch-glugin remove x-pack移除插件並刪除config下的elasticsearch.keystore文件和x-pack目錄從新安裝
/usr/local/elasticsearch-6.0.1/bin/elasticsearch-plugin install install file:///usr/local/x-pack-6.0.1.zip //安裝 指定壓縮包 出現內容後連按兩次y便可安裝成功,以後也會在plugins目錄下生成插件對應的目錄.
/usr/local/elasticsearch-6.0.1/bin/x-pack/setup-passwords interactive //執行後輸入y會設置elastic,kibana,logstash_system三個用戶密碼;該命令只可執行一次,以後你能夠在Kibana的管理>用戶界面更新密碼
echo "http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type" >> config/elasticsearch.yml //追加一行到配置文件用於head插件的安全認證
重啓es後訪問 http://192.168.1.1:9100/?auth_user=elastic&auth_password=yourpasswd 登錄head插件查看
②. 修改logstash配置文件添加認證屬性
vim /usr/local/logstash-6.0.1/config/logstash-test.conf //在output區域中添加es認證
output { //輸出到es elasticsearch { hosts => ["192.168.1.1:9200"] index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" user => "elastic" password => "yourpasswd" } }
重啓後查看下nohup文件中的輸出是否有異常信息
③. kibana安裝
/usr/local/kibana-6.0.1/bin/kibana-plugin install file:///usr/local/x-pack-6.0.1.zip //切回用戶一樣方式在kibana安裝
sed -i '$a\elasticsearch.username: "elastic"\nelasticsearch.password: "yourpasswd"' config/kibana.yml //在配置文件中添加認證屬性
重啓後漢化 漢化過程在項目中有詳細講解 漢化項目連接:https://github.com/anbai-inc/Kibana_Hanization
漢化後訪問 http://192.168.1.1:5601 出現如下界面表明成功 (登錄時使用elastic帳戶,我使用了kibana帳戶登錄後沒法鏈接到es獲取索引和數據)
192.168.1.2:
1. 安裝filebeat
tar zxvf filebeat-6.0.1-linux-x86_64.tar.gz -C /usr/local
mv filebeat-6.0.1-linux-x86_64 filebeat //重命名
vim filebeat.yml
filebeat.prospectors: - type: log enabled: true //設置爲true時該配置生效 paths: - /usr/local/nginx/logs/www_access.log //日誌路徑,若是有多個,繼續往下寫 fields: log_source: test_nginx_www //自定義屬性,用來區分日誌,接上述logstash中index屬性配置 - type: log enabled: true paths: - /usr/local/nginx/logs/api_access.log fields: log_source: test_nginx_api //若是要收集相似java的多行爲一條信息的日誌類型可百度一下multiline屬性,此屬性在filebeat和logstash中均可以配置 output.logstash: //輸出到logstash 若是不須要logstash作更加詳細的數據格式化也能夠選擇直接輸出到es hosts: ["192.168.1.1:5044"] index: "filebeat" //這個index的值對應@metadata[beat]的值,能夠自定義,我在此項目中沒有用到該屬性
nohup ./filebeat -e -c filebeat.yml > filebeat.log & //後臺啓動filebeat並將服務信息輸出到filebeat.log中, 若是想看詳細的輸出到logstash的數據流能夠不輸出到文件 -d "publish"而後查看nohup文件
至於爲何logstash就能夠收集咱們還要選擇filebeat呢?
這是由於logstash的功能多可是也很是的吃資源,首先就是必需要保留出1G的內存給它用,可是咱們被收集日誌的服務器原本就是在給咱們的用戶提供服務,負載已經很大了,咱們再增長一個logstash只會使該服務器更不穩定
而filebeat是一個輕量級的日誌傳輸工具,它的存在彌補了logstash的缺點,理論上若是隻是簡單的收集日誌並展現的話僅file+es+kibana就足夠實現了,當咱們須要使用更加複雜的功能時,則可使用該項目的方式將日誌推送到logstash再處理而後統一傳輸給es。