<p>I have a mosquitto broker running on a pod, this server is exposed as a service as both DNS and IP address.</p>
<p>But this service is accessible by any pod in the cluster.</p>
<p>I want to restrict access to this service such that pods trying to connect to this DNS or IP address should only be able to if the pods have certain name/metadata.</p>
<p>One solution I guess will be to use namespaces? What other solution is there?</p>

<p>The UseCase you are describing is exactly what <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/" rel="noreferrer">NetworkPolicies</a> are here for.</p>
<p>Basically you define selector for pods which the network traffic should be restricted (i.e. your mosquito broker) and what specifica pods need to have in order to be allowed to reach it. For example a label <code>&quot;broker-access: true&quot;</code> or whatever seems to be suitable for you.</p>
<p>an example network policy could look like this:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: broker-policy
  namespace: default  
spec:
  podSelector:
    matchLabels:
      role: message-broker
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          broker-access: true
    ports:
    - protocol: TCP
      port: 6379
</code></pre>
<p>this network policy would be applied to every pod with label <code>role=message-broker</code>.
and it would restrict all incoming traffic except for traffic from pods with label <code>broker-acces=true</code> on port 6379.</p>
<p>Hope this helps and gives you a bit of a skaffold for your NetworkPolicy</p>
