Traffic coming into something is called Ingress. Traffic leaving someting (Server, service, pod...) is called Egress.
The picture below shows how traffic would normally flow for a 3-tier application in k8s.
Note: For traffic between pods, you should have an ingress rule (coming into the service) as well as an egress rule fo the traffic leaving the pod/pods that are calling the service.
All pods within a namespace should have their own internal vlan to allow them to communicate with one another without needing to configure anything like routes, even across the various nodes within a cluster..
This will allow traffic on port 3306 into
apiVersion: networking.k8s.io/vi
kind: NetworkPolicy
metadata:
name: db-policy
spec:
podSelector: # Apply this policy to all pods
matchLabels:
role: db # that have the role label set to db
policyTypes:
- Ingress # allow incoming traffic
ingress:
- from: # from any pod
- podSelector:
matchLabels:
name: api-pod # that has a name label = api-pod
ports:
- protocol: TCP
port: 3306 # on port 3306.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: internal-policy
namespace: default
spec:
podSelector:
matchLabels:
name: internal
policyTypes:
- Egress
- Ingress
ingress:
- {}
egress:
- to:
- podSelector:
matchLabels:
name: mysql
ports:
- protocol: TCP
port: 3306
- to:
- podSelector:
matchLabels:
name: payroll
ports:
- protocol: TCP
port: 8080
- ports:
- port: 53
protocol: UDP
- port: 53
protocol: TCP
apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
name: default.allow-egress-to-photos
namespace: myclientnamespace
spec:
order: 1005 # must be set (lower values take precedence)
selector: app == 'frontend' # apply policy on pod(s) with matching label
types:
- Egress
egress:
- action: Allow
protocol: TCP
destination:
domains:
- photos.aaronhatcher.com # allow egress to specific FQDN
ports: # must define ports on destination
- 443 # allow egress to port 443
```yaml
apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
name: default.allow-egress-to-photos
namespace: myclientnamespace
spec:
order: 1005 # must be set (lower values take precedence)
selector: app == 'frontend' # apply policy on pod(s) with matching label
types:
- Egress
egress:
- action: Allow
protocol: TCP
destination:
nets:
- 123.43.232.13/32 # allow egress to specific FQDN
- 123.43.232.14/32
ports: # must define ports on destination
- 443 # allow egress to port 443
Similar to firewalls or security groups that control access to virtual machines running in a cloud.
Scoped to Namespaces.
Sample network Policy:
If the podSelector: section is left empty, the policy is applied to all pods in the namespace
under the ingress/egress:
if the from list, if left empty, all hosts will be allowed.
it support namespaces, podSelector (labels within a pod) and ipBlock
if the ports list is left empty, all ports
if both from and ports are left, empty, all traffic will be allowed
kubectl describe -n <namespace> networkpolicies <policy name>
kubectl get networkpolicy.projectcalico.org
Network policies are configured on the pods, not the services. So, while a pod maybe configured to call a service:<service port>, you still need to create the network, policy for the actual port the Pod sitting behind the service is using. This can make configuring