Your first policy
About 1184 words
This tutorial renders the network policy of a small cluster: a web shop that consumes an orders API and keeps its data in a PostgreSQL database. At the end you'll have the rendered manifests in the console and, if you like, on your machine with backstage-cilium-render. Nothing is applied to a cluster yet; Turn on the default deny does that, carefully.
It assumes you've installed the plugins and allowed kind: Cilium from a location. The examples use a repository acme/k8s-deployment with the Cilium entities under cilium/policy/, and Components, APIs and Resources wherever your catalog already keeps them. The files are in the plugin's repository under docs/examples/first-policy.
1. The cluster
The policy is for a cluster, and the cluster is a Resource. You may have one already, for Backstage's Kubernetes plugin:
apiVersion: backstage.io/v1alpha1
kind: Resource
metadata:
name: prod
title: Production cluster
spec:
type: kubernetes-cluster
owner: group:platform2. The policy
Every cluster gets one entity of kind: Cilium and spec.type: policy:
# cilium/policy/policy.yaml
apiVersion: specht-labs.de/v1alpha1
kind: Cilium
metadata:
name: prod
title: Production cluster
spec:
type: policy
owner: group:platform
cluster: resource:prod
defaultDeny:
exclude: [kube-system]spec.cluster names the cluster's Resource. spec.defaultDeny.exclude lists the namespaces the default deny leaves alone; start with kube-system, whose pods (the DNS, the CNI itself, the cloud controllers) you don't want to model on day one. Excluded namespaces explains what being left out means.
3. The workloads
A Component or Resource is part of the policy, an endpoint, when it dependsOn the cluster's Resource and says where its pods are, with the annotations Backstage's Kubernetes plugin already uses:
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: shop
annotations:
backstage.io/kubernetes-namespace: shop
backstage.io/kubernetes-label-selector: app=shop
specht-labs.de/cilium-ports: tcp:8080
spec:
type: service
lifecycle: production
owner: group:shop
consumesApis: [orders-api]
dependsOn: [resource:prod, resource:shop-db]
---
apiVersion: backstage.io/v1alpha1
kind: Resource
metadata:
name: shop-db
annotations:
backstage.io/kubernetes-namespace: shop
backstage.io/kubernetes-label-selector: cnpg.io/cluster=shop-db
specht-labs.de/cilium-ports: tcp:5432
spec:
type: database
owner: group:shop
dependsOn: [resource:prod]
---
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: orders
annotations:
backstage.io/kubernetes-namespace: orders
backstage.io/kubernetes-label-selector: app=orders
spec:
type: service
lifecycle: production
owner: group:orders
providesApis: [orders-api]
dependsOn: [resource:prod]
---
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: orders-api
annotations:
specht-labs.de/cilium-ports: tcp:9000
spec:
type: grpc
lifecycle: production
owner: group:orders
definition: 'syntax = "proto3";'Three facts in there become flows:
shopconsumesorders-api, whichordersprovides: shop's pods reach orders' pods. The port comes from the API'sspecht-labs.de/cilium-ports, 9000.shopdepends onshop-db, a Resource with pods on the cluster: shop's pods reach the database on its port, 5432.- Each endpoint's own pods reach each other.
specht-labs.de/cilium-ports is the port the pod listens on, after the Service in front of it has translated the port. Without it a flow is allowed on every port, and the policy warns about it.
4. Look at the result
Commit the files and let the catalog pick them up. Open /cilium, or the Network policy tab on the prod entity. The overview says 3 of 3 endpoints are under the default deny and counts 2 flows; Rendered file has the manifests. The first one is the default deny:
# Network policies for resource:prod, rendered by Backstage from
# cilium:prod and the catalog's relations. Change the catalog, not
# this file: each rule says which catalog fact it comes from.
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: default-deny
labels:
app.kubernetes.io/managed-by: backstage
annotations:
specht-labs.de/cilium-policy: cilium:default/prod
spec:
description: "Default deny on resource:prod, outside kube-system: nothing reaches a pod, and a pod reaches nothing but the cluster DNS, unless a policy below allows it."
endpointSelector:
matchExpressions:
- key: io.kubernetes.pod.namespace
operator: NotIn
values:
- kube-system
enableDefaultDeny:
ingress: true
egress: true
ingress:
- {}
egress:
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
rules:
dns:
- matchPattern: "*"It selects every pod outside kube-system, turns on default deny in both directions, and allows only DNS queries to kube-dns, through Cilium's DNS proxy. Then comes one policy per endpoint. The shop's:
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: allow-component-shop
labels:
app.kubernetes.io/managed-by: backstage
annotations:
specht-labs.de/cilium-source: component:default/shop
specht-labs.de/cilium-policy: cilium:default/prod
spec:
description: "shop (component:shop): what reaches it, and what it reaches."
endpointSelector:
matchLabels:
io.kubernetes.pod.namespace: shop
app: shop
enableDefaultDeny:
ingress: false
egress: false
ingress:
# its own pods
- fromEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: shop
app: shop
egress:
# to component:orders
# component:shop consumes api:orders-api
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: orders
app: orders
toPorts:
- ports:
- port: "9000"
protocol: TCP
# to resource:shop-db
# component:shop depends on resource:shop-db
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: shop
cnpg.io/cluster: shop-db
toPorts:
- ports:
- port: "5432"
protocol: TCP
# its own pods
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: shop
app: shopEvery rule has a comment that says which catalog fact it comes from. The allow policies set enableDefaultDeny to false for both directions: they only add to what default-deny allows, and never put a pod into default deny on their own. allow-component-orders has the matching ingress from the shop on port 9000, and allow-resource-shop-db the ingress from the shop on 5432.
5. Check it on your machine (optional)
You don't have to wait for the catalog. backstage-cilium-render renders the same manifests from local checkouts of your catalog repositories, such as ~/src/acme/k8s-deployment ~/src/acme/catalog. Every directory you pass must be in a git working tree. Here it runs on this tutorial's files, in a checkout of the plugin's repository:
$ npx -p @spechtlabs/backstage-plugin-cilium-backend backstage-cilium-render \
--flows docs/examples/first-policy
Production cluster: 6 entities, 3 endpoints (3 under the default deny), 5 flows, 4 policies, 172 lines
component:default/orders orders/app=orders in 2 / out 1
component:default/shop shop/app=shop in 1 / out 3
resource:default/shop-db shop/cnpg.io/cluster=shop-db in 2 / out 1
flows:
component:shop -> component:orders [tcp:9000] component:shop consumes api:orders-api
component:shop -> resource:shop-db [tcp:5432] component:shop depends on resource:shop-dbThe first line counts every catalog entity it read, the endpoints, the flows (each endpoint's flow to its own pods included), the rendered policies and the lines of the file. Each endpoint follows with the pods its policy selects, written namespace/selector, and how many flows reach it and leave it. --flows lists the flows, without the ones within an endpoint. Add --out prod.yaml to write the manifests. Check a change locally has the details.
Next
- Turn on the default deny without breaking the cluster.
- Flows from relations covers every relation the plugin reads.
- External destinations lets pods reach the internet, by name.
