What is a Kubernetes NetworkPolicy?
IntermediateConceptKubernetes, Containers, MTU & Performance Basics
Answer
A namespaced resource that restricts pod ingress and egress by pod selector, namespace selector, IP block and port. By default all traffic is allowed; once a pod is selected by any policy, only the allowed flows are permitted. It is enforced by the CNI, so Flannel alone ignores it.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: db-allow-app, namespace: shop}
spec:
podSelector: {matchLabels: {app: db}}
policyTypes: [Ingress]
ingress:
- from: [{podSelector: {matchLabels: {app: api}}}]
ports: [{protocol: TCP, port: 5432}]
More on Kubernetes, Containers, MTU & Performance Basics
What are the basic Kubernetes networking rules?BeginnerWhat are the Service types in Kubernetes?BeginnerWhat is the difference between a Service and an Ingress?BeginnerHow does kube-proxy implement Services?AdvancedHow does DNS work for Services and pods in Kubernetes?IntermediateWhat is a CNI plugin and how do the main ones differ?Intermediate