<p>Create an NetworkPolicy named cka-netpol in the namespace netpol.
1] Allow the pods to communicate if they are running on port 8080 within the namespace.
2] Ensure the NetworkPolicy doesn’t allow other pods that are running other than port 8080.
3] The communication from and to the pods running on port 8080. No pods running on port 8080 from other namespaces to allowed.</p>
<p>I want yaml file with some description theoretically.</p>

<blockquote>
<p>Allow the pods to communicate if they are running on port 8080 within
the namespace.</p>
</blockquote>
<p>We will only open and accept requests on port <strong>8080</strong> to satisfy the above request.</p>
<blockquote>
<p>The communication from and to the pods running on port 8080. No pods
running on port 8080 from other namespaces to allowed.</p>
</blockquote>
<p>Using namespace selector to filter out the traffic from specific namespace.</p>
<blockquote>
<p>Ensure the NetworkPolicy doesn’t allow other pods that are running
other than port 8080.</p>
</blockquote>
<p>We have applied the network policy with port as input on the namespace level</p>
<p>check the namespace label</p>
<pre><code>kubectl get namespace netpol --show-labels
</code></pre>
<p>Example YAML</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: cka-netpol
  namespace: netpol
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          namespace: netpol #Use label accordingly
    ports:
      - protocol: TCP
        port: 8080
</code></pre>
<p>You check more example and use this link for ref : <a href="https://github.com/ahmetb/kubernetes-network-policy-recipes/blob/master/09-allow-traffic-only-to-a-port.md" rel="nofollow noreferrer">https://github.com/ahmetb/kubernetes-network-policy-recipes/blob/master/09-allow-traffic-only-to-a-port.md</a></p>
