k8s集羣日誌收集
收集哪些日誌
K8S系統的組件日誌
K8S Cluster裏面部署的應用程序日誌php
Filebeat(日誌採集工具)+Logstach(數據處理引擎)+Elasticserch(數據存儲、全文檢索、分佈式搜索引擎)+Kibana(展現數據、繪圖、搜索)
html
3 容器中的日誌怎麼收集
java
收集方案:Pod中附加專用日誌收集的容器
優勢:低耦合
缺點:每一個Pod啓動一個日誌收集代理,增長資源消耗和運維維護成本
4 部署DaemonSet採起k8s組件日誌/var/log/messages
4.1 ELK安裝在harbor節點
安裝、配置ELK
nginx
1)安裝openjdk
yum -y install java-1.8.0-openjdk
2)安裝Logstash
https://www.elastic.co/guide/en/logstash/7.6/installing-logstash.html
配置網絡源
/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
web
3)安裝ELK組件
yum -y install logstash elasticsearch kibana
4)配置組件
修改kibana配置
vi /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
docker
修改logstash配置
vi /etc/logstash/conf.d/logstash-to-es.confapi
input {
beats {
port => 5044
}tomcat
}ruby
filter {網絡
}
output {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
index => "k8s-log-%{+YYYY.MM.dd}" #索引格式
}
stdout { codec => rubydebug }
}
啓動服務
systemctl start kibana #啓動kibana
systemctl start elasticsearch #啓動elasticsearch
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-es.conf &
192.168.1.143:5601 #訪問kibana
5) 建立pod(master節點執行)收集組件日誌
kubectl apply -f k8s-logs.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-logs-filebeat-config
namespace: kube-system
data:
filebeat.yml: |-
filebeat.prospectors:
type: log
paths:
output.logstash:
hosts: ['192.168.1.143: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:
6)kibana添加索引
kibana界面->Index Patterns-》k8s-log-*->@timestamp->Create index pattern->Discover
7)查看日誌
6 收集nginx日誌
1)建立名稱空間
cat namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
2)建立pod的configmap(保存filebeat的配置文件)
kubectl apply -f filebeat-nginx-configmap.yaml
cat filebeat-nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-nginx-config
namespace: test
data:
filebeat.yml: |-
filebeat.prospectors:
type: log
paths:
type: log
paths:
output.logstash: #收集的日誌數據輸出到logstash中
hosts: ['192.168.1.143:5044']
3)建立secret用於保存harbor用戶的密碼
kubectl create secret docker-registry harborsecret123 --docker-server=192.168.1.143 --docker-username='zhanghai' --docker-password='123@Abcde' -n test
4)修改logstash配置
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash-to-es.conf &
cat logstash-to-es.conf
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}"
}
}
}
}
}
5)建立nginx的pod
kubectl apply -f nginx-deployment.yaml
cat nginx-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: php-demo
namespace: test
spec:
replicas: 3
selector:
matchLabels:
project: www
app: php-demo
template:
metadata:
labels:
project: www
app: php-demo
spec:
imagePullSecrets:
name: nginx
image: 192.168.1.143/project/php-demo:latest
imagePullPolicy: Always
ports:
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: nginx-logs
mountPath: /usr/local/nginx/logs
volumes:
5)配置kibana前臺
kibana界面->Kibana Index Patterns-》nginx-access-*->@timestamp->Create index pattern->Discover
data:
filebeat.yml: |-
filebeat.prospectors:
2)建立tomcat的pod
kubectl apply -f tomcat-deployment.yaml
cat tomcat-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: tomcat-java-demo
namespace: test
spec:
replicas: 3
selector:
matchLabels:
project: www
app: java-demo
template:
metadata:
labels:
project: www
app: java-demo
spec:
imagePullSecrets:
name: tomcat
image: 192.168.1.143/project/tomcat-java-demo:latest
imagePullPolicy: Always
ports:
3)配置kibana前臺
kibana界面->Kibana Index Patterns-tomcat-catalina-*->@timestamp->Create index pattern->Discover
特別聲明:本文素材來源於網絡,僅做爲分享學習之用,若有侵權,請聯繫刪除!