
三種收集方案的優缺點:

下面我們就實踐第二種日志收集方案
一:安裝ELK
1.安裝jdk yum install -y java-1.8.0-openjdk 2.添加ELK源 cat << EOF >>/etc/yum.repos.d/logstash.repo [logstash-7.x] name=Elastic repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF 3.安裝ELK組件 yum install elasticsearch logstash kibana -y 或者: elk下載地址 wget https://www.elastic.co/cn/downloads/ get https://artifacts.elastic.co/downloads/logstash/logstash-7.9.3.rpm wget https://artifacts.elastic.co/downloads/kibana/kibana-7.9.3-x86_64.rpm wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-x86_64.rpm wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.1-x86_64.rpm yum install -y *.rpm 4.修改kibana的配置文件 [root@master ~]# grep -Ev '^$|^#' /etc/kibana/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.hosts: ["http://localhost:9200"] 5.啟動服務 systemctl start elasticsearch && systemctl enable elasticsearch && systemctl status elasticsearch systemctl start kibana && systemctl enabel kibana && systemctl status kibana
二、收集K8S的日志
1、添加logstash配置文件
cat >/etc/logstash/conf.d/logstash-to-es.conf <<EOF
input {
beats {
port => 5044
}
}
filter {
}
output {
if [app] == "k8s" {
if [type] == "module" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "k8s-log-%{+YYYY.MM.dd}"
}
}
}
}
EOF
2、啟動logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-es.conf &
3、創建filebeat的yaml文件
cat >k8s-logs.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-logs-filebeat-config
namespace: kube-system
data:
filebeat.yml: |-
filebeat.prospectors:
- type: log
paths:
- /messages
fields:
app: k8s
type: module
fields_under_root: true
output.logstash:
hosts: ['192.168.10.170:5044']
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: k8s-logs
namespace: kube-system
spec:
selector:
matchLabels:
project: k8s
app: filebeat
template:
metadata:
labels:
project: k8s
app: filebeat
spec:
containers:
- name: filebeat
image: docker.elastic.co/beats/filebeat:6.4.2
args: [
"-c", "/etc/filebeat.yml",
"-e",
]
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 500m
memory: 500Mi
securityContext:
runAsUser: 0
volumeMounts:
- name: filebeat-config
mountPath: /etc/filebeat.yml
subPath: filebeat.yml
- name: k8s-logs
mountPath: /messages
volumes:
- name: k8s-logs
hostPath:
path: /var/log/messages
type: File
- name: filebeat-config
configMap:
name: k8s-logs-filebeat-config
EOF
4、創建pod
kubectl apply -f k8s-logs.yaml
5、kibana添加索引
管理-->堆棧管理-->索引模式



三、收集NGINX日志
1、創建nginx-deployment
默認的日志路徑為:/var/log/nginx/
[root@master logs]# cat >nginx-deployment.yaml <<EOF
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-demo
spec:
replicas: 3
selector:
matchLabels:
project: www
app: www
template:
metadata:
labels:
project: www
app: www
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: Always
ports:
- containerPort: 80
name: web
protocol: TCP
resources:
requests:
cpu: 0.5
memory: 256Mi
limits:
cpu: 1
memory: 1Gi
resources:
requests:
cpu: 0.5
memory: 256Mi
limits:
cpu: 1
memory: 1Gi
livenessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 6
timeoutSeconds: 20
volumeMounts:
- name: nginx-logs
mountPath: /var/log/nginx/
- name: filebeat
image: docker.elastic.co/beats/filebeat:6.4.2
args: [
"-c", "/etc/filebeat.yml",
"-e",
]
resources:
limits:
memory: 500Mi
requests:
cpu: 100m
memory: 100Mi
securityContext:
runAsUser: 0
volumeMounts:
- name: filebeat-config
mountPath: /etc/filebeat.yml
subPath: filebeat.yml
- name: nginx-logs
mountPath: /var/log/nginx/
volumes:
- name: nginx-logs
emptyDir: {}
- name: filebeat-config
configMap:
name: filebeat-nginx-config
EOF
2、創建filebest收集日志
cat >filebeat-nginx-configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-nginx-config
data:
filebeat.yml: |-
filebeat.prospectors:
- type: log
paths:
- /var/log/nginx/access.log
# tags: ["access"]
fields:
app: www
type: nginx-access
fields_under_root: true
- type: log
paths:
- /var/log/nginx/error.log
# tags: ["error"]
fields:
app: www
type: nginx-error
fields_under_root: true
output.logstash:
hosts: ['192.168.10.170:5044']
EOF
3、添加logstas配置
cat >/etc/logstash/conf.d/logstash-to-es.conf <<EOF
input {
beats {
port => 5044
}
}
filter {
}
output {
if [app] == "www" {
if [type] == "nginx-access" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}
else if [type] == "nginx-error" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-error-%{+YYYY.MM.dd}"
}
}
} else if [app] == "k8s" {
if [type] == "module" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "k8s-log-%{+YYYY.MM.dd}"
}
}
}
}
EOF
4、生效清單文件
kubectl apply -f nginx-deployment.yaml kubectl apply -f filebeat-nginx-configmap.yaml
5、修改完成配置文件需要重啟logstash服務
ps -ef |grep logstash|awk -F [" "]+ 'NR==1 {print $2}' |xargs kill
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-es.conf &
6、kibana添加索引

