More Related Content Similar to DevOps - Cilium Networking Policy and Introduction (20) DevOps - Cilium Networking Policy and Introduction2. Agenda
● Policy Enforcement Modes
● Rule Basics
● L3 Examples
● L4 Examples
● L7 Examples
● Deny Policies
● Host Policies
● L7 Protocol Visibility
● Using Kubernetes Constructs in Policy
3. What’s Cilium Network Policy
● Network Policy 是限制 Pod 之間⼩顆粒度 Networking 管控
● Cilium 與其它CNI 相同,提供了整個集群的網路互通,使⽤者需要⾃訂安
全隔離策略
● 除了基本的L3/L4 策略外,還多了L7的⽀援
● 通過 Agent 在容器運⾏時的 BPF 注入 Network Policy Bytecode
● Cilium 封裝了 eBPF
4. Why Cilium
● Pod 數量越來越多 (>20000 or more)
● Pod Lifecycle 越來越短 (<10s)
● 在 Micro Services 的流⾏下,Multi-Cluster (K8S Mesh) 是⼀個難題,甚
⾄是 K8S in K8S
這些對 Kubernetes Cluster Networking 都是越來越⼤的挑戰,Policy 已經多
到成為性能瓶頸。
5. Kube Proxy
● Default 由 kube-proxy 來實現 k8s service 的功能
○ Iptables
■ 可擴展性極差,每加⼀條規則都需要遍歷所有規則 O(n²)
○ IPVS
■ ⽤ Hash Table 管理 service O(1)
■ 複⽤ iptables 的 SNAT
6. Cilium
● eBPF and Hash Table
● Cilium eBPF 繞過了整個 Netfilter
Ref: https://blog.51cto.com/u_14992974/2547769
9. Policy Enforcement Modes
● Cilium Agent / Cilium Network Policy 決定流量是否被接受或拒絕
● Agent Policy Enforcement Modes (Runtime 可改動):
○ Default
■ Endpoint 啟動時是 Allow-All 的
■ 當 Rule Selected Endpoint,必須明確指定允許的規則,否則 Deny
(Whitelist Mode)
○ Always
■ 沒有 Rule 的話是無法訪問以及被訪問的
○ Never
■ 忽略所有 Network Policies,所有 Traffic 都會被允許
11. Rule Basics
● Whitelist Model - Like AWS Security Group
● Rule Structures
○ Endpoint Selector / Node Selector
■ 兩者皆不能為空,並且互斥
■ 以 Label Selector 進⾏匹配 Endpoints
○ Ingress / Egress
■ 兩者皆為空的話,規則無效
○ Labels
■ ⽤為辨識 Rule,可重複
○ Description
13. Layer 3 Example
● 基於 IP/CIDR 或是 DNS
○ IP / CIDR (IPv4 / IPv6)
■ 有時候並不⾜夠彈性
○ DNS
■ Fully Qualified Domain Name (FQDN) / Remote / Non-Cluster
● toFQDN.matchPattern (*.cilium.io / www.*fdg.net)
■ 將經由 DNS Lookups 轉化成 IP
■ 需注意 TTL / DNS Cache 配置
● --tofqdn-min-ttl
● --tofqdn-max-ip-per-hostname (default: 50)
14. Layer 3 Example
● 基於 Label Matching
○ 基於 Endpoint Selector
○ Decoupling from IP
● 基於 Service
○ ⾃動 Apply 到 Service 後⾯的 Targets
○ Service 不由 Cilium 控制可以⽤這種⽅式 Apply
○ Decoupling from IP
15. Layer 3 Example
● 基於 Entities
○ host
■ Localhost / Host Networking Mode
○ remote-node
■ 其他 nodes
○ cluster
■ 本 cluster 的所有 endpoints
○ init
■ 還沒有被解析的 Endpoint
○ health
■ cluster health check 的 endpoint
○ unmanaged
■ 不經由 Cilium 管理的 Endpoint
○ world / all
■ 0.0.0.0/0 以及已知 cluster
○ Decoupling from IP
17. ● 可以作為 L3 Policy 的補充或是完全獨立
● L4 管理 Protocols / Ports 規則
● 如果不設定 L4 Policy
○ 所有 Protocols / Ports 都默認允許
○ ICMP 也默認允許
○ ⼀但 L4 Policy 配置,ICMP 會 Deny
● CIDR Based
● Label Matching
○ Decoupling from IP
Layer 4 Example
19. Layer 7 Examples
● 與L3/L4 不同的是,違反L7 的規則不會導致封包被drop,會返回特定協議
的訪問被拒消息,如http 403
● 每個endpoint 最多⽀持40個port
● Host policy ⽬前不⽀援L7 規則,可以⽤Node Selector
● ⽀援協議
○ HTTP
○ Kafka (beta)
○ DNS Policy and IP Discovery
20. HTTP - 可配置的參數
● Path
○ ⽀援POSIX regex match,必須以 / 開頭,如果省略或為空,則所有
路徑都允許
● Method
○ ⽀援POSIX regex match,如果省略或為空,則所有Method 都允許
● Host
○ ⽀援POSIX regex match,如果省略或為空,則⾃動忽略
● Headers
○ 如果省略或為空,則所有請求都允許
23. Kafka (beta)
● ⽀援 2 種 Kafka Rule
○ Role:
■ 為 Topic 指定 Producer 或 Consumer Role
○ Kafka API Key:
■ Kafka 本⾝每個 request 都會包含⼀個 API key,標⽰正在調⽤
的 API 以及版本號和 request/response patterns (Cilium Kakfa
Rule 會⾃動擴展所需的 API key)
24. Kafka - 可配置的參數
● Role
○ 可配置為以下⾓⾊
■ Produce
■ Consume
● APIKey
○ 與 Role 不相容
○ 可配置為 roduce, fetch, createtopic, deletopic, more extensive list
● APIVersion
● ClientID
● Topic
27. DNS Policy and IP Discovery
● DNS policy 通過DNS Proxy 實現
● 可以設定允許或禁⽌特定的域名查詢或匹配式
● 不⽀援指定查詢類型,例如 NS, SOA, TXT, …
● 可配置的參數
○ matchName
○ matchPattern
■ a-z, 0-9, . , - , *
● 設定DNS Policy 時建議加上 matchPattern: *.*.svc.cluster.local.
○ 常⾒的 container 如 alpine 會因為 DNS refused ⽽停⽌遍歷 /etc/resolv.conf 的搜尋列表,如
搜尋 cilium.io 會先嘗試 cilium.io.default.svc.cluster.local ,如果被 DNS Proxy 拒絕了,Pod
會將DNS 查詢視為失敗
○ 適當修改 dnsConfig 的 ndots
32. Deny Policies (Beta)
● For example
○ world = 0.0.0.0/0
○ all = known clusters + 0.0.0.0/0
○ 允許all 但拒絕 world
○ 可以視為只允許 cluster 內流量
35. Host Policies (Beta)
● 仍是測試版
● Host policies ⽤CiliumClusterwideNetworkPolicy
● 使⽤ Node Selector ⽽不是Endpoint Selector
● 只適⽤ host namespace, 包含host-networking pods
36. Host Policies (Beta)
● For example
○ remote-node = 除了本機以外的
任何worker node, 包含host-
networking pods
○ health = cilium ⾃帶的health
check endpoint
38. Layer 7 protocol Visibility
● Cilium 預設只會對 L3/L4 封包提供visibility 功能,Layer 7 需要另外啟⽤
● 在 annotation 加上:
○ <{Traffic Direction}/{L4 Port}/{L4 Protocol}/{L7 Protocol}>
● 例如:
○ kubectl annotate pod foo -n bar io.cilium.proxy-visibility="<Egress/53/UDP/
DNS>,<Egress/80/TCP/HTTP>"
● 僅⽀援HTTP 和DNS, 不⽀援 proxylib parsers (例如kafka)
○ DNS 僅⽀援 egress