title: Setup statsd-exporter as daemon on Kubernetes and send metrics to it from pods
body: <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>