Tailnet egress
About 770 words
If some of your APIs live on a Tailscale tailnet, pods usually reach them through the Tailscale Kubernetes operator's egress proxies: a Service in the consumer's namespace points at a proxy pod in the operator's namespace, and the proxy forwards to the tailnet. Under the default deny, both hops need an allow. This guide sets that up. The examples come from docs/examples/prod.
It reads the annotations of the sister plugin, Tailscale for Backstage, but doesn't need it installed.
1. Mark the API as being on the tailnet
The API says where it is on the tailnet and on which ports, with the Tailscale plugin's annotations:
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: inventory-api
annotations:
specht-labs.de/tailnet-dst: svc:inventory
specht-labs.de/tailnet-ports: tcp:443
spec:
type: openapi
lifecycle: production
owner: group:warehouse
definition: '{}'The plugin only uses it when nothing else says where the API is: no provider on the cluster, and no specht-labs.de/cilium-dst on the API or its providers.
2. Tell the policy where the proxies are
apiVersion: specht-labs.de/v1alpha1
kind: Cilium
metadata:
name: prod
spec:
type: policy
owner: group:platform
cluster: resource:prod
tailnetEgress:
namespace: tailscale
selector: tailscale.com/parent-resource-type=svc
consumerNamespaceLabel: tailscale.com/parent-resource-ns| Field | Meaning |
|---|---|
namespace | where the operator runs its proxies |
selector | which pods in it are egress proxies; every pod in the namespace if unset |
consumerNamespaceLabel | a label on each proxy that holds the namespace of the Service it serves. With it, a consumer only reaches the proxies of Services in its own namespace |
The operator labels each proxy pod with the kind, namespace and name of the resource it serves, which is what the values above select on. Check the labels on your proxies with kubectl -n tailscale get pods --show-labels.
Without tailnetEgress, a consumption of a tailnet API isn't allowed, and the consumer gets a warning that ends in (it is on the tailnet, but the policy has no tailnetEgress): not allowed.
What renders
The shop consumes inventory-api. Its policy gets an egress rule to the proxies of Services in its own namespace, on the API's tailnet-ports:
egress:
# to tailnet svc:inventory
# component:shop consumes api:inventory-api
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: tailscale
tailscale.com/parent-resource-type: svc
tailscale.com/parent-resource-ns: shop
toPorts:
- ports:
- port: "443"
protocol: TCPThe proxies are pods too, and under the default deny they need an ingress from their consumers. The plugin renders one policy per consumer namespace for it:
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: allow-tailnet-egress-shop
labels:
app.kubernetes.io/managed-by: backstage
annotations:
specht-labs.de/cilium-policy: cilium:default/prod
spec:
description: "The Tailscale egress proxies of Services in shop: their consumers reach them on the way to the tailnet."
endpointSelector:
matchLabels:
io.kubernetes.pod.namespace: tailscale
tailscale.com/parent-resource-type: svc
tailscale.com/parent-resource-ns: shop
enableDefaultDeny:
ingress: false
egress: false
ingress:
# from component:shop, toward the tailnet
# component:shop consumes api:inventory-api (svc:inventory)
- fromEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: shop
app: shop
toPorts:
- ports:
- port: "443"
protocol: TCPThe console and backstage-cilium-render show the destination as tailnet svc:inventory.
3. Let the proxies reach the tailnet
The plugin allows the hop into the proxies, not the proxies' own traffic to the tailnet: Tailscale's coordination server, its relays and other nodes. Pick one:
- Leave the operator's namespace open. Add it to
spec.defaultDeny.exclude. The proxies then accept and send anything, and theallow-tailnet-egress-…policies only add to that. This is the simple choice. - Model the proxies. Add a Component for the operator with
backstage.io/kubernetes-namespace: tailscale,specht-labs.de/cilium-selector: '*'and adependsOnon the cluster, so that it's an endpoint covering the proxies, and a rule entity from it toentity:worldon the ports Tailscale uses. The proxies are then under the default deny like everything else.
Limits
consumerNamespaceLabelneeds to know the consumer's namespace, so it's only added for consumers with exactly one. A consumer spread over several namespaces reaches every egress proxy, and gets noallow-tailnet-egress-…ingress. That's fine when the operator's namespace is left open; otherwise allow it with a rule entity to the Component that covers the proxies.- The plugin doesn't check that a Service and proxy for the API exist in the consumer's namespace. If the shop's namespace has no egress Service for
svc:inventory, the flow is allowed and leads nowhere.
