GoAccess 是一款专为快速、终端化日志分析设计的工具。其核心设计理念是无需浏览器即可实时快速分析并查看 Web 服务器统计数据 —— 这一特性尤为实用:无论是通过 SSH 快速分析访问日志,还是你本身偏好终端工作流,都能轻松适配。
终端输出来为默认呈现方式,同时它还支持生成功能完整、独立运行的实时 HTML 报告(适用于数据分析、监控及数据可视化场景),此外也可导出 JSON 和 CSV 格式的报告文件。
HTMl报告样例
包括最后1000行访问记录,按天的访问流量(MB)、请求的URL频率统计:
客户端IP访问统计、客户端的操作系统统计
点击率统计、请求的HTTP状态码统计
部署Goaccess到Kubernetes平台
本例部署的Goaccess服务,将实时分析nginx的访问日志,生成HTML报告。
主要部署架构为:
- goaccess持续监控主机上的nginx访问日志文件,实时生成HTML报告。
- nginx容器将通过共享存储卷读取goaccess生成的HTML报告,提供html页面访问。
以下架构图直观展示了GoAccess与Nginx容器在Kubernetes环境中的协作流程:
Kubernetes编排文件配置
1. GoAccess Deployment (goaccess-deployment.yaml)
kind: Deployment
apiVersion: apps/v1
metadata:
name: goaccess
namespace: goaccess
spec:
replicas: 1
selector:
matchLabels:
app: goaccess
template:
metadata:
labels:
app: goaccess
spec:
volumes:
- name: nginx-logs
hostPath:
path: /data/nginx/logs
type: Directory
- name: nginx-config
configMap:
name: nginx-config
items:
- key: default.conf
path: default.conf
defaultMode: 420
- name: report-volume
emptyDir: {}
- name: time-vol
hostPath:
path: /etc/localtime
type: ''
containers:
- name: nginx
image: 'nginx:v1.29.1'
ports:
- containerPort: 80
protocol: TCP
resources: {}
volumeMounts:
- name: report-volume
readOnly: true
mountPath: /usr/share/nginx/html
- name: nginx-config
readOnly: true
mountPath: /etc/nginx/conf.d
- name: time-vol
readOnly: true
mountPath: /etc/localtime
imagePullPolicy: IfNotPresent
- name: goaccess
image: 'goaccess:1.9.4-arm64'
command:
- /bin/sh
args:
- '-c'
- >-
tail -F /var/log/nginx/access.log | goaccess -
--log-format=COMBINED -o /goaccess-report/report.html
--real-time-html --port=7890 --addr=0.0.0.0
--ws-url=ws://${your_ip_address}:31367/ws/
ports:
- containerPort: 7890
protocol: TCP
env:
- name: LANG
value: en_US.UTF-8
resources: {}
volumeMounts:
- name: nginx-logs
readOnly: true
mountPath: /var/log/nginx
- name: report-volume
mountPath: /goaccess-report
- name: time-vol
readOnly: true
mountPath: /etc/localtime
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
nodeName: ${nginx代理所在主机名}
配置说明:
- 创建一个包含Nginx和GoAccess两个容器的Pod
- 使用
hostPath卷挂载主机上的Nginx日志目录 - 使用
emptyDir卷作为共享存储,GoAccess生成HTML报告,Nginx提供访问 - GoAccess容器实时监控Nginx日志并生成实时HTML报告
--ws-url=ws://${your_ip_address}:31367/ws/report.html中WebSocket通信,用于实现实时更新,因为我们在Nginx配置中设置了proxy_pass http://127.0.0.1:7890/来代理websocket请求,所以使用了Nginx的NodePort端口31367. 默认会访问ws://$ip:7890,很显然浏览器访问不到k8s环境内部端口。
2. GoAccess Service (goaccess-service.yaml)
kind: Service
apiVersion: v1
metadata:
name: goaccess-service
namespace: goaccess
labels:
app: goaccess
component: goaccess-reporting
spec:
ports:
- name: http-report
protocol: TCP
port: 80
targetPort: 80
nodePort: 31367
selector:
app: goaccess
type: NodePort
配置说明:
- 提供两个服务端口:80端口用于HTML报告访问,7890端口用于GoAccess管理界面
- 使用NodePort类型,外部可通过节点IP和指定端口访问服务
- 通过标签选择器关联到GoAccess Deployment
3. Nginx ConfigMap (nginx-configmap.yaml)
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
namespace: goaccess
data:
default.conf: |
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index report.html;
location / {
try_files $uri $uri/ =404;
autoindex off;
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
location /ws/ {
# 代理到 GoAccess 容器的实时服务器
proxy_pass http://127.0.0.1:7890/; # 关键点1:使用 localhost
# 必须的头部,用于升级协议到 WebSocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 传递必要的主机信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 重要:调整超时设置以适应长连接
proxy_read_timeout 86400s; # WebSocket连接可能保持很久
proxy_send_timeout 86400s;
}
# 启用 gzip 压缩
gzip on;
gzip_types text/html text/css application/javascript;
}
配置说明:
- 配置Nginx服务器,根目录指向共享存储中的HTML报告
- 设置默认索引文件为report.html
- 配置WebSocket代理,支持GoAccess的实时更新功能