I'm experimenting with spinning up a Varnish instance to speed up a slow but static endpoint of a service. The service is already running in a Kubernetes cluster so I'm basing the setup on the official docker image and following the advice of baking the VCL file into the image:
FROM varnish:6.2
COPY default.vcl /etc/varnish/
I'm starting with a small amount of configuration in default.vcl
:
vcl 4.0;
backend default {
.host = "172.17.0.1:8018"; # IP for local experimentation
}
# only cache '/v1/xyz/...'
sub vcl_recv {
if (req.url ~ "^/v1/xyz/") {
return (hash);
}
return (pass);
}
I would like to be able to declare the target backend in the deployment file, either using env vars or cli args.
The -b
flag seemed perfect for the job, but fails with Error: Only one of -b or -f can be specified
.
And using std.getenv
in the backend
block doesn't work either:
import std;
backend default {
.host = std.getenv("VARNISH_TAGET_HOST");
}
results in
Expected CSTR got 'std'
(program line 369), at
('/etc/varnish/default.vcl' Line 6 Pos 17)
.host = std.getenv("VARNISH_TAGET_HOST");
----------------###------------------------------
Is there some way (not including sed
-like hacks) by which I can configure the backend without hardcoding it into the VCL?
Varnish Cache, the open source version of Varnish, only allows static backend definitions.
The only way you can define backends on-the-fly, is by using Varnish Enterprise, the commercial version of the software.
See https://docs.varnish-software.com/varnish-cache-plus/vmods/goto/ for more information about the dynamic backends feature.
Apparently the -b
parameter is a shorthand for the following command:
varnishadm vcl.inline boot << EOF
vcl 4.1;
backend default {
.host = "<addr>";
}
EOF
So in fact -b
already creates and loads VCL in the background, which makes this option mutually exclusive with -f
If by "on-the-fly" you mean on runtime then that's not what I need. I know the backend on boot time, I would just like to configure it as a parameter instead of having it hardcoded.
The only way around this is with a VCL pre-processor: some script that replaces a placeholder in the backend definition of your VCL file before
varnishd
is booted.Ok thanks. Do you know if that's an intentional limitation, i.e. why I can't just use
-b
and-f
together?I'll look into it asap.
I edited my answer, and added some more information about the mutual exclusivity of
-b
&-f
.