why networkpolicy ingress not working for my case

Viewed 52

I made deployment with 2 pods:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx1
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
        ports:
        - containerPort: 80
status: {}

Then exposed it with clusterip, then created a networkpolicy like below:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: nginx1
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend

But when i request it via wget using busybox pod which DOES NOT contain the label (role=frontend), i still getting the html page of nginx.

I would like to know why ?

Any help would be really appriciated. Thank you :)

1 Answers

If your k8s cluster is deployed with Container Network Interface (CNI) plugin that does not support network policies, it will not have an impact on it. From k8s docs:

Network policies are implemented by the network plugin. To use network policies, you must be using a networking solution which supports NetworkPolicy. Creating a NetworkPolicy resource without a controller that implements it will have no effect.

Related