Skip to content

Commit 9f13fa7

Browse files
authored
feat: Varnish NCSA frontend/backend logs (#129)
1 parent 61a4279 commit 9f13fa7

4 files changed

Lines changed: 125 additions & 10 deletions

File tree

charts/varnish/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ name: varnish
1111
sources:
1212
- https://varnish-cache.org/
1313
- https://artifacthub.io/packages/helm/varnish/varnish-cache
14-
version: 1.0.2
14+
version: 1.0.3

charts/varnish/templates/configmaps.yaml

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,70 @@ metadata:
4747
data:
4848
default.vcl: |
4949
vcl 4.1;
50-
50+
5151
probe health {
5252
.url = "/";
5353
.timeout = 2s;
5454
.interval = 5s;
5555
.window = 5;
5656
.threshold = 3;
57-
}
58-
59-
backend server_nginx_0 {
57+
}
58+
59+
backend default {
6060
.host = "localhost";
6161
.port = "8080";
6262
.probe = health;
6363
}
64+
65+
sub vcl_recv {
66+
# Force a cache miss to ensure backend requests
67+
return (pass);
68+
}
69+
70+
sub vcl_backend_response {
71+
set beresp.ttl = 10s;
72+
}
73+
74+
sub vcl_deliver {
75+
if (obj.hits > 0) {
76+
set resp.http.X-Cache = "HIT";
77+
} else {
78+
set resp.http.X-Cache = "MISS";
79+
}
80+
}
81+
---
82+
apiVersion: v1
83+
kind: ConfigMap
84+
metadata:
85+
name: alloy-varnish-log-config
86+
labels:
87+
alloy-config: varnish-logs
88+
data:
89+
config.alloy: |
90+
logging {
91+
level = "info"
92+
format = "logfmt"
93+
}
94+
95+
loki.write "logs_integrations" {
96+
endpoint {
97+
url = "http://loki.default.svc.cluster.local:3100/loki/api/v1/push"
98+
99+
}
100+
external_labels = {"cluster" = "my-cluster"}
101+
}
102+
103+
local.file_match "logs_integrations_integrations_varnish_cache" {
104+
path_targets = [{
105+
__address__ = "localhost",
106+
__path__ = "/var/log/varnish/varnishncsa-*.log",
107+
instance = "custom-varnish-cache.sample-apps.svc.cluster.local:9131",
108+
job = "integrations/varnish-cache",
109+
}]
110+
}
111+
112+
loki.source.file "logs_integrations_integrations_apache_airflow" {
113+
targets = local.file_match.logs_integrations_integrations_varnish_cache.targets
114+
forward_to = [loki.write.logs_integrations.receiver]
115+
}
116+
---

charts/varnish/templates/deployment.yaml

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ spec:
2222
spec:
2323
serviceAccountName: custom-varnish-cache
2424
securityContext:
25-
fsGroup: 999
25+
fsGroup: 101
2626
shareProcessNamespace: true
27+
initContainers:
28+
- name: init-script
29+
image: alpine:latest
30+
securityContext:
31+
runAsNonRoot: true
32+
runAsUser: 103
33+
command: ['sh', '-c', 'touch /var/log/varnish/varnishncsa-frontend.log /var/log/varnish/varnishncsa-backend.log && chmod 666 /var/log/varnish/*.log']
34+
volumeMounts:
35+
- name: varnish-log
36+
mountPath: /var/log/varnish
2737
containers:
2838
- name: nginx
2939
image: {{ .Values.nginxImage }}
@@ -37,7 +47,7 @@ spec:
3747
- name: varnish-cache
3848
securityContext:
3949
runAsNonRoot: true
40-
runAsUser: 1002
50+
runAsUser: 103
4151
image: {{ .Values.varnishImage }}
4252
imagePullPolicy: IfNotPresent
4353
ports:
@@ -59,7 +69,9 @@ spec:
5969
mountPath: "/etc/varnish/default.vcl"
6070
subPath: default.vcl
6171
- name: custom-varnish-vsm
62-
mountPath: /var/lib/varnish
72+
mountPath: /var/lib/varnish
73+
- name: varnish-log
74+
mountPath: /var/log/varnish
6375
- name: exporter
6476
command:
6577
- "/prometheus_varnish_exporter"
@@ -73,7 +85,49 @@ spec:
7385
subPath: secret
7486
ports:
7587
- containerPort: 9131
76-
name: prometheus
88+
name: prometheus
89+
- name: varnish-cache-ncsa-frontend
90+
securityContext:
91+
runAsNonRoot: true
92+
runAsUser: 103
93+
image: {{ .Values.varnishImage }}
94+
imagePullPolicy: IfNotPresent
95+
command: ["/bin/sh", "-c"]
96+
args: ["varnishncsa -n /var/lib/varnish -F '{\"Timestamp\": \"%t\", \"Varnish-Side\": \"%{Varnish:side}x\", \"Age\": %{age}o, \"Handling\": \"%{Varnish:handling}x\", \"Request\": \"%r\", \"Status\": \"%s\", \"Response-Reason\": \"%{VSL:RespReason}x\", \"Fetch-Error\": \"%{VSL:FetchError}x\", \"X-Forwarded-For\": \"%{x-forwarded-for}i\", \"Remote-User\": \"%u\", \"Bytes\": \"%b\", \"Time-To-Serve\": %D, \"User-Agent\": \"%{User-agent}i\", \"Referer\": \"%{Referer}i\", \"X-Varnish\": \"%{x-varnish}o\", \"X-Magento-Tags\": \"%{x-magento-tags}o\"}' -w /var/log/varnish/varnishncsa-frontend.log"]
97+
volumeMounts:
98+
- name: custom-varnish-vsm
99+
mountPath: /var/lib/varnish
100+
readOnly: true
101+
- name: varnish-secret
102+
mountPath: /etc/varnish/secret
103+
subPath: secret
104+
- name: varnish-log
105+
mountPath: /var/log/varnish
106+
- name: varnish-cache-ncsa-backend
107+
securityContext:
108+
runAsNonRoot: true
109+
runAsUser: 103
110+
image: {{ .Values.varnishImage }}
111+
imagePullPolicy: IfNotPresent
112+
command: ["/bin/sh", "-c"]
113+
args: ["varnishncsa -n /var/lib/varnish -b -F '{\"Timestamp\": \"%t\", \"Varnish-Side\": \"%{Varnish:side}x\", \"Handling\": \"%{Varnish:handling}x\", \"Request\": \"%r\", \"Status\": \"%s\", \"Response-Reason\": \"%{VSL:RespReason}x\", \"Fetch-Error\": \"%{VSL:FetchError}x\", \"Bytes\": \"%b\", \"Time-To-Serve\": %D}' -w /var/log/varnish/varnishncsa-backend.log"]
114+
volumeMounts:
115+
- name: custom-varnish-vsm
116+
mountPath: /var/lib/varnish
117+
readOnly: true
118+
- name: varnish-secret
119+
mountPath: /etc/varnish/secret
120+
subPath: secret
121+
- name: varnish-log
122+
mountPath: /var/log/varnish
123+
- name: alloy-varnish-logs
124+
image: grafana/alloy:latest
125+
volumeMounts:
126+
- name: varnish-log
127+
mountPath: /var/log/varnish
128+
- name: alloy-varnish-log-config
129+
mountPath: /etc/alloy/config.alloy
130+
subPath: config.alloy
77131
volumes:
78132
- name: custom-config
79133
emptyDir:
@@ -90,6 +144,14 @@ spec:
90144
- name: nginx-config
91145
configMap:
92146
name: nginx-config
147+
- name: varnish-log
148+
emptyDir: {}
149+
- name: alloy-varnish-log-config
150+
configMap:
151+
name: alloy-varnish-log-config
152+
items:
153+
- key: config.alloy
154+
path: config.alloy
93155
affinity:
94156
podAntiAffinity:
95157
requiredDuringSchedulingIgnoredDuringExecution:

charts/varnish/templates/service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ spec:
1313
ports:
1414
- name: http
1515
port: 80
16-
targetPort: 6081
16+
targetPort: 80
1717
- name: varnish-exporter
1818
port: 9131
1919
targetPort: 9131

0 commit comments

Comments
 (0)