Integrating Network Policies with Container Network Interfaces (CNIs) for Kubernetes

Container Network Interfaces (CNIs) are responsible for providing network connectivity to containers in a Kubernetes cluster. While CNI plugins handle the networking aspect, network policies are used to control the flow of traffic between pods. In this blog post, we will explore how to integrate network policies with CNIs for Kubernetes.

Prerequisites

Before we dive into the implementation, let's review the prerequisites:

  1. A Kubernetes cluster with a CNI plugin installed.

  2. kubectl configured to interact with the cluster.

  3. A network policy provider installed on the cluster.

Creating Network Policies

To create a network policy, we need to define the rules that govern the flow of traffic between pods. Here's an example of a network policy that allows traffic between two pods in different namespaces:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-inter-namespace-traffic
  namespace: namespace-1
spec:
  podSelector:
    matchLabels:
      app: app-1
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: app-2
          namespace: namespace-2
    ports:
    - protocol: TCP
      port: 80

In this example, the network policy is named allow-inter-namespace-traffic and is applied to the namespace-1 namespace. The podSelector field specifies the pods that the policy applies to, in this case, pods with the label app: app-1. The policyTypes field specifies the types of traffic that the policy allows, in this case, ingress traffic. The ingress field specifies the rules for incoming traffic, in this case, allowing traffic from pods with the label app: app-2 in the namespace-2 namespace on port 80.

Applying Network Policies with CNI Plugins

Once we have created a network policy, we need to apply it to the cluster. To do this, we need to ensure that the CNI plugin we are using supports network policies. Some popular CNI plugins that support network policies include Calico, Cilium, and Canal.

Here's an example of how to apply a network policy with Calico:

kubectl apply -f network-policy.yaml

In this example, we are applying the network-policy.yaml file to the cluster using kubectl. Calico will automatically apply the policy to the pods in the cluster.

Verifying Network Policies with CNI Plugins

To verify that the network policies are working as expected, we can use the kubectl describe networkpolicy command to view the details of a specific policy:

kubectl describe networkpolicy allow-inter-namespace-traffic --namespace namespace-1

We can also use the calicoctl command to view the network policies in the cluster:

calicoctl get networkpolicy

Troubleshooting Network Policies with CNI Plugins

If you are having trouble with network policies, there are several tools you can use to troubleshoot the issue. One useful tool is the calicoctl diagnose command, which provides a detailed report of the Calico installation and configuration:

calicoctl diagnose

Another useful tool is the calicoctl node status command, which shows the status of the Calico nodes in the cluster:

calicoctl node status

Conclusion

Integrating network policies with CNI plugins for Kubernetes provides a powerful and flexible way to control the flow of traffic between pods. By defining the rules that govern the flow of traffic, we can ensure that only authorized communication occurs within the cluster. With the kubectl apply command and the appropriate CNI plugin, we can easily create, apply, and troubleshoot network policies. By using the calicoctl command, we can quickly identify and resolve any issues with the Calico installation and configuration.

0
Subscribe to my newsletter

Read articles from Platform Engineers directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Platform Engineers
Platform Engineers

In today's global arena, secure & scalable platforms are mission-critical. Platform engineers design, build, and manage resilient infrastructure & tools for your software applications. We deliver enhanced security, fault tolerance, and elastic scalability, perfectly aligned with your business objectives.