Apply Plugins to L4 Routes
Learn how to attach APISIX stream plugins to Gateway API TCPRoute, UDPRoute, and TLSRoute resources by configuring and verifying an L4RoutePolicy.
L4RoutePolicy attaches APISIX stream plugins to Gateway API TCP, UDP, and TLS routes. This guide applies an IP restriction to a TCPRoute and verifies that the policy blocks connections.
Prerequisites
- Complete Proxy TCP Traffic by Port using the Gateway API tab. The example creates the
stream-route-mysqlTCPRoute in theaicnamespace. - Verify that you can connect to the MySQL backend before applying the policy.
Apply a Stream Plugin
Create an L4RoutePolicy that attaches the ip-restriction stream plugin to stream-route-mysql. The blacklist in this example blocks all IPv4 client addresses:
apiVersion: apisix.apache.org/v1alpha1
kind: L4RoutePolicy
metadata:
name: block-mysql-connections
namespace: aic
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: TCPRoute
name: stream-route-mysql
plugins:
- name: ip-restriction
config:
blacklist:
- 0.0.0.0/0Apply the policy:
kubectl apply -f l4-route-policy.yamlVerify the Policy
Check the policy status:
kubectl get l4routepolicy block-mysql-connections -n aic \
-o jsonpath='{range .status.ancestors[*].conditions[*]}{.type}{"="}{.status}{" ("}{.reason}{")\n"}{end}'The accepted policy should report:
Accepted=True (Accepted)Try to connect to MySQL again:
mysqlsh --sqlc --host=127.0.0.1 --port=9100 --user=root --passwordThe connection should fail because the plugin rejects the client address.
Delete the policy and confirm that the connection succeeds again:
kubectl delete l4routepolicy block-mysql-connections -n aicPolicy Attachment Rules
- A policy can target
TCPRoute,UDPRoute, orTLSRouteresources in the same namespace as the policy. targetRefsmust use thegateway.networking.k8s.iogroup. One policy can contain between 1 and 16 target references.- L4 routes do not expose addressable sections. A target reference with
sectionNamedoes not attach. - Only APISIX stream plugins are valid. For example, use
ip-restrictionorlimit-conn; HTTP-only plugins cannot run on L4 routes. - If multiple policies target the same route, the oldest policy is applied. Later policies report
Accepted=Falsewith theConflictedreason.