<p>I want to setup <code>statsd-exporter</code> as DaemonSet on my Kubernetes cluster. It exposes a UDP port 9125, on which applications can send metrics using statsD client library. <code>Prometheus</code> crawlers can crawl this exporter for application or system metrics. I want to send the metrics to the UDP server running in the exporter on port 9125. I have two options:</p>

<ol>
<li><p>Expose a service as <code>ClusterIP</code> for the DaemonSet and then configure the statsD clients to use that IP and Port for sending metrics</p></li>
<li><p>Make the <code>statsd-exporter</code> run on <code>hostNetwork</code>, and somehow enable the pods to send metrics to <code>exporter</code> running on the same node. </p></li>
</ol>

<p>Somehow, option 2 seems better, since my pods will be sending metrics to an exporter running on the same node, but I am not able to send metrics to the local pod of <code>statsd-exporter</code> since I don't have the IP of the node the pod is running on.</p>

<p>Can you please compare the pros and cons of both methods, and suggest how can I know the IP address of Node on which the pod is running along with the exporter. </p>

<p><strong>EDIT 1</strong></p>

<p>I am able to get the Node IP by adding the environment variable.</p>

<pre><code>- name: NODE_IP
  valueFrom:
    fieldRef:
      fieldPath: status.hostIP
</code></pre>

<p>I still need clarity on which will be better approach to setting this up. Exposing a service with type <code>ClusterIP</code> and then using the <code>HOST:PORT</code> from environment variable in the pod or use <code>hostNetwork: true</code> in pod spec and then access it using <code>NODE_IP</code> environment variable. Does clusterIp guarantees that the packet will be routed to the same node as the pod sending the packet?</p>

<p><strong>EDIT 2</strong></p>

<p>I explored <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">headless service</a> and I think this comes closest to what I want. But I am not sure that the DNS resolution will return the local node IP as the first result in <code>nslookup</code>. </p>

Abhishek
<p>I would suggest one of the below two approaches, both have their pros and cons.</p>

<ul>
<li>Fast, may not be entirely secure.</li>
</ul>

<p>Daemon set using <code>hostPort</code>. It would be fast as both pods would be on the same node, but the statsd port would be exposed. (You need to secure statsd by some other way)</p>

<ul>
<li>Not as fast as <code>hostPort</code> but secure</li>
</ul>

<p>Exposing a service and using the service dns to connect (<code>servicename.namespace.svc.cluster.local</code>). Not as fast as the <code>hostPort</code> as there is no way to reach specific pod, but secure as no one from outside the cluster can hit the statsd.</p>

<p>More details: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#communicating-with-daemon-pods" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#communicating-with-daemon-pods</a></p>
