How can a pod communicate with the other pod? The answer to this question is through a network policy.
Network Policy is a specification of how groups of pods are allowed to communicate with each other and other network endpoints.
Like Services, it uses labels to select the pods.
Pod by default is NON-ISOLATED, implying they accept traffic from any source, by virtue of NetworkPolicy an isolation is defined.
pod will reject any connections that are not allowed by any NetworkPolicy.Network policies are additive by nature.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-network-policy
namespace: default
spec:
podSelector:
matchLabels:
app: my-running-pod
policyTypes:
- Ingress
- EgressMandatory fields for a NetworkPolicy
- apiVersion
- kind
- metadata
Type of Services (popular)
ClusterIP
NodePort
LoadBalancer
podSelector#
Each NetworkPolicy defines a podSelector, which identifies the pods to which the policy applies. In our example (above) the policy is applied to all pods with label app: my-running-pod
podSelector selects all pods in the namespace.policyTypes#
It can be either of Ingress (default), Egress or both.
Ingress#
Each rule allows traffic which matches both the from and ports sections.
Egress#
Each rule allows traffic which matches both the to and ports sections.
