📦 How Calico Works
About 1326 words
kind create cluster --name calico-cluster --config - <<EOF kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes:
- role: control-plane
- role: worker
- role: worker networking: disableDefaultCNI: true podSubnet: 192.167.0.0/16 EOF
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/tigera-operator.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/custom-resources.yaml
cat <<EOF | calicoctl apply -f -
apiVersion: projectcalico.org/v3 kind: IPPool metadata: name: external-pool spec: cidr: 192.168.64.0/24 blockSize: 29 ipipMode: Never natOutgoing: true nodeSelector: "!all()" EOF
multipass set local.bridged-network.range=10.168.64.0/24
cat <<EOF | calicoctl apply -f -
apiVersion: projectcalico.org/v3 kind: BGPPeer metadata: name: bgppeer-global-host1 spec: peerIP: 192.168.64.5 asNumber: 64512 EOF
sudo bash -c "cat > /etc/bird/bird.conf <<EOF router id 192.168.64.6;
protocol kernel { scan time 60; import none; export all; }
protocol device { scan time 60; }
protocol bgp k8s_calico { local as 64512; neighbor 172.18.0.3 as 64511; # Point to the kind node's IP import all; export none; multihop; } EOF"
kubectl apply -f - <<EOF apiVersion: projectcalico.org/v3 kind: BGPPeer metadata: name: multipass-peer spec: peerIP: 192.168.64.6 asNumber: 64512 nodeSelector: all() EOF
1. A Node Joins the Cluster
When a new node joins a Kubernetes cluster, Calico triggers route advertisement as follows:
- Node registration:
- The new node registers with the Kubernetes API Server through
kubelet. - Calico's
CNI pluginassigns the node a Pod CIDR, such as10.244.3.0/24.
- The new node registers with the Kubernetes API Server through
- Felix detects the change:
- Felix watches the Kubernetes API and discovers the new node and its assigned Pod CIDR.
- Felix configures the local routing table so local Pods can reach the new node's Pod CIDR.
- BIRD triggers BGP advertisement:
- BIRD detects the new route, such as
10.244.3.0/24belonging to the new node, and advertises it to all peers (other nodes) over BGP. - Other nodes learn the route through BGP and update their kernel routing tables.
- BIRD detects the new route, such as
Key logs (view with journalctl -u bird):
BGP: Sending UPDATE to neighbor 192.168.100.3: Added 10.244.3.0/242. A Node Leaves or Fails
- The node becomes unreachable:
- The BGP session disconnects because the node is down or the network is interrupted (the default Keepalive timeout is 90 seconds).
- Route withdrawal:
- BIRD detects the failed peer and automatically sends a
WITHDRAWmessage, notifying other nodes to remove that node's routes, such as10.244.3.0/24.
- BIRD detects the failed peer and automatically sends a
- Cluster convergence:
- Other nodes remove the invalid routes and stop forwarding traffic to the failed node.
Impact:
- If the node recovers, the BGP session is reestablished and routes are advertised again.
3. Pod Creation or Deletion
- Pod creation:
- Calico
IPAMallocates an IP from the Pod CIDR, such as10.244.1.5. - Felix adds a directly connected route for the Pod on the host (no BGP advertisement is needed because it belongs to the local CIDR).
- Calico
- Cross-node Pod communication:
- Only the Pod CIDR subnet, such as
10.244.1.0/24, is advertised over BGP; individual Pod IPs are not advertised separately. - Other nodes locate the specific Pod using learned Pod CIDR routes together with the host's ARP resolution.
- Only the Pod CIDR subnet, such as
Exception:
- If
host-localIPAM is enabled and the Pod CIDR is exhausted, a new node may be added to expand the address pool.
4. Network Policy Changes
- Policy updates:
- Changing
NetworkPolicymay cause Felix to regenerate iptables/eBPF rules.
- Changing
- Routing policy adjustments:
- If a policy affects routing, such as blocking communication with certain subnets, BIRD may filter or readvertise routes.
- For example, use an
export filterto restrict the advertised route range.
5. Manual Intervention
- Force a route refresh:
birdc reload # Reload BIRD configuration birdc disable bgp_peer # Disable the peer temporarily, then reenable it to trigger a route update - Calico configuration changes:
- Changing
BGPConfiguration, such as adjusting the AS number or enabling RR mode, triggers cluster-wide route resynchronization.
- Changing
6. Other Scenarios
| Scenario | Trigger | Route Advertisement Behavior |
|---|---|---|
| Cluster expansion | Multiple new nodes | Each new node's Pod CIDR is advertised separately |
| Node IP changes | Host IP update, such as a DHCP renewal | Reestablish the BGP session and readvertise routes |
| BGP peer configuration changes | Add/remove BGP neighbors, such as in a spine-leaf topology | Adjust the scope of route propagation dynamically |
| IP pool changes | Modify an IPPool CIDR | All affected nodes readvertise routes |
Underlying Mechanisms from the BGP Perspective
- Conditions for route updates:
- When a BGP speaker such as BIRD detects local routing-table changes (routes added or removed), it sends
UPDATEmessages to its peers.
- When a BGP speaker such as BIRD detects local routing-table changes (routes added or removed), it sends
- Convergence:
- The default
Full-Meshmode has every node exchange routes directly. - Large clusters can use a
Route Reflector (RR)to reduce BGP connection counts.
- The default
Verifying Route Advertisements
- Check BGP neighbor status:
calicoctl node status # Or birdc show protocols - Inspect routing tables:
ip route # Show the kernel routing table birdc show route # Show routes learned by BIRD - Analyze packet captures:
tcpdump -i eth0 'tcp port 179' # Capture BGP traffic
Summary
Calico route advertisements are mainly triggered by:
- Node lifecycle changes (joining/leaving).
- Pod CIDR allocation or reclamation.
- BGP session state changes, such as peer disconnection.
- Policy or configuration updates.
This design provides high availability and fast convergence for the cluster network while avoiding the single-point bottleneck of a traditional SDN controller.
In Calico, route advertisement responsibilities are shared by the following components, with different components leading in different scenarios:
1. Default Scenario: Pod Network Route Advertisement
Advertiser: BIRD (BGP Client)
- Role: The lightweight BGP client deployed by Calico on each node, using BIRD or GoBGP by default.
- Workflow:
- Felix watches local Pod changes and updates the local routing table, such as
10.244.1.0/24 via veth-host. - BIRD detects route changes and advertises them over BGP to peers, either other nodes or external routers.
- Peers learn the routes and update their own routing tables.
- Felix watches local Pod changes and updates the local routing table, such as
- Key configuration:
# Inspect Calico's BGP configuration calicoctl get bgpconfiguration - Log verification:
journalctl -u bird | grep "UPDATE" # View routes advertised by BIRD
2. Special Scenario: Service CIDR Advertisement (Experimental)
Advertiser: BIRD on Calico Nodes
- Trigger: Explicitly configure
serviceClusterIPsinBGPConfiguration. - Example configuration:
apiVersion: projectcalico.org/v3 kind: BGPConfiguration metadata: name: default spec: serviceClusterIPs: - cidr: 10.96.0.0/16 # Kubernetes Service CIDR - Behavior:
- Calico uses BIRD to advertise
10.96.0.0/16as an aggregate route. - Note:
kube-proxyoreBPFstill handles Service IP load balancing; BGP provides only route reachability.
- Calico uses BIRD to advertise
3. Advanced Scenario: Route Reflectors
Advertiser: Calico Route Reflector (RR)
- Use case: Avoid excessive full-mesh BGP connections in large clusters.
- Role:
- Dedicated RR nodes centrally manage route advertisement.
- Other nodes (RR clients) exchange routes only with the RR instead of connecting directly to each other.
- Configuration:
# Mark a node as a Route Reflector calicoctl patch node node1 -p '{"spec": {"bgp": {"routeReflectorClusterID": "224.0.0.1"}}}'
4. External Network Integration
Advertiser: Border Node
- Scenario: Advertise cluster routes (Pods or Services) to an external network, such as data-center routers.
- Configuration:
- Configure BGP peering on the border node:
apiVersion: projectcalico.org/v3 kind: BGPPeer metadata: name: border-router spec: peerIP: 192.168.100.254 # External router IP asNumber: 65000 # External AS number
Component Interaction Diagram
Key Points
| Route Type | Advertiser | Dependencies | Configuration |
|---|---|---|---|
| Pod routes | BIRD on each node | Felix | Triggered automatically; no extra configuration |
| Service routes | BIRD (experimental) | BGPConfiguration | Explicitly declare serviceClusterIPs |
| External routes | BIRD on border nodes | BGPPeer | Configure peer IP and AS number |
| Aggregate routes | Route Reflector | Cluster topology | Mark nodes as RRs |
Frequently Asked Questions
Q1: Why Are My Pod Routes Not Being Advertised?
- Check BGP session status:
calicoctl node status - Confirm BGP is enabled:
calicoctl get bgpconfig -o yaml
Q2: How Do I Troubleshoot Failed Service Route Advertisements?
- Verify the configuration:
calicoctl get bgpconfiguration -o yaml - Inspect BIRD logs:
journalctl -u bird | grep "service"
Q3: How Do I Restrict Which Routes Are Advertised?
Use exportFilter to filter routes:
apiVersion: projectcalico.org/v3
kind: BGPConfiguration
spec:
nodeToNodeMeshEnabled: false
serviceClusterIPs:
- cidr: 10.96.0.0/16
exportFilter:
- action: Deny
matchExpression: '!has(10.244.0.0/16)' # Allow only routes for 10.244.0.0/16