Ports and metrics
About 931 words
A flow without ports is allowed on every port. That works, but it lets a compromised client try every port on the destination, so the policy warns until the catalog names them. This guide covers specht-labs.de/cilium-ports, where each flow takes its ports from, and the separate metrics ports a scraper reaches. The examples come from docs/examples/prod.
Name the ports
metadata:
annotations:
specht-labs.de/cilium-ports: tcp:8080The value is a comma-separated list. Each entry is a port or a range, with an optional protocol:
| Entry | Means | Renders to |
|---|---|---|
tcp:443 | TCP port 443 | port: "443", protocol: TCP |
udp:53 | UDP port 53 | port: "53", protocol: UDP |
443 | port 443, any protocol | port: "443", protocol: ANY |
tcp:8000-8100 | a range | port: "8000", endPort: 8100, protocol: TCP |
udp:* | every UDP port | port: "0", protocol: UDP |
* | every port of every protocol | no toPorts at all |
sctp: and any: work as protocols too. Port syntax has the grammar.
The port is the one the pod listens on: Cilium enforces the policy after a Service has translated its port to the pod's. A Service that listens on 80 in front of a container on 8080 needs tcp:8080.
Where a flow's ports come from
| Flow | Ports |
|---|---|
| to the providers of a consumed API | the API's cilium-ports, else the provider's |
| to a Resource the endpoint depends on | the Resource's |
| from a gateway to an endpoint exposed through it | the endpoint's |
to an external destination (cilium-dst) | the API's, the provider's or the dependency's, as in External destinations |
from a cilium-allow source | the API's (when the annotation is on an API), else the endpoint's |
| from the metrics scraper | the endpoint's cilium-metrics-ports |
| from a rule entity | the rule's spec.ports |
| to the Tailscale egress proxies | the API's specht-labs.de/tailnet-ports |
| between an endpoint's own pods | every port |
An API's ports win over its provider's, so a Component that serves two APIs on two ports gives each consumer only the port of the API it consumes. Put the ports on the API when there is one, and on the Component or Resource for everything that reaches it without an API: gateways, cilium-allow sources, dependents.
The TURN relay of the example takes TCP on 3478 and UDP on any port, from the internet:
metadata:
name: turn
annotations:
backstage.io/kubernetes-namespace: turn
backstage.io/kubernetes-label-selector: app=coturn
specht-labs.de/cilium-ports: tcp:3478, udp:*
specht-labs.de/cilium-allow: entity:worldingress:
# from entity:world
# allowed by component:turn (specht-labs.de/cilium-allow)
- fromEntities:
- world
toPorts:
- ports:
- port: "0"
protocol: UDP
- port: "3478"
protocol: TCPudp:* renders as port 0 with protocol UDP, which Cilium reads as every port of that protocol. * alone renders as no port restriction at all.
Merged ports
When several flows reach the same peer, they share one rule, and their ports are merged: duplicates and entries another one covers (tcp:443 under 443, a range inside a larger one) are dropped, and the rest are sorted by port. A flow without ports absorbs the others; the merged rule is then open on every port. udp:* stays an entry of its own, since it only covers UDP.
The warning
warning: component:default/backend: reached on every port by 1 flow: give it (or the APIs it provides) specht-labs.de/cilium-portsIt counts the flows that reach an endpoint without ports, apart from its own pods' and rule entities' (a rule without ports means every port on purpose). A malformed entry is an error of its own, and the whole annotation is ignored until it's fixed:
error: component:default/backend: specht-labs.de/cilium-ports: not ports: tcp:httpNamed ports such as http aren't supported; use the number.
Metrics ports
A metrics scraper such as Prometheus reaches nearly every endpoint, on a port that has nothing to do with the endpoint's API. Instead of a relation from the scraper to everything, name the scraper once in the policy:
apiVersion: specht-labs.de/v1alpha1
kind: Cilium
metadata:
name: prod
spec:
type: policy
owner: group:platform
cluster: resource:prod
metrics:
scraper: component:prometheusand the metrics port on each endpoint:
metadata:
name: orders
annotations:
backstage.io/kubernetes-namespace: orders
backstage.io/kubernetes-label-selector: app=orders
specht-labs.de/cilium-ports: tcp:8080
specht-labs.de/cilium-metrics-ports: tcp:9090The scraper reaches every endpoint that has cilium-metrics-ports, on those ports and no others:
egress:
# to component:orders
# component:prometheus scrapes its metrics (specht-labs.de/cilium-metrics-ports)
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: orders
app: orders
toPorts:
- ports:
- port: "9090"
protocol: TCP
# to resource:shop-db
# component:prometheus scrapes its metrics (specht-labs.de/cilium-metrics-ports)
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: shop
cnpg.io/cluster: shop-db
toPorts:
- ports:
- port: "9187"
protocol: TCPand each endpoint's policy has the matching ingress, commented component:prometheus scrapes its metrics (specht-labs.de/cilium-metrics-ports).
The scraper must be an endpoint itself: a Component (the default kind) or Resource with pods on the cluster. If it isn't, the policy has an error, spec.metrics.scraper: component:default/grafana-agent has no pods on resource:default/lab, and no metrics flows. An endpoint without cilium-metrics-ports isn't scraped.
Scrapers that run on the host network, such as some node agents, aren't pods to Cilium; their traffic comes from host or remote-node. Allow those on the endpoint with cilium-allow instead.
