I came across this question in killercoda
my-app-deployment
and cache-deployment
deployed, and my-app-deployment
deployment exposed through a service named my-app-service
. Create a NetworkPolicy named my-app-network-policy
to restrict incoming and outgoing traffic to my-app-deployment
pods with the following specifications:
- Allow incoming traffic only from pods within the same namespace.
- Allow incoming traffic from a specific pod with the label
app=trusted
- Allow outgoing traffic to pods within the same namespace.
- Deny all other incoming and outgoing traffic.
I used this netpol and it fails
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-app-network-policy
namespace: default
spec:
podSelector:
matchLabels:
app: my-app
policyTypes:
- Ingress
- Egress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: default
- podSelector:
matchLabels:
app: trusted
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: default
whereas the below works and I Don’t understand how, can someone pls explain?
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-app-network-policy
namespace: default
spec:
podSelector:
matchLabels:
app: my-app
ingress:
- from:
- podSelector: {}
- from:
- podSelector:
matchLabels:
app: trusted
egress:
- to:
- podSelector: {}