🐱 Calico Architecture and Usage
About 789 words
In Calico, or other BGP-based network architectures, route advertisement is closely linked to topology changes, policy adjustments, and system events. The following sections examine scenarios and their triggering mechanisms:
1. Calico Architecture Diagram
| Component | Submodule/File | Core Responsibilities | Example Configuration | Interacting Components | Typical Scenario |
|---|---|---|---|---|---|
| Felix | felix.cfg | - Manage node-local routing tables (route table) - Configure iptables/eBPF chains ( cali-INPUT, cali-FORWARD, etc.) - Enforce network policies (mark traffic allow/deny) | RouteRefreshInterval=10s MetadataAddr=127.0.0.1 IptablesMarkMask=0xff000000 | BIRD, etcd/K8s API, Typha | Add routes when Pods are created; apply policy updates immediately |
| BIRD | bird.cfg (generated by confd) | - Advertise this node's Pod CIDR over BGP, such as 10.0.1.0/24 - Learn routes from other nodes ( via Node2) | protocol bgp { local as 64512; neighbor 192.168.1.2 as 64512; } | Felix, Route Reflector | Establish BGP peering automatically when a node joins |
| Calico CNI Plugin | 10-calico.conflist | - Call IPAM to allocate Pod IPs - Create veth pairs ( caliXXXXX) - Set routes in Pod network namespaces | "ipam": { "type": "calico-ipam", "subnet": "usePodCidr" } | kubelet, IPAM, Felix | Invoked by kubelet during kubectl run to configure Pod networking |
| calico-kube-controllers | policy-controller | - Watch K8s NetworkPolicy and synchronize with Felix - Reclaim orphaned IPs ( IPGC) - Maintain node heartbeats | controllers: node, policy, serviceaccount | K8s API Server, etcd | Convert user-created NetworkPolicy objects into Calico format and distribute them |
| Typha | typha-deployment.yaml | - Proxy Felix API requests (cache Nodes, Policies) - Reduce API Server load (connections from O(N) to O(1)) | maxConnsPerClient=4 clientPort=5473 | Felix, K8s API | In clusters exceeding 50 nodes, Felix obtains policies through Typha instead of accessing the API directly |
| Route Reflector | bgpconfiguration.yaml | - Centralize route distribution in place of a full mesh - Support a cluster AS number ( AS 64512) and route aggregation | routeReflectorClusterID: 1.0.0.1 logSeverityScreen: Info | BIRD on all nodes | Avoid direct BGP sessions between nodes in cross-availability-zone deployments |
| etcd/K8s API | calico-config ConfigMap | - Store IPPools (10.0.0.0/16) - Store BGPPeers and NetworkPolicy definitions | etcd_endpoints: "https://etcd:2379" useKubernetesAPI: true | All components | Write to etcd when an administrator creates an IP pool with calicoctl |
| confd | /etc/calico/confd | - Watch etcd/K8s API changes, such as BGPPeer updates - Generate BIRD configuration dynamically and trigger reloads | confd -watch -interval 10 | BIRD, etcd | Update bird.cfg and run birdc configure when a BGP peer IP changes |
| eBPF Dataplane | bpfnat, bpfilter | - Replace kube-proxy for Service load balancing - Accelerate policy matching through eBPF maps ( calico_prefilter) | bpfEnabled: true bpfExternalServiceMode: DSR | Linux kernel, Felix | Handle Service DNAT rules with eBPF programs in performance-sensitive environments |
| WireGuard | wg-cali (interface) | - Encrypt inter-node traffic (udp port 51820) - Support PSK or certificate authentication | wireguardEnabled: true wireguardListeningPort: 51820 | Node network stack | Establish encrypted tunnels automatically across public clouds |
2. Using Calico
1. Set Up a kind Cluster
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
networking:
disableDefaultCNI: true
podSubnet: 192.168.0.0/16kind create cluster --name=calico-cluster --config=config.yaml2. Install Calico
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/tigera-operator.yamlkubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/custom-resources.yaml3. Open the Web Interface
kubectl port-forward -n calico-system service/whisker 8081:80814. Install Nginx for Testing
kubectl create namespace quickstartkubectl create deployment --namespace=quickstart nginx --image=nginxkubectl expose --namespace=quickstart deployment nginx --port=80After installing and exposing the service, create a BusyBox container and access the Nginx Pod.
kubectl run --namespace=quickstart access --rm -ti --image busybox /bin/shAt this point, both the Nginx instance in the cluster and the Internet are reachable over the Pod network.
wget -qO- http://nginx
wget -qO- https://docs.tigera.io/pod-connection-test.txt5. Add Network Restrictions with Calico
Restrict both ingress and egress.
kubectl create -f - <<EOF
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: default-deny
spec:
selector: projectcalico.org/namespace not in {'kube-system', 'calico-system', 'calico-apiserver'}
types:
- Ingress
- Egress
EOFAfter applying the restrictions, access to both Nginx and the Internet fails.
wget -qO- http://nginx
wget -qO- https://docs.tigera.io/pod-connection-test.txt5. Relax the Network Restrictions
Allow egress.
kubectl create -f - <<EOF
apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
name: allow-busybox-egress
namespace: quickstart
spec:
selector: run == 'access'
types:
- Egress
egress:
- action: Allow
EOFInternet access now works, but Nginx remains unreachable.
wget -qO- https://docs.tigera.io/pod-connection-test.txt
wget -qO- http://nginxAllow ingress to Nginx.
kubectl create -f - <<EOF
apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
name: allow-nginx-ingress
namespace: quickstart
spec:
selector: app == 'nginx'
types:
- Ingress
ingress:
- action: Allow
source:
selector: run == 'access'
EOFNginx is now reachable.
wget -qO- http://nginx6. Clean Up
kind delete cluster --name calico-cluster