Kubernetes reverse proxy
Last updated 2024-03-14
IMPORTANT
This guide only applies to Next-Gen WAF customers with access to the Next-Gen WAF control panel. If you have access to the Next-Gen WAF product in the Fastly control panel, you can only deploy the Next-Gen WAF with the Edge WAF deployment method.
In this example, the Next-Gen WAF agent runs in a sidecar container and proxies all incoming requests for inspection before sending them upstream to the application container.
Integrating the Next-Gen WAF agent
The Next-Gen WAF agent can be installed as a sidecar into each pod or as a service for some specialized needs.
The recommended way of installing the Next-Gen WAF agent in Kubernetes is by integrating the sigsci-agent
into a pod as a sidecar. This means adding the sigsci-agent
as an additional container to the Kubernetes pod. As a sidecar, the agent will scale with the app/service in the pod instead of having to do this separately. However, in some situations, it may make more sense to install the sigsci-agent
container as a service and scale it separately from the application.
The sigsci-agent
container can be configured in various ways depending on the installation type and module being used.
You can use the preStop
container hook to slow the pod's shutdown and ensure drain timeouts are met.
1preStop:2 exec:3 command:4 - sleep5 - "30"
By default, the agent prioritizes quick start up and performance readiness for preliminary inspection. However, quick startup isn't always desirable if you only want the agent to inspect traffic after loading your rules and configuration data. If you want to delay agent startup, consider configuring a startup probe.
Getting and updating the agent container image
An official signalsciences/sigsci-agent
container image is available on Docker Hub.
Alternatively, if you want to build your own image or need to customize the image, then follow the sigsci-agent build instructions.
These instructions reference the latest
version of the agent with imagePullPolicy: Always
, which will pull the latest agent version even if one already exist locally. This is so the documentation does not fall out of date and anyone using this will not have an agent that stays stagnant. However, this may not be what if you need to keep installations consistent or on a specific version of the agent. In these cases, you should specify an agent version. Images on Docker Hub are tagged with their versions and a list of versions is available on Docker Hub.
Whether you choose to use the latest
image or a specific version, there are a few items to consider to keep the agent up-to-date.
Using the latest
container image
If you do choose to use the latest
image, then you will want to consider how you will keep the agent up to date.
If you have used the
imagePullPolicy: Always
option, then the latest image will be pulled on each startup and your agent will continue to get updates.Alternatively, you may instead choose to manually update the local cache by periodically forcing a pull instead of always pulling on startup:
$ docker pull signalsciences/sigsci-agent:latestThen, use
latest
withimagePullPolicy: Never
set in the configuration so that pulls are never done on startup (only manually as above):1- name: sigsci-agent2 image: signalsciences/sigsci-agent:latest3 imagePullPolicy: Never4 ...
Using a versioned container image
To use a specific version of the agent, replace latest
with the agent version (represented here by x.xx.x
). You may also want to change imagePullPolicy: IfNotPresent
in this case as the image should not change.
1- name: sigsci-agent2 image: signalsciences/sigsci-agent:x.xx.x3 imagePullPolicy: IfNotPresent4 ...
This will pull the specified agent version and cache it locally. If you use this method, then it is recommended that you parameterize the agent image, using Helm or similar, so that it is easier to update the agent images later on.
Using a custom tag for the container image
It is also possible to apply a custom tag to a local agent image. To do this, pull the agent image (by version or use latest
), apply a custom tag, then use that custom tag in the configuration. You will need to specify imagePullPolicy: Never
so local images are only updated manually. After doing so, you will need to periodically update the local image to keep the agent up-to-date.
For example:
$ docker pull signalsciences/sigsci-agent:latest$ docker tag signalsciences/sigsci-agent:latest signalsciences/sigsci-agent:testing
Then use this image tag in the configuration:
1- name: sigsci-agent2 image: signalsciences/sigsci-agent:testing3 imagePullPolicy: Never4...
Configuring the agent container
Agent configuration is normally done via the environment. Most configuration options are available as environment variables. Environment variables names have the configuration option name all capitalized, prefixed with SIGSCI_
and any dashes (-) changed to underscores (_). For example, the max-procs option would become the SIGSCI_MAX_PROCS
environment variable. For more details on what options are available, see the Agent Configuration documentation.
The sigsci-agent
container has a few required options that need to be configured:
- Agent credentials (Agent Access Key and Agent Secret Key).
- A volume to write temporary files.
Agent credentials
The sigsci-agent
credentials are configured with two environment variables. These variables must be set or the agent will not start.
- SIGSCI_ACCESSKEYID: The Agent Access Key identifies which site (also known as workspace) in the Next-Gen WAF control panel that the agent is configured for.
- SIGSCI_SECRETACCESSKEY: The Agent Secret Key is the shared secret key to authenticate and authorize the agent.
Because of the sensitive nature of these values, we recommend you use the built in secrets
functionality of Kubernetes. With this configuration, the agent will pull the values from the secrets data instead of reading hardcoded values into the deployment configuration. This also makes any desired agent credential rotation easier to manage by having to change them in only one place.
Use the valueFrom
option instead of the value
option to use the secrets
functionality. For example:
1env:2 - name: SIGSCI_ACCESSKEYID3 valueFrom:4 secretKeyRef:5 # Update my-site-name-here to the correct site (workspace) name or similar identifier6 name: sigsci.my-site-name-here7 key: accesskeyid8 - name: SIGSCI_SECRETACCESSKEY9 valueFrom:10 secretKeyRef:11 # Update my-site-name-here to the correct site (workspace) name or similar identifier12 name: sigsci.my-site-name-here13 key: secretaccesskey
The secrets
functionality keeps secrets in various stores in Kubernetes. This guide uses the generic secret store in its examples, however any equivalent store can be used. Agent secrets can be added to the generic secret store using YAML similar to the following example:
1apiVersion: v12kind: Secret3metadata:4 name: sigsci.my-site-name-here5stringData:6 accesskeyid: 12345678-abcd-1234-abcd-1234567890ab7 secretaccesskey: abcdefg_hijklmn_opqrstuvwxy_z0123456789ABCD
This can also be created from the command line with kubectl
such as with the following example:
1$ kubectl create secret generic sigsci.my-site-name-here \2 --from-literal=accesskeyid=12345678-abcd-1234-abcd-1234567890ab \3 --from-literal=secretaccesskey=abcdefg_hijklmn_opqrstuvwxy_z0123456789ABCD
Additional information about Kubernetes secrets
functionality can be found in the Kubernetes documentation.
Agent temporary volume
For added security, we recommended the sigsci-agent
container be executed with the root filesystem mounted as read only. However, the agent still needs to write some temporary files such as the socket file for RPC communication and some periodically updated files such as geolocation data.
To accomplish this with a read only root filesystem, there needs to be a writeable volume mounted. This writeable volume can also be shared to expose the RPC socket file to other containers in the same pod.
The recommended way of creating a writeable volume is to use the builtin emptyDir
volume type. This is typically configured in the volumes
section of a deployment, as shown in the following example:
1volumes:2 - name: sigsci-tmp3 emptyDir: {}
Containers will then mount this volume at /sigsci/tmp
:
1volumeMounts:2 - name: sigsci-tmp3 mountPath: /sigsci/tmp
The default in the official agent container image is to have the temporary volume mounted at /sigsci/tmp
. If this needs to be moved for the agent container, then the following agent configuration options should also be changed from their defaults to match the new mount location:
rpc-address
defaults to/sigsci/tmp/sigsci.sock
shared-cache-dir
defaults to/sigsci/tmp/cache
Next-Gen WAF agent as a reverse proxy in front of a web application without the Next-Gen WAF module
If your web application does not support a Next-Gen WAF module (or you prefer not to install a module), then you can configure the sigsci-agent
container to run as a reverse proxy in front of the web application in the same pod.
To configure the Next-Gen WAF agent to run in reverse proxy mode in a sidecar container, you must:
Add the
sigsci-agent
container to the pod, configured in reverse proxy mode to:- listen for incoming requests (on a new port or by reconfiguring your application or Kubernetes service accordingly)
- proxy requests to your web application container
Add an
emptyDir{}
volume as a place for thesigsci-agent
to write temporary data.
The following configuration exposes an example application (helloworld
) on port 8000
, adding the sigsci-agent
as a reverse proxy listener on a new port 8001
with an upstream of the example web application port 8000
.
Add the Next-Gen WAF agent as a reverse proxy
1...2 containers:3 # Example helloworld app running on port 8000 without sigsci configured4 - name: helloworld5 image: signalsciences/example-helloworld:latest6 imagePullPolicy: IfNotPresent7 args:8 - localhost:80009 ports:10 - containerPort: 800011 # Next-Gen WAF agent running in reverse proxy mode (SIGSCI_REVPROXY_LISTENER configured)12 - name: sigsci-agent13 image: signalsciences/sigsci-agent:latest14 imagePullPolicy: Always15 env:16 - name: SIGSCI_ACCESSKEYID17 valueFrom:18 secretKeyRef:19 name: sigsci.my-site-name-here20 key: accesskeyid21 - name: SIGSCI_SECRETACCESSKEY22 valueFrom:23 secretKeyRef:24 name: sigsci.my-site-name-here25 key: secretaccesskey26 # Configure the revproxy listener to listen on a new port 800127 # forwarding to the app on the original port 8000 as the upstream28 - name: SIGSCI_REVPROXY_LISTENER29 value: "http:{listener='http://0.0.0.0:8001',upstreams='http://0.0.0.0:8000',access-log='/dev/stdout'}"30 ports:31 - containerPort: 800132 securityContext:33 # The sigsci-agent container should run with its root filesystem read only34 readOnlyRootFilesystem: true35 volumeMounts:36 # Default volume mount location for sigsci-agent writeable data37 # NOTE: Also change `SIGSCI_SHARED_CACHE_DIR` (default `/sigsci/tmp/cache`)38 # if mountPath is changed, but best not to change.39 - name: sigsci-tmp40 mountPath: /sigsci/tmp
NOTE
The above modification assumes that sigsci
secrets were added to the system.
Adding the Next-Gen WAF agent temp volume definition to the deployment
You must define the agent temp volume for use by the other containers in the pod. This example uses the builtin emptyDir: {}
volume type.
1...2 volumes:3 # Define a volume where sigsci-agent will write temp data and share the socket file,4 # which is required with the root filesystem is mounted read only5 - name: sigsci-tmp6 emptyDir: {}
Changing the service definition and adding the Next-Gen WAF agent as a reverse proxy
In the example above, the sigsci-agent
reverse proxy listens on a new port, leaving the original application listener in place. You may wish for requests to be routed to the sigsci-agent
at the original application port to make the agent addition as seamless as possible. One way to do this is to modify the Kubernetes service definition to route traffic to the sigsci-agent
reverse proxy listener port instead of directly to the web application.
Change the service definition to point to the Next-Gen WAF agent port
Change the service targetPort
from pointing directly to the application, to instead point to the sigsci-agent
reverse proxy listener port. The sigsci-agent
will then proxy to the application port:
1apiVersion: v12kind: Service3metadata:4 name: helloworld5 labels:6 app: helloworld7spec:8 ports:9 - name: http10 port: 800011 # Target is now sigsci-agent on port 800112 targetPort: 800113 selector:14 app: helloworld15 type: LoadBalancer
Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.