1、環(huán)境介紹
| 安裝軟件 | 主機(jī)名 | IP地址 | 系統(tǒng)版本 |
|---|---|---|---|
| Elasticsearch | mes-1 | 10.8.156.35 | centos7.4--3G |
| tomcat/nginx/Logstash | es-2 | 10.8.156.91 | centos7.4--2G |
| head/Kibana | es-3 | 10.8.156.34 | centos7.4---2G |
2、軟件版本
下載地址https://www.elastic.co/cn/downloads/past-releases#logstash
Elasticsearch: 7.8.0
Logstash: 7.8.0
Kibana: 7.8.0
3、Elasticsearch部署
因?yàn)镋lasticsearch7.8.0內(nèi)置有jdk環(huán)境,所以我們不需要再提供本地jdk環(huán)境
3.1 創(chuàng)建運(yùn)行ES的普通用戶
[root@mes-1 ~]# useradd elsearch
[root@mes-1 ~]# echo "123456" | passwd --stdin "elsearch"
3.2 安裝配置ES
[root@mes-1 ~]# tar xzf elasticsearch-7.8.0.tar.gz -C /usr/local/
[root@mes-1 ~]# cd /usr/local/elasticsearch-7.8.0/config/
[root@mes-1 config]# ls
elasticsearch.yml log4j2.properties roles.yml users_roles
jvm.options role_mapping.yml users
[root@mes-1 config]# cp elasticsearch.yml elasticsearch.yml.bak
[root@mes-1 config]# vim elasticsearch.yml ----找個(gè)地方添加如下內(nèi)容
cluster.name: elk
node.name: elk01
node.master: true
node.data: true
cluster.initial_master_nodes: ["elk01"]
path.data: /data/elasticsearch/data
path.logs: /data/elasticsearch/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
#discovery.zen.ping.unicast.hosts: ["192.168.246.234", "192.168.246.231","192.168.246.235"]
#discovery.zen.minimum_master_nodes: 2
#discovery.zen.ping_timeout: 150s
#discovery.zen.fd.ping_retries: 10
#client.transport.ping_timeout: 60s
http.cors.enabled: true
http.cors.allow-origin: "*"
配置項(xiàng)含義:
cluster.name 集群名稱,各節(jié)點(diǎn)配成相同的集群名稱。
node.name 節(jié)點(diǎn)名稱,各節(jié)點(diǎn)配置不同。
node.master 指示某個(gè)節(jié)點(diǎn)是否符合成為主節(jié)點(diǎn)的條件。
node.data 指示節(jié)點(diǎn)是否為數(shù)據(jù)節(jié)點(diǎn)。數(shù)據(jù)節(jié)點(diǎn)包含并管理索引的一部分。
path.data 數(shù)據(jù)存儲(chǔ)目錄。
path.logs 日志存儲(chǔ)目錄。
bootstrap.memory_lock 內(nèi)存鎖定,是否禁用交換。
bootstrap.system_call_filter 系統(tǒng)調(diào)用過濾器。
network.host 綁定節(jié)點(diǎn)IP。
http.port 端口。
discovery.zen.ping.unicast.hosts 提供其他 Elasticsearch 服務(wù)節(jié)點(diǎn)的單點(diǎn)廣播發(fā)現(xiàn)功能。
discovery.zen.minimum_master_nodes 集群中可工作的具有Master節(jié)點(diǎn)資格的最小數(shù)量,官方的推薦值是(N/2)+1,其中N是具有master資格的節(jié)點(diǎn)的數(shù)量。
discovery.zen.ping_timeout 節(jié)點(diǎn)在發(fā)現(xiàn)過程中的等待時(shí)間。
discovery.zen.fd.ping_retries 節(jié)點(diǎn)發(fā)現(xiàn)重試次數(shù)。
http.cors.enabled 是否允許跨源 REST 請(qǐng)求,用于允許head插件訪問ES。
http.cors.allow-origin 允許的源地址。
3.3 設(shè)置JVM堆大小
[root@mes-1 config]# vim jvm.options ----將
-Xms1g ----修改成 -Xms2g
-Xmx1g ----修改成 -Xms2g
#一般設(shè)置為虛擬機(jī)內(nèi)存的一半,最少為2G,推薦設(shè)置為4G
注意:確保堆內(nèi)存最小值(Xms)與最大值(Xmx)的大小相同,防止程序在運(yùn)行時(shí)改變堆內(nèi)存大小。堆內(nèi)存大小不要超過系統(tǒng)內(nèi)存的50%
3.4 創(chuàng)建ES數(shù)據(jù)及日志存儲(chǔ)目錄
[root@mes-1 ~]# mkdir -p /data/elasticsearch/data
[root@mes-1 ~]# mkdir -p /data/elasticsearch/logs
3.5 修改安裝目錄及存儲(chǔ)目錄權(quán)限
[root@mes-1 ~]# chown -R elsearch:elsearch /data/elasticsearch
[root@mes-1 ~]# chown -R elsearch:elsearch /usr/local/elasticsearch-7.8.0
3.6 系統(tǒng)優(yōu)化
3.6.1 增加最大文件打開數(shù)
永久生效方法echo "* - nofile 65536" >> /etc/security/limits.conf
3.6.2 增加最大進(jìn)程數(shù)
[root@mes-1 ~]# vim /etc/security/limits.conf ---在文件最后面添加如下內(nèi)容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
解釋:
soft xxx : 代表警告的設(shè)定,可以超過這個(gè)設(shè)定值,但是超過后會(huì)有警告。
hard xxx : 代表嚴(yán)格的設(shè)定,不允許超過這個(gè)設(shè)定的值。
nofile : 是每個(gè)進(jìn)程可以打開的文件數(shù)的限制
nproc : 是操作系統(tǒng)級(jí)別對(duì)每個(gè)用戶創(chuàng)建的進(jìn)程數(shù)的限制
3.6.3 增加最大內(nèi)存映射數(shù)
#增大用戶使用內(nèi)存的空間(臨時(shí))
[root@mes-1 ~]# sysctl -w vm.max_map_count=262144
#永久
[root@mes-1 ~]# vim /etc/sysctl.conf ---添加如下
vm.max_map_count=262144
vm.swappiness=0
[root@mes-1 ~]# sysctl -p
3.7 啟動(dòng)ES
[root@mes-1 ~]# su - elsearch
[elsearch@mes-1 ~]$ cd /usr/local/elasticsearch-7.8.0/
[elsearch@mes-1 elasticsearch-7.8.0]$ nohup ./bin/elasticsearch & #放后臺(tái)啟動(dòng)
[1] 11731
[elsearch@mes-1 elasticsearch-7.8.0]$ nohup: ignoring input and appending output to ‘nohup.out’
[elsearch@mes-1 elasticsearch-7.8.0$ tail -f nohup.out #看一下是否啟動(dòng)
瀏覽器訪問10.8.156.35:9200

4、安裝配置head監(jiān)控插件(Web前端)
主機(jī):10.8.165.34
4.1 安裝node
[root@es-3 ~]# wget https://npm.taobao.org/mirrors/node/latest-v4.x/node-v4.4.7-linux-x64.tar.gz
[root@es-3 ~]# tar -xzf node-v4.4.7-linux-x64.tar.gz -C /usr/local/
[root@es-3 ~]# vim /etc/profile #添加如下變量
NODE_HOME=/usr/local/node-v4.4.7-linux-x64
PATH=$NODE_HOME/bin:$PATH
export NODE_HOME PATH
[root@es-3 ~]# source /etc/profile
[root@es-3 ~]# node --version #檢查node版本號(hào)
v4.4.7
4.2 下載head插件
[root@es-3 ~]# wget https://github.com/mobz/elasticsearch-head/archive/master.zip
[root@es-3 ~]# cp master.zip /usr/local/
[root@es-3 ~]# yum -y install unzip
[root@es-3 ~]# cd /usr/local
[root@es-3 ~]# unzip master.zip
4.3 安裝grunt
[root@es-3 ~]# cd elasticsearch-head-master/
[root@es-3 elasticsearch-head-master]# npm install -g grunt-cli #時(shí)間會(huì)很長
[root@es-3 elasticsearch-head-master]# grunt --version #檢查grunt版本號(hào)
grunt-cli v1.3.2
4.4 修改head源碼
[root@es-3 elasticsearch-head-master]# vim /usr/local/elasticsearch-head-master/Gruntfile.js (95行左右)

添加hostname,注意在上一行末尾添加逗號(hào),hostname 不需要添加逗號(hào)
[root@es-3 elasticsearch-head-master]# vim /usr/local/elasticsearch-head-master/_site/app.js如果head和ES在一臺(tái)機(jī)器上面可以不修改下面的操作,保持原來的就可以了。
原本是http://localhost:9200 ,如果head和ES不在同一個(gè)節(jié)點(diǎn),注意修改成ES的IP地址

4.5 下載head必要的文件
[root@es-3 ~]# wget https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@es-3 ~]# yum -y install bzip2
[root@es-3 ~]# tar -jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /tmp/ #解壓
4.6 運(yùn)行head
[root@es-3 ~]# cd /usr/local/elasticsearch-head-master/
[root@es-3 elasticsearch-head-master]# npm config set registry https://registry.npm.taobao.org
[root@es-3 elasticsearch-head-master]# npm install
[root@es-3 elasticsearch-head-master]# nohup grunt server &
[root@es-3 elasticsearch-head-master]# tail -f nohup.out
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
4.7 測試
瀏覽器訪問:http://10.8.156.34:9100/

5、kibana部署
主機(jī)IP:10.8.156.34
5.1 安裝配置Kibana
[root@es-3 ~]# tar zvxf kibana-7.8.0-linux-x86_64.tar.gz -C /usr/local/
[root@es-3 ~]# cd /usr/local/kibana-7.8.0-linux-x86_64/config/
[root@es-3 config]# vim kibana.yml
server.port: 5601
server.host: "10.8.156.34" #kibana本機(jī)的地址
elasticsearch.hosts: "http://10.8.156.35:9200" #ES主節(jié)點(diǎn)地址+端口
kibana.index: ".kibana"
配置項(xiàng)含義:
server.port kibana 服務(wù)端口,默認(rèn)5601
server.host kibana 主機(jī)IP地址,默認(rèn)localhost
elasticsearch.hosts 用來做查詢的ES節(jié)點(diǎn),默認(rèn)http://localhost:9200
kibana.index kibana在Elasticsearch中使用索引來存儲(chǔ)保存的searches, visualizations和dashboards,默認(rèn).kibana
啟動(dòng):
[root@es-3 config]# cd ..
[root@es-3 kibana-7.8.0-linux-x86_64]# nohup ./bin/kibana --allow-root &
[root@es-3 kibana-7.8.0-linux-x86_64]# tailf nohup.out
5.2 安裝配置Nginx反向代理
5.2.1 安裝nginx
[root@es-3 ~]# yum install -y eprl-release;yum install -y nginx
5.2.2 配置反向代理
[root@es-3 ~]# vim /etc/nginx/nginx.conf -----如果里面有server模塊要?jiǎng)h掉
[root@es-3 ~]# cd /etc/nginx/conf.d/
[root@es-3 conf.d]# vim nginx.conf
server {
listen 80;
server_name 10.8.156.34;
#charset koi8-r;
# access_log /var/log/nginx/host.access.log main;
# access_log off;
location / {
proxy_pass http://10.8.156.34:5601;
proxy_set_header Host $host:5601;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
location /status {
stub_status on; #開啟網(wǎng)站監(jiān)控狀態(tài)
access_log /var/log/nginx/kibana_status.log; #監(jiān)控日志
auth_basic "NginxStatus"; }
location /head/{
proxy_pass http://10.8.156.34:9100;
proxy_set_header Host $host:9100;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
}
5.2.3 配置nginx
1.將原來的log_format注釋掉,添加json格式的配置信息,如下:
[root@es-3-head-kib conf.d]# vim /etc/nginx/nginx.conf
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"referer": "$http_referer",'
'"ua": "$http_user_agent"'
'}';
2.引用定義的json格式的日志:
access_log /var/log/nginx/access_json.log json;

5.2.4 啟動(dòng)nginx
[root@es-3 ~]# systemctl start nginx
瀏覽器訪問http://10.8.156.34/剛開始沒有任何數(shù)據(jù),會(huì)提示你創(chuàng)建新的索引。


6、Logstash部署
主機(jī)IP:10.8.156.91
6.1 安裝jdk
[root@es-2 ~]# tar -xvzf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@es-2 ~]# cd /usr/local/
[root@es-2 local]# mv jdk1.8.0_211/ java
[root@es-2 local]# vim /etc/profile
[root@es-2 local]# vim /etc/profile ----添加
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
[root@es-2 local]# source /etc/profile
6.2 安裝配置nginx
[root@es-2 ~]# yum install -y eprl-release;yum install -y nginx
#將原來的日志格式注釋掉定義成json格式:
[root@es-2-zk-log conf.d]# vim /etc/nginx/nginx.conf
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"referer": "$http_referer",'
'"ua": "$http_user_agent"'
'}';
#引用定義的json格式的日志:
access_log /var/log/nginx/access_json.log json;
#啟動(dòng)nginx
[root@es-2 ~]# systemctl start nginx
[root@es-2 ~]# systemctl enable nginx
瀏覽器多訪問幾次
6.3 安裝配置logstash
[root@es-2 ~]# tar xvzf logstash-7.8.0.tar.gz -C /usr/local/
#創(chuàng)建目錄,我們將所有input、filter、output配置文件全部放到該目錄中。
[root@es-2 ~]# mkdir -p /usr/local/logstash-7.8.0/etc/conf.d
[root@es-2 ~]# cd /usr/local/logstash-7.8.0 /etc/conf.d/
[root@es-2 conf.d]# vim input.conf #---在下面添加
input{ #讓logstash可以讀取特定的事件源。
file{ #從文件讀取
path => ["/var/log/nginx/access_json.log"] #要輸入的文件路徑
# code => "json" #定義編碼,用什么格式輸入和輸出,由于日志就是json格式,這里不用再寫
type => "shopweb" #定義一個(gè)類型,通用選項(xiàng). 用于激活過濾器
}
}
[root@es-2 conf.d]# vim output.conf
output{ #輸出插件,將事件發(fā)送到特定目標(biāo)
elasticsearch { #輸出到es
hosts => ["10.8.156.35:9200"] #指定es服務(wù)的ip加端口
index => ["%{type}-%{+YYYY.MM.dd}"] #引用input中的type名稱,定義輸出的格式
}
}
啟動(dòng):
[root@es-2 conf.d]# cd /usr/local/logstash-7.8.0/
[root@es-2 logstash-7.8.0]# nohup bin/logstash -f etc/conf.d/ --config.reload.automatic &
[root@es-2 logstash-7.8.0]# tailf nohup.out
6.4、收集nginx日志
在瀏覽器中訪問本機(jī)的nginx網(wǎng)站,然后去head插件頁面查看是否有shopweb索引出現(xiàn)

發(fā)現(xiàn)之后,去配置kibanna添加索引





7、收集Tomcat日志
配置文件:
[root@es-2 logstash-7.8.0]# cat etc/conf.d/tomcat.conf
input {
file {
path => "/usr/local/tomcat/logs/localhost_access_log*.txt"
type => "tomcat"
# start_position => "beginning"
# stat_interval => "2"
}
}
output {
elasticsearch {
hosts => ["10.8.156.35:9200"]
index => ["%{type}-%{+YYYY.MM.dd}"]
}
}
效果如下圖:
