CNI Working
Understanding the CNI Workflow in Kubernetes¶
The Container Network Interface (CNI) is a standardized specification that enables Kubernetes to configure networking for pods, ensuring they can communicate within and across nodes. The workflow outlines how Kubernetes’ kubelet, container runtime, and CNI plugin collaborate to set up pod networking.
Unified CNI Workflow¶
The CNI workflow involves Kubernetes components and the CNI plugin working together to assign IPs, configure network interfaces, and enable pod communication. Here’s a detailed, step-by-step explanation:
- Kubelet Initiates Pod Creation:
- What Happens: When you deploy a pod (e.g.,
kubectl run nginx --image=nginx), the Kubernetes API server schedules it to a node, and the node’s kubelet (the agent running on each node) is responsible for creating the pod. - Action: Kubelet instructs the container runtime (e.g., containerd, CRI-O) to create the pod’s sandbox, a network namespace that isolates the pod’s networking environment.
- Details:
- The sandbox is a Linux network namespace (netns) that acts as the pod’s isolated networking stack.
- Kubelet passes pod metadata (e.g., name, namespace, container ID) to the runtime.
-
In Your Setup: Using containerd on Ubuntu 22.04, kubelet communicates via the CRI socket (
/var/run/containerd/containerd.sock), as configured in yourkubeadm init --cri-socket. -
Container Runtime Requests Network Setup:
- What Happens: The container runtime, before starting the pod’s containers, needs to configure the pod’s network (e.g., assign an IP, set up interfaces). It delegates this to the CNI interface.
- Action: The runtime invokes the CNI plugin by executing scripts or binaries specified in the CNI configuration files located at
/etc/cni/net.d/(e.g.,10-calico.conflistfor Calico). - Details:
- The runtime passes pod details (e.g., pod name, namespace, container ID, network namespace path) to the CNI plugin via environment variables or JSON.
- The CNI interface is a standardized API (part of the CNI spec) that ensures compatibility between runtimes and plugins.
-
In Your Setup: Your
kind-config-file.yamlsetspodSubnet: "10.244.0.0/16"anddisableDefaultCNI: true, meaning containerd will call your manually installed Calico plugin. -
CNI Interface Triggers the CNI Plugin:
- What Happens: The CNI interface identifies the configured plugin (e.g., Calico, Flannel) from
/etc/cni/net.d/and triggers it to handle networking setup. - Action: The plugin’s binary (e.g., Calico’s
calicoorcalico-ipam) is executed with the pod’s details. - Details:
- The plugin reads its configuration from
/etc/cni/net.d/(e.g., Calico’s IP pool settings). - It communicates with the cluster’s networking infrastructure (e.g., Calico’s Felix agent or etcd for IP allocation).
- The plugin reads its configuration from
-
In Your Setup: Calico is configured with
CALICO_IPV4POOL_CIDR: "10.244.0.0/16", ensuring the plugin uses the correct IP range. -
CNI Plugin Configures Networking:
- What Happens: The CNI plugin performs the necessary networking tasks to connect the pod to the cluster network.
- Action:
- Assigns an IP: Allocates an IP address from the configured range (e.g.,
10.244.0.5from10.244.0.0/16). - Sets Up Interfaces: Creates a virtual ethernet (veth) pair, linking the pod’s network namespace to the host’s network (e.g., a bridge or routing table).
- Configures Routes: Adds routing rules to enable communication between pods, nodes, and external networks.
- Applies Firewall Rules: Implements network policies or NAT rules (e.g., Calico’s iptables for policies).
- Assigns an IP: Allocates an IP address from the configured range (e.g.,
- Details:
- For Calico (Layer 3), IPs are assigned via IPAM (IP Address Management), and routes are managed using BGP or IP-in-IP encapsulation for inter-node communication.
- Example: A pod gets
10.244.0.5, connected via a veth pair to the host’scali0interface, with BGP routes to other nodes.
-
In Your Setup:
- Calico assigns IPs from
10.244.0.0/16, matching yourpodSubnetand--pod-network-cidr. - Your past issue (errors with
192.168.0.0/16) occurred because a commented-outCALICO_IPV4POOL_CIDRprevented IP pool creation. Using10.244.0.0/16with an uncommented CIDR resolves this.
- Calico assigns IPs from
-
Pod is Connected to the Cluster Network:
- What Happens: Once the CNI plugin completes setup, the pod is fully networked and can communicate with other pods, services, and external resources.
- Action:
- The runtime starts the pod’s containers within the configured network namespace.
- Kubelet marks the pod as
Running(if other conditions, like image pulling, are met).
- Details:
- The pod’s IP (e.g.,
10.244.0.5) is visible viakubectl get pod -o wide. - Communication relies on the CNI plugin’s routing (e.g., Calico’s BGP for cross-node traffic).
- The pod’s IP (e.g.,
- In Your Setup:
- Pods receive IPs like
10.244.0.5, enabling communication across your Kind cluster (k8s-master-1,k8s-worker-1). - Verified via: Expected:
kubectl get pods -o wide kubectl get ippool -o yamlspec.cidr: 10.244.0.0/16.
- Pods receive IPs like
How CNI Works (Summary)¶
The Container Network Interface (CNI) enables Kubernetes to configure pod networking, assigning IPs and enabling communication. Here’s how it works:
- Kubelet Initiates Pod Creation:
-
The kubelet, Kubernetes’ node agent, schedules a pod (e.g.,
nginx) and instructs the container runtime (e.g., containerd) to create a pod sandbox—a network namespace isolating the pod’s networking. -
Container Runtime Requests Networking:
-
The runtime calls the CNI interface, passing pod details (e.g., name, namespace, container ID) via
/etc/cni/net.d/configuration files (e.g.,10-calico.conflist). -
CNI Interface Triggers the Plugin:
-
The CNI interface invokes the configured plugin (e.g., Calico, Flannel) to set up networking.
-
CNI Plugin Configures Networking:
-
The plugin:
- Assigns an IP (e.g.,
10.244.0.5) from the configured range (e.g.,10.244.0.0/16). - Creates a virtual ethernet (veth) pair, linking the pod’s namespace to the host’s network.
- Configures routes (e.g., via BGP for Calico) for pod-to-pod and node communication.
- Applies firewall rules or network policies.
- Assigns an IP (e.g.,
-
Pod Joins the Cluster Network:
- The pod receives its IP and is fully connected, enabling communication with other pods, services, and external resources.
Example: Calico in Your Setup¶
- Context: You run: Then apply Calico:
curl -s https://raw.githubusercontent.com/ibtisam-iq/SilverKube/main/kind-calico-config-file.yaml | kind create cluster --config -curl -O https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/calico.yaml sed -i 's/# - name: CALICO_IPV4POOL_CIDR/- name: CALICO_IPV4POOL_CIDR/' calico.yaml sed -i 's/# value: "192.168.0.0\/16"/ value: "10.244.0.0\/16"/' calico.yaml kubectl apply -f calico.yaml - Workflow:
- Deploy a pod:
kubectl run nginx --image=nginx --port=80 - Step 1: Kubelet on
k8s-worker-1tells containerd to create thenginxpod’s sandbox. - Step 2: Containerd calls the CNI interface (
/etc/cni/net.d/10-calico.conflist). - Step 3: The Calico plugin is triggered, reading
CALICO_IPV4POOL_CIDR: "10.244.0.0/16". - Step 4: Calico:
- Assigns
10.244.0.5to the pod. - Creates a veth pair (e.g.,
cali123in the pod’s netns, linked tocali0on the host). - Sets BGP routes via Felix to reach other nodes (e.g.,
k8s-master-1).
- Assigns
- Step 5: The pod is
Runningwith IP10.244.0.5, reachable cluster-wide. - Verification: Output:
kubectl get pod nginx -o wideNAME READY STATUS RESTARTS AGE IP NODE nginx 1/1 Running 0 5m 10.244.0.5 k8s-worker-1