Open In App

How To Use Kubernetes Network Policies?

Kubernetes is the kind of container-centric management software used for the deployment and operation of cont energized applications. It was originally developed by google clouds. It improves your reliability and reduces the time, workload and provides developers resources attributes to daily operations. It allows you to scale up and down your application.

What Are Kubernetes Network Policies?

Why Use Kubernetes Network Policies?



How do Kubernetes Network Policies Work?

How To Create Kubernetes Network Policies?

Yaml file

#policy1-do.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: policy1
namespace: dev
spec:
podSelector:
matchLabels:
app: webserver-dev
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 10.169.25.20/32
- namespaceSelector:
matchLabels:
project: jtac
- podSelector:
matchLabels:
app: client1-dev
ports:
- protocol: TCP
port: 80
egress:
- to:
- podSelector:
matchLabels:
app: dbserver-dev
ports:
- protocol: TCP
port: 80

$ kubectl apply -f policy1-do.yaml
networkpolicy.networking.k8s.io/policy1-do created

$ kubectl get netpol -n dev
NAME POD-SELECTOR AGE
policy1 app=webserver-dev 6s

$ kubectl describe netpol policy -n dev
Name: policy1
Namespace: dev
Created on: 2019-10-01 11:18:19 -0400 EDT
Labels: <none>
Annotations: <none>
Spec:
PodSelector: app=webserver-dev
Allowing ingress traffic:
To Port: 80/TCP
From:
IPBlock:
CIDR: 10.169.25.20/32
Except:
From:
NamespaceSelector: project=jtac
From:
PodSelector: app=client1-dev
Allowing egress traffic:
To Port: 80/TCP
To:
PodSelector: app=dbserver-dev
Policy Types: Ingress, Egress

According To Ingress Policy

The following clients can reach the webserver-dev server pod located in the dev namespace.

According To Egress Policy

The webserver-dev server pod in the dev namespace can initiate a TCP session toward dbserver-dev pod with destination port 80 to access the data.



Post Kubernetes Network Policies Creation

After successfully creating policy 1 for Kubernetes, let’s examine the accessing of the HTTP server in the webserver-dev pod from pod client-1 dev, client jtac, and node cent 222

$ kubectl exec -it client1-dev -n dev -- curl http://$webserverIP | webpr
Hello
This page is served
by a Contrail pod
IP address =
10.47.255.234
Hostname = webserver-dev

The access from these two pods to webserver-dev is okay and that is what we want. Now, if we repeat the same test from the other pod client2-dev, client-qa and another node cent333 now get timed out:

$ kubectl exec -it client2-dev -n dev --
curl http://$webserverIP -m 5 curl: (28)
Connection timed out after 5000
milliseconds
command terminated with exit code 28
$ kubectl exec -it client-jtac -n jtac --
curl http://$webserverIP -m 5 curl: (28)
Connection timed out after 5000
milliseconds
command terminated with exit code 28

$ curl http://$webserverIP -m 5
curl: (28) Connection timed out after 5000 milliseconds

Examples of Kubernetes network policies

$ kubectl apply -f policy1-
do.yaml
networkpolicy.networking.k8
s.io/policy1 created
$ kubectl get networkpolicies --
all-namespaces NAMESPACE
NAME POD-
SELECTOR AGE
dev policy1 app=webserver-dev 17s

Conclusion

Initially, in a Kubernetes cluster, all pods are non-isolated by default and they work in an allow-any-any model so any pod can talk to any other pod. Now apply a network policy named policy1 to pod A. In policy policy1 you define a rule to explicitly allow pod A to talk to pod B. In this case let’s call pod A a target pod because it is the pod that the network policy will act on.


Article Tags :