四、收集Tomcat日志
1、創建tomcat-deployment
默認的日志路徑為:/usr/local/tomcat/logs
cat >tomcat-deployment.yaml <<EOF
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: tomcat-java-demo
spec:
replicas: 3
selector:
matchLabels:
project: www
app: www
template:
metadata:
labels:
project: www
app: www
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: tomcat
image: tomcat:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
name: web
protocol: TCP
resources:
requests:
cpu: 0.5
memory: 1Gi
limits:
cpu: 1
memory: 2Gi
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
volumeMounts:
- name: tomcat-logs
mountPath: /usr/local/tomcat/logs
- name: filebeat
image: docker.elastic.co/beats/filebeat:6.4.2
args: [
"-c", "/etc/filebeat.yml",
"-e",
]
resources:
limits:
memory: 500Mi
requests:
cpu: 100m
memory: 100Mi
securityContext:
runAsUser: 0
volumeMounts:
- name: filebeat-config
mountPath: /etc/filebeat.yml
subPath: filebeat.yml
- name: tomcat-logs
mountPath: /usr/local/tomcat/logs
volumes:
- name: tomcat-logs
emptyDir: {}
- name: filebeat-config
configMap:
name: filebeat-config
EOF
2、創建filebest收集tomcat日志
cat >filebeat-tomcat-configmap.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-config
data:
filebeat.yml: |-
filebeat.prospectors:
- type: log
paths:
- /usr/local/tomcat/logs/catalina.*
fields:
app: www
type: tomcat-catalina
fields_under_root: true
multiline:
pattern: '^\['
negate: true
match: after
output.logstash:
hosts: ['192.168.10.170:5044']
EOF
3、添加logstas配置后重啟
cat >/etc/logstash/conf.d/logstash-to-es.conf <<EOF
input {
beats {
port => 5044
}
}
filter {
}
output {
if [app] == "www" {
if [type] == "nginx-access" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}
else if [type] == "nginx-error" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-error-%{+YYYY.MM.dd}"
}
}
else if [type] == "tomcat-catalina" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "tomcat-catalina-%{+YYYY.MM.dd}"
}
}
} else if [app] == "k8s" {
if [type] == "module" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "k8s-log-%{+YYYY.MM.dd}"
}
}
}
}
EOF
生效清單文件
kubectl apply -f tomcat-deployment.yaml kubectl apply -f filebeat-tomcat-configmap.yaml
3、添加logstas配置
cat >/etc/logstash/conf.d/logstash-to-es.conf <<EOF
input {
beats {
port => 5044
}
}
filter {
}
output {
if [app] == "www" {
if [type] == "nginx-access" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}
else if [type] == "nginx-error" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "nginx-error-%{+YYYY.MM.dd}"
}
}
else if [type] == "tomcat-catalina" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "tomcat-catalina-%{+YYYY.MM.dd}"
}
}
} else if [app] == "k8s" {
if [type] == "module" {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "k8s-log-%{+YYYY.MM.dd}"
}
}
}
}
EOF
修改完成配置文件需要重啟服務
ps -ef |grep logstash|awk -F [" "]+ 'NR==1 {print $2}' |xargs kill
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-es.conf &
3、kibana添加索引

