I have a question which kind of is tricky on what the solution should be: _Netw . . .

Ravi Vijaykumar:
I have a question which kind of is tricky on what the solution should be:

NetworkPolicy which restricts outgoing tcp connections from Deployment frontend and only allows those going to Deployment api which runs on port 2222

Ravi Vijaykumar:
but when i add ports, it does not work any idea why? i.e.

Ravi Vijaykumar:

apiVersion: <http://networking.k8s.io/v1|networking.k8s.io/v1>
kind: NetworkPolicy
metadata:
  name: np1
  namespace: venus
spec:
  podSelector:
    matchLabels:
      id: frontend
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          id: api
    ports:
    - port: 2222
      protocol: TCP
  - ports:
    - port: 53
      protocol: UDP
    - port: 53
      protocol: TCP

Ravi Vijaykumar:
Network policy gets created successfully, but when i use a frontend pod and wget api:2222 it does not work. If i remove ports it works fine.

Ravi Vijaykumar:
@unnivkn if you can guide will be helpful :slightly_smiling_face:

Raamkanna Saranathan:

    ports:
    - port: 2222
      protocol: TCP

remove this… i just solved this question on killer. you’ll see the solution is good except that :slightly_smiling_face:

Raamkanna Saranathan:
the question doesnt say that the port 2222 should be restricted, just that all traffic to the api should be allowed.

Ravi Vijaykumar:
hey @Raamkanna Saranathan that is correct

Ravi Vijaykumar:
even i got it working after removing 2222

Ravi Vijaykumar:
but we should restrict 2222 based on the example they shared isnt it

Ravi Vijaykumar:
also in case we want to restrict by port, why did this not work was my question :slightly_smiling_face:

Ravi Vijaykumar:
@Mumshad Mannambeth or @Tej_Singh_Rana i know you must be busy, but in case you find sometime please help in clarifying this, i have to take exam today would be good to go with better clarity on ports here.