Understanding Network Policies in Kubernetes: How to Implement Calico (CNI plugin)

NHAILA Achraf
4 min readOct 16, 2024

--

Network Policies ,CNI plugins

In Kubernetes, managing network traffic between pods, namespaces, and external services is crucial for security and performance. Network policies in Kubernetes enable fine-grained control over how pods communicate with each other and with other resources inside and outside the cluster. In this article, we’ll break down the importance of network policies, how to check if your cluster has a CNI plugin that supports them, and how to install and configure a CNI plugin, with examples using popular plugins like Calico.

Key Points Recap of the Article:

  1. Importance of Network Policies in Kubernetes
  2. Steps to Verify if a CNI Plugin is Installed
  3. Guide to Installing a CNI Plugin (e.g., Calico)
  4. How to Set Up and Configure Network Policies
  5. Comparison of Common CNI Plugins

1- Why Are Network Policies Important in Kubernetes?

Kubernetes is designed to facilitate communication between pods by default. All pods can communicate with each other across namespaces, which can pose security risks in large clusters. Network policies allow administrators to define rules that control the flow of traffic, improving both security and traffic management. With network policies, you can:

  • Isolate sensitive workloads: Only allow traffic from trusted sources to critical services.
  • Restrict external access: Block unwanted access to specific pods or namespaces.
  • Ensure compliance: Apply policies that meet security regulations or internal policies.
  • Optimize performance: Control unnecessary network traffic to reduce latency and enhance resource utilization.
Network Policies Important in Kubernetes

Without network policies, pod communication is unrestricted, leading to potential vulnerabilities. Network policies mitigate these risks by enforcing rules around traffic flow.

2- How to Check If a CNI Plugin Is Already Installed

To use network policies, a CNI plugin (Container Network Interface) that supports them must be installed. Let’s first check if your Kubernetes cluster has a plugin installed.

Step 1: Check Pods in the kube-system Namespace

Network plugins usually run as pods in the kube-system namespace. You can list these pods to see if a network policy plugin is already installed:

kubectl get pods -n kube-system

Example Output:

NAME                         READY   STATUS    RESTARTS   AGE
calico-node-xysw8 1/1 Running 0 45d
kube-proxy-7dfbc 1/1 Running 0 45d
metrics-server-68f77 1/1 Running 0 45d

The presence of calico-node confirms that the Calico CNI plugin is installed.

Step 2: Check Applied Network Policies

Run the following command to check if any network policies are applied:

kubectl get networkpolicies --all-namespaces

Output:

NAMESPACE     NAME                 POD-SELECTOR             AGE
kube-system konnectivity-agent app=konnectivity-agent 163d

This shows that the konnectivity-agent has a network policy applied, but there may not be any custom policies configured yet.

3- Installing a CNI Plugin

If your cluster doesn’t have a CNI plugin that supports network policies, you can install one. Let’s use Calico, one of the most popular CNI plugins.

4- Installing Calico

To install Calico, run:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Verifying the Installation

Once installed, verify that the Calico pods are running:

kubectl get pods -n kube-system

Look for calico-node pods in the output. If they’re running, Calico is installed successfully.

Output:

NAME                         READY   STATUS    RESTARTS   AGE
calico-node-xyz45 1/1 Running 0 12m
calico-kube-controllers 1/1 Running 0 12m

Configuring Network Policies with Calico

Once the plugin is installed, you can create network policies to control traffic. Let’s configure a simple network policy that restricts communication between namespaces.

Example: Restricting Traffic Between Namespaces

This example creates a policy that only allows traffic from the frontend namespace to the backend namespace.

  1. Create two namespaces:
kubectl create namespace frontend
kubectl create namespace backend

2. Deploy pods in each namespace:

kubectl run nginx-frontend --image=nginx --namespace=frontend
kubectl run nginx-backend --image=nginx --namespace=backend

3. Create a network policy to restrict communication:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend
namespace: backend
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: frontend

4.Apply the policy:

kubectl apply -f network-policy.yaml

Now, only pods in the frontend namespace can communicate with pods in the backend namespace.

5- Comparison of Popular CNI Plugins

Here is a comparative table of the most widely-used CNI plugins in Kubernetes, highlighting their key features, strengths, and weaknesses:

  • Calico: Best for large-scale production environments with complex security needs.
  • Flannel: Ideal for simple, smaller clusters without advanced networking needs.
  • Weave: Great for multi-cloud environments with built-in encryption.
  • Cilium: Best for high-security environments and microservices with Layer 7 support.

Network policies are essential for securing and controlling traffic flow in Kubernetes clusters. By installing a CNI plugin like Calico, you can enforce detailed network rules that enhance the security of your infrastructure. Whether you need a simple solution for small environments or a complex, high-performance network policy setup for large clusters, choosing the right CNI plugin is crucial.

With Calico installed, configuring and managing network policies becomes straightforward, allowing you to maintain a secure, optimized Kubernetes environment.

I hope this will help ,If you have any further questions or run into issues, don’t hesitate to reach out.

NHAILA Achraf
Keywords SEO: #Kubernetes #networkpolicies, #CNIplugin, #Calico network policy, Kubernetes security, Kubernetes pod networking, install Calico Kubernetes, configure network policies, #Kubernetes #CNI

--

--

NHAILA Achraf
NHAILA Achraf

No responses yet