你對L3、L4、L7層網絡策略也就是防火牆策略的區別清楚嗎?直到我學習cilium時,通過cilium的官方網站給出的使用說明,我終於理解了L3、L4、L7層網絡策略的差別。現記錄如下:
L3網絡策略
L3的網絡測試是基於IP/CIDR或者DNS域名的,對於cilium所在的雲計算領域還可以基於容器的label,service,entity.其中IP/CIDR就是基於IP或者IP段;DNS就是指基於域名;entity可以是“host、remote-node、cluster、init、world、all”等。下面是一個三層策略示例:
apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
name: "cidr-rule"
spec:
endpointSelector:
matchLabels:
app: myService
egress:
- toCIDR:
- 20.1.1.1/32
- toCIDRSet:
- cidr: 10.0.0.0/8
except:
- 10.96.0.0/12
L4網絡策略
四層策略可以和三層策略配置使用也可以單獨使用,它指定了協議並指定端口,如下所示:
apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
name: "l4-rule"
spec:
endpointSelector:
matchLabels:
app: myService
egress:
- toPorts:
- ports:
- port: "80"
protocol: TCP
L7網絡策略
7層策略規則嵌入到第4層示例規則中,並且可以在ingress和egress方向指定。L7Rules結構是一個base type,包含協議特定字段的枚舉值。如下是cilium中的一個L7策略,它針對的是http協議中的get方法並且路徑是public的報文:
kind: CiliumNetworkPolicy
metadata:
name: "rule1"
spec:
description: "Allow HTTP GET /public from env=prod to app=service"
endpointSelector:
matchLabels:
app: service
ingress:
- fromEndpoints:
- matchLabels:
env: prod
toPorts:
- ports:
- port: "80"
protocol: TCP
rules:
http:
- method: "GET"
path: "/public"