Example 1:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-policy
spec:
podSelector:
matchLabels:
role: db # Pods where the NetPol will be applied
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
name: api-pod # the api-pod will have rights to connect to db Pod on port 3306
namespaceSelector: # this with podSelector acts as AND, put - to make it OR
matchLabels:
name: prod
- ipBlock: # the address of external server
cidr: 192.168.5.10/32
ports:
- protocol: TCP
port: 3306
Example 2:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-policy
spec:
podSelector:
matchLabels:
role: db # Pods where the NetPol will be applied
policyTypes:
- Ingress
- Egresss
ingress:
- from:
- podSelector:
matchLabels:
name: api-pod # the api-pod will have rights to connect to db Pod on port 3306
ports:
- protocol: TCP
port: 3306
egress:
- to:
- ipBlock: # the address of external server
cidr: 192.168.5.10/32
ports:
- protocol: TCP
port: 3306