赞
踩
目录
1、创建“filebeat-nginx-configmap.yaml”文件配置FileBeat参数
2、创建“nginx-deployment.yaml”文件部署Nginx应用
我们这里底层的容器引擎使用的是Docker,且宿主机上的systemd服务可用。因此,我们这里k8s集群系统的日志文件在宿主机的“/var/log”目录下
在kubernetes集群中部署的应用(如tomcat、nginx)在运行过程中产生的日志
Elasticsearch + Logstash + Kibana
是一个数据实时传输管道:能够将数据实时地从输入端传输到输出端,还能够根据实际的需求在传输过程中加入过滤器来筛选数据。在日志系统中,常被作为日志采集工具使用
是一个开源的全文搜索和分析引擎:能够快速地存储、搜集和分析数据。可以被看成一个非关系型数据库。在日志系统中,常被作为存储和搜索日志的工具使用。
开源的分析与可视化平台。在日志系统中,常作为Elasticsearch的输出端使用。用户可以使用Kibana搜索、查看Elasticsearch中的数据,并以不同的方式(如图表、表格、地图等)进行展现。
下面将Elasticsearch、Logstash、Kibana都安装在master节点上。
在安装与配置ELK时,建议不要使用root用户,可以创建一个普通用户来进行
链接:https://pan.baidu.com/s/1iuN7HZU1kg1Kv55WW1q-Jg
提取码:4ns0
elk/elk
[root@k8s-master ~]# useradd elk
[root@k8s-master ~]# passwd elk
更改用户 elk 的密码 。新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
#解压jdk包
[elk@k8s-master ~]$ tar -zxvf jdk-8u401-linux-x64.tar.gz
#编辑“~/.bash_profile” 追加如下内容:JAVA_HOME=/home/elk/jdk1.8.0_401
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
#生效文件
[elk@k8s-master ~]$ source ~/.bash_profile
#解压Elasticsearch
[elk@k8s-master ~]$ tar -zxvf elasticsearch-6.8.7.tar.gz
#启动Elasticsearch
[elk@k8s-master ~]$ elasticsearch-6.8.7/bin/elasticsearch &
#测试是否启动成功
[elk@k8s-master ~]$ curl localhost:9200
如下,Elasticsearch启动成功
#解压安装包
[elk@k8s-master ~]$ tar -zxvf kibana-6.8.7-linux-x86_64.tar.gz
#编辑kibana-6.8.7-linux-x86_64/config/kibana.yml 文件---
server.port: 5601
# 需要写成master节点的IP地址
server.host: "172.19.3.240"
elasticsearch.hosts: ["http://localhost:9200"]---
#启动Kibana
[elk@k8s-master ~]$ kibana-6.8.7-linux-x86_64/bin/kibana &
# 输出如下信息启动成功
# 访问控制台
# 解压logstash
[elk@k8s-master ~]$ tar -zxvf logstash-6.8.7.tar.gz
#启动命令[elk@k8s-master ~]$ cd logstash-6.8.7/
[elk@k8s-master logstash-6.8.7]$ bin/logstash -f config/logstash.conf &
方式一:node节点上部署专门的日志收集程序。在node节点上部署一个日志收集代理来收集该节点的日志信息。为了保证每个node节点都能够运行这样一个代理,可以采用DaemonSet方式来运行代理。如上图所示:将宿主机的容器日志挂载到DaemonSet中,然后由日志收集代理将日志转发到后端的日志收集系统ELK中
apiVersion: v1 kind: ConfigMap metadata: name: k8s-logs-filebeat-config namespace: kube-system data: filebeat.yml: |- filebeat.prospectors: - type: log paths: - /messages # filebeat中的日志位置 fields: # 给日志打的2个标签 app: k8s type: module fields_under_root: true output.logstash: # filebeat中日志给logstash hosts: ['172.19.3.240: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: collenzhao/filebeat:6.5.4 args: [ "-c","/etc/filebeat.yml" ] 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 # 采集宿主机/var/log/messages 到filebeat容器中 /messages中 mountPath: /messages volumes: - name: k8s-logs hostPath: path: /var/log/messages type: File - name: filebeat-config configMap: name: k8s-logs-filebeat-config
[K8S@k8s-master k8s-log]$ kubectl apply -f k8s-log.yaml
configmap/k8s-logs-filebeat-config created
daemonset.apps/k8s-logs created查看启动成功
根据需求,将filebeat中指定fields字段分类传给Elasticsearch
[elk@k8s-master ~]$ cat logstash-6.8.7/config/logstash.conf
# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.input {
beats {
port => 5044
}
}output {
if [app] == "k8s" { # filebeat中的field字段
if [type] == "module" {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "k8s-log-%{+YYYY.MM.dd}" #打包到日志名称到elasticsearch中
}
}
}
}
重启Logstash
[elk@k8s-master ~]$ cd logstash-6.8.7/
[elk@k8s-master logstash-6.8.7]$ ./bin/logstash -f ./config/logstash.conf &
点击message后边都add,会被添加到上边的Selected field中,可以方便我们查看日志
方式二:Pod中增加专用的日志收集容器
核心是在每个运行应用的Pod中增加一个新的容器,该容器专门用于运行日志收集代理程序。应用可以通过数据卷的方式将日志挂碍到这个容器中,最终将日志转发到后端的日志收集系统ELK中
[K8S@k8s-master nginx]$ cat filebeat-nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-nginx-config
data:
filebeat.yml: |-
filebeat.prospectors:
- type: log
paths:
- /usr/local/nginx/logs/access.log # filebeat中日志文件位置
fields: # 给日志文件的标签
app: nginx
type: nginx-access
fields_under_root: true
- type: log
paths:
- /usr/local/nginx/logs/error.log
fields: # 给日志文件的标签
app: nginx
type: nginx-error
fields_under_root: true
output.logstash:
hosts: ['172.19.3.240:5044']
[K8S@k8s-master nginx]$ cat filebeat-nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-nginx-config
data:
filebeat.yml: |-
filebeat.prospectors:
- type: log
paths:
- /usr/local/nginx/logs/access.log
fields:
app: nginx
type: nginx-access
fields_under_root: true
- type: log
paths:
- /usr/local/nginx/logs/error.log
fields:
app: nginx
type: nginx-error
fields_under_root: true
output.logstash:
hosts: ['172.19.3.240:5044'][K8S@k8s-master nginx]$ cat nginx-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-test-service
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
selector:
app: nginx
type: nginx-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx
type: nginx-demo
template:
metadata:
labels:
app: nginx
type: nginx-demo
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginx-logs
mountPath: /var/log/nginx
- name: filebeat
image: collenzhao/filebeat:6.5.4
args: [
"-c", "/etc/filebeat.yml",
"-e",
]
volumeMounts:
- name: filebeat-config
mountPath: /etc/filebeat.yml
subPath: filebeat.yml
- name: nginx-logs
mountPath: /usr/local/nginx/logs
volumes:
- name: nginx-logs # 通过临时数据卷,将nginx容器中日志挂在到filebeat容器中
emptyDir: {}
- name: filebeat-config
configMap:
name: filebeat-nginx-config
[K8S@k8s-master nginx]$ kubectl apply -f filebeat-nginx-configmap.yaml
[K8S@k8s-master nginx]$ kubectl apply -f nginx-deployment.yaml查看应用启动成功,为了生成access.log文件,我们访问下应用即可 172.19.3.240:31535
[elk@k8s-master config]$ cat logstash.conf
# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.input {
beats {
port => 5044
}
}output {
if [app] == "k8s" {
if [type] == "module" {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "k8s-log-%{+YYYY.MM.dd}"
}
}
}
if [app] == "nginx" {
if [type]== "nginx-access" {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}
if [type]== "nginx-error" {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "nginx-error-%{+YYYY.MM.dd}"
}
}
}
}
获取进程号,关闭进程
[elk@k8s-master config]$ ps -ef | grep logstash
elk 28634 34503 0 15:39 pts/1 00:00:00 grep --color=auto logstash
elk 61123 1 7 14:54 ? 00:03:28 /home/elk/jdk1.8.0_401/b..................................省略..........#关闭进程
[elk@k8s-master config]$ kill -9 61123
#再次查看进程关闭
[elk@k8s-master config]$ ps -ef | grep logstash
elk 44799 34503 0 15:41 pts/1 00:00:00 grep --color=auto logstash
[elk@k8s-master config]$
启动logstash
[elk@k8s-master logstash-6.8.7]$ bin/logstash -f config/logstash.conf &
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。