ElasticSearch
ElasticSearch 是一個高可用開源全文檢索和分析組件。提供存儲服務,搜索服務,大數據准實時分析等。一般用於提供一些提供復雜搜索的應用
基本概念:
Index
定義:類似於mysql中的database。索引只是一個邏輯上的空間,物理上是分為多個文件來管理的。
命名:必須全小寫
描述:在實踐過程中每個index都會有一個相應的副 本。主要用來在硬件出現問題時,用來回滾數據的。這也某種程序上,加劇了ES對於內存高要求。
Type
定義:類似於mysql中的table,根據用戶需求每個index中可以新建任意數量的type。
Document
定義:對應mysql中的row。有點類似於MongoDB中的文檔結構,每個Document是一個json格式的文本
Mapping
更像是一個用來定義每個字段類型的語義規范。在mysql中類似sql語句,在ES中經過包裝后,都被封裝為友好的Restful風格的接口進行操作。
這一點也是為什么開發人員更願意使用ES的原因。
Shards & Replicas
定義:能夠為每個索引提供水平的擴展以及備份操作。保證了數據的完整性和安全性
描述:
Shards:在單個節點中,index的存儲始終是有限制,並且隨着存儲的增大會帶來性能的問題。為了解決這個問題,ElasticSearch提供一個能夠分割單個index到集群各個節點的功能。你可以在新建這個索引時,手動的定義每個索引分片的數量。
Replicas:在每個node出現宕機或者下線的情況,Replicas能夠在該節點下線的同時將副本同時自動分配到其他仍然可用的節點。而且在提供搜索的同時,允許進行擴展節點的數量,在這個期間並不會出現服務終止的情況。
默認情況下,每個索引會分配5個分片,並且對應5個分片副本,同時會出現一個完整的副本【包括5個分配的副本數據】
elasticsearch常用命令
curl -X GET http://127.0.0.1:9200 #驗證服務
curl -XGET ‘http://localhost:9200/_cluster/stats?pretty’ #elasticsearch 查看集群統計信息
curl -X GET ‘localhost:9200/_cat/health?v’ #查看集群狀態
curl -X PUT HTTP://localhost:9200/test_index?pretty #創建索引:test_index
curl -X GET HTTP://localhost:9200/cat/indices?v #elasticsearch 查看所有索引
curl -s http://192.168.10.170:9200/_cat/indices|grep “msg”|awk ‘{print $3}’|sort
curl -XDELETE ‘localhost:9200/test_index?pretty’ #刪除索引:test_index
logstash工作原理
Logstash事件處理有三個階段:
inputs → filters → outputs。是一個接收,處理,轉發日志的工具。
支持系統日志,webserver日志,錯誤日志,應用日志,包括所有可以拋出來的日志類型。
Input模塊:輸入數據到logstash。
一些常用的輸入為:
file:從文件系統的文件中讀取,類似於tail-f命令
redis:從redis service中讀取
beats:從filebeat中讀取
kafka:從kafka隊列中讀取
Filters:數據中間處理,對數據進行操作。
一些常用的過濾器為:
grok:解析任意文本數據,Grok 是 Logstash 最重要的插件。
它的主要作用就是將文本格式的字符串,轉換成為具體的結構化的數據,配合正則表達式使用。內置120多個解析語法。
官方提供的grok表達式:https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns
grok在線調試:https://grokdebug.herokuapp.com/
mutate [ˈmjuːteɪt]:對字段進行轉換。
例如對字段進行刪除、替換、修改、重命名等。
drop:丟棄一部分events不進行處理。
clone:拷貝 event,這個過程中也可以添加或移除字段。
geoip:添加地理信息(為前台kibana圖形化展示使用)
Outputs模塊:outputs是logstash處理管道的最末端組件。
一個event可以在處理過程中經過多重輸出,但是一旦所有的outputs都執行結束,這個event也就完成生命周期。
一些常見的outputs為:
elasticsearch:可以高效的保存數據,並且能夠方便和簡單的進行查詢。
file:將event數據保存到文件中。
graphite [ˈɡræfaɪt]:將event數據發送到圖形化組件中,一個很流行的開源存儲圖形化展示的組件。
Codecs模塊:codecs 是基於數據流的過濾器,它可以作為input,output的一部分配置。Codecs可以幫助你輕松的分割發送過來已經被序列化的數據。
一些常見的codecs:
json:使用json格式對數據進行編碼/解碼。
multiline:將匯多個事件中數據匯總為一個單一的行。比如:java異常信息和堆棧信息
Kibana
Kibana是一個針對Elasticsearch的開源分析及可視化平台,用來搜索、查看交互存儲在Elasticsearch索引中的數據。使用Kibana,可以通過各種圖表進行高級數據分析及展示。
Kibana讓海量數據更容易理解。它操作簡單,基於瀏覽器的用戶界面可以快速創建儀表板(dashboard)實時顯示Elasticsearch查詢動態。
設置Kibana非常簡單。無需編碼或者額外的基礎架構,幾分鍾內就可以完成Kibana安裝並啟動Elasticsearch索引監測。
參考地址:https://blog.csdn.net/mazhiwb/article/details/105775199
https://www.cnblogs.com/Dev0ps/p/10778962.html
