kubernetes network policy examples

Mastering Kubernetes: 7 Network Policy Examples You Can't Overlook

By Laura Moore
AD

If you're managing containerized applications using Kubernetes, you're likely aware of the significance of network policies in controlling traffic flow between pods. Network policies are crucial for enhancing security, compliance, and maintaining cluster stability. Let's explore some practical Kubernetes network policy examples, diving deep into their configuration and use cases.

Before we delve into specific examples, let's quickly recap. Network policies are essentially rules that govern how groups of pods communicate. They define allowable or disallowable traffic, enabling precise control over inter-pod communication.

Einstein Synonyms

One of the simplest yet beneficial uses of Kubernetes network policies is to control access to critical services like etcd. Here's a network policy that restricts access to the Kubernetes API server endpoints:

AD
the kuberite's yaml explanation poster
the kuberite's yaml explanation poster
the kubernets networking diagram
the kubernets networking diagram
the roadmap for kubernets is shown in this graphic, which shows how
the roadmap for kubernets is shown in this graphic, which shows how

```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-access-to-kube-api spec: podSelector: matchLabels: k8s-app: kube-apiserver policyTypes: - Ingress ingress: - from: - podSelector: {} ports: - protocol: TCP port: 6443 ```

Allowing Access to the Kube API Server

This policy, aptly named `allow-access-to-kube-api`, is designed to permit traffic to the Kubernetes API server (kube-apiserver) on port 6443, which is the secure endpoint for remote communication. It uses an empty pod selector in the ingress rule, allowing traffic from all pods in the cluster.

the life of a kubernets pod, infographed in blue and black
the life of a kubernets pod, infographed in blue and black
a diagram with the words how to learn kubernets on it and an image of
a diagram with the words how to learn kubernets on it and an image of
Kubernetes Explained: How Containers Run at Scale
Kubernetes Explained: How Containers Run at Scale

For enhanced security, you might want to refine this policy to allow access from specific sources only. Let's see an example:

Restricting Kube API Access to Specific Namespaces

In this scenario, we'll tweak the previous policy to allow access to the Kubernetes API server only from pods in the kube-system and admin namespaces. Here's how to do it:

AD

```yaml ... ingress: - from: - namespaceSelector: matchLabels: name: kube-system pods: - namespaceSelector: matchLabels: name: admin pods: ... ```

the kubernets basics diagram with several different types of text and symbols on it
the kubernets basics diagram with several different types of text and symbols on it
a blue and white poster with the words kubernets written in different languages
a blue and white poster with the words kubernets written in different languages
Infographic: Kubernetes for Beginners: A Tutorial For K8s Basics, Features, Architecture & Security
Infographic: Kubernetes for Beginners: A Tutorial For K8s Basics, Features, Architecture & Security
Kubernetes in Plain English
Kubernetes in Plain English
the kubernets administration tips and tricks poster
the kubernets administration tips and tricks poster

The namespaceSelector field restricts access to pods in the specified namespaces, enhancing security by limiting the ability to initiate API requests to authorized pods only.

Access Control based on pod labels

Another common use case is controlling traffic flow based on pod labels. Let's consider an application composed of several services, where communication is desired only among those having the same label:

AD

```yaml apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-app-services-communication spec: podSelector: matchLabels: app: my-app policyTypes: - Ingress - Egress ingress: - from: - podSelector: matchLabels: app: my-app ports: - protocol: TCP port: 8080 egress: - to: - podSelector: matchLabels: app: my-app ports: - protocol: TCP port: 8080 ```

Enabling Intra-service Communication

This `allow-app-services-communication` policy allows traffic flow between pods labeled with app: my-app on port 8080, facilitating communication within our application services. The policy includes both ingress and egress rules to ensure smooth bi-directional communication.

Preventing External Access

To further harden the cluster's security, we might want to prevent external traffic from accessing our application. Here's how to configure Calico to disallow external access:

AD

```yaml ... applyOnce: true ineffectiveWithOtherEgressRules: true spec: podSelector: matchLabels: app: my-app policyTypes: - Egress egress: - to: - ipBlock: cidr: 0.0.0.0/0 except: - 192.168.0.0/16 ```

This policy prevents all pods with the label app: my-app from initiating outbound requests to any IP address except those in the 192.168.0.0/16 range, significantly reducing the potential attack surface.

In our dynamic, containerized world, network policies are essential tools for maintaining a secure and stable Kubernetes cluster. By implementing the examples discussed, you can tailor communication between your pods, ensuring that your applications run smoothly and safely.

AD