<p><strong>EDIT : I harcoded the fluentd service IP directly in my express app and its working.. how to get it work without harcoding ip?</strong></p>
<p>I have a couple of pods <strong>(nodejs + express server)</strong> running on a Kubernetes cluster.</p>
<p>I'd like send logs from my <strong>nodejs pods</strong> to a <strong>Fluentd DeamonSet</strong>.</p>
<p>But I'm getting this error :</p>
<p><code>Fluentd error Error: connect ECONNREFUSED 127.0.0.1:24224</code></p>
<p>I'm using <a href="https://github.com/fluent/fluent-logger-node" rel="nofollow noreferrer">https://github.com/fluent/fluent-logger-node</a> and my configuration is pretty simple:</p>
<pre><code>const logger = require('fluent-logger')

logger.configure('pptr', {
   host: 'localhost',
   port: 24224,
   timeout: 3.0,
   reconnectInterval: 600000
});
</code></pre>
<p>My fluentd conf file:</p>
<pre><code>&lt;source&gt;
  @type forward
  port 24224
  bind 0.0.0.0
&lt;/source&gt;

# Ignore fluent logs
&lt;label @FLUENT_LOG&gt;
  &lt;match fluent.*&gt;
    @type null
  &lt;/match&gt;
&lt;/label&gt;

&lt;match pptr.**&gt;
  @type elasticsearch
  host &quot;#{ENV['FLUENT_ELASTICSEARCH_HOST']}&quot;
  port &quot;#{ENV['FLUENT_ELASTICSEARCH_PORT']}&quot;
  scheme &quot;#{ENV['FLUENT_ELASTICSEARCH_SCHEME'] || 'http'}&quot;
  ssl_verify &quot;#{ENV['FLUENT_ELASTICSEARCH_SSL_VERIFY'] || 'true'}&quot;
  user &quot;#{ENV['FLUENT_ELASTICSEARCH_USER']}&quot;
  password &quot;#{ENV['FLUENT_ELASTICSEARCH_PASSWORD']}&quot;
  reload_connections &quot;#{ENV['FLUENT_ELASTICSEARCH_RELOAD_CONNECTIONS'] || 'true'}&quot;
  type_name fluentd
  logstash_format true
&lt;/match&gt;
</code></pre>
<p>Here's the Fluentd DeamonSet config file:</p>
<pre><code>apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
    version: v1
spec:
  selector:
    matchLabels:
      k8s-app: fluentd-logging
      version: v1
  template:
    metadata:
      labels:
        k8s-app: fluentd-logging
        version: v1
    spec:
      serviceAccount: fluentd
      serviceAccountName: fluentd
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      containers:
        - name: fluentd
          image: fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch
          ports:
            - containerPort: 24224
          env:
            - name:  FLUENT_ELASTICSEARCH_HOST
              value: &quot;xxx&quot;
            - name:  FLUENT_ELASTICSEARCH_PORT
              value: &quot;xxx&quot;
            - name: FLUENT_ELASTICSEARCH_SCHEME
              value: &quot;https&quot;
            # Option to configure elasticsearch plugin with self signed certs
            # ================================================================
            - name: FLUENT_ELASTICSEARCH_SSL_VERIFY
              value: &quot;true&quot;
            # Option to configure elasticsearch plugin with tls
            # ================================================================
            - name: FLUENT_ELASTICSEARCH_SSL_VERSION
              value: &quot;TLSv1_2&quot;
            # X-Pack Authentication
            # =====================
            - name: FLUENT_ELASTICSEARCH_USER
              value: &quot;xxx&quot;
            - name: FLUENT_ELASTICSEARCH_PASSWORD
              value: &quot;xxx&quot;
          resources:
            limits:
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 200Mi
          volumeMounts:
            - name: config-volume
              mountPath: /fluentd/etc/kubernetes.conf
              subPath: kubernetes.conf
            - name: varlog
              mountPath: /var/log
            - name: varlibdockercontainers
              mountPath: /var/lib/docker/containers
              readOnly: true
      terminationGracePeriodSeconds: 30
      volumes:
        - name: config-volume
          configMap:
            name: fluentd-conf
        - name: varlog
          hostPath:
            path: /var/log
        - name: varlibdockercontainers
          hostPath:
            path: /var/lib/docker/containers
</code></pre>
<p>I also tried to deploy a service and expose the <strong>24224</strong> port :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: fluentd
  namespace: kube-system
  labels:
    app: fluentd
spec:
  ports:
    - name: &quot;24224&quot;
      port: 24224
      targetPort: 24224
  selector:
    k8s-app: fluentd-logging
status:
  loadBalancer: {}
</code></pre>
<p>Finally my express app (deployment) is here:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: puppet
  labels:
    app: puppet
spec:
  replicas: 5
  selector:
    matchLabels:
      app: puppet
  template:
    metadata:
      labels:
        app: puppet
    spec:
      containers:
        - name: puppet
          image: myrepo/my-image
          ports:
            - containerPort: 8080
</code></pre>
<p><strong>EDIT : I harcoded the fluentd service IP directly in my express app and its working.. how to get it work without harcoding ip?</strong></p>

MichelDelpech
<p>Focusing on below parts of the question:</p>
<blockquote>
<p>I'd like send logs from my nodejs pods to a Fluentd DeamonSet.</p>
</blockquote>
<blockquote>
<p>EDIT : I harcoded the fluentd service IP directly in my express app and its working.. how to get it work without harcoding ip?</p>
</blockquote>
<p>It looks like the communication between pods and the <code>fluentd</code> service is correct (hardcoding the IP works).  The issue here is the way they can communicate with each other.</p>
<p>You can communicate with service <code>fluentd</code> by its name. For example (from the inside of a pod):</p>
<ul>
<li><code>curl fluentd:24224</code></li>
</ul>
<p><strong>You can communicate with services by its name (like <code>fluentd</code>) only in the same namespace.</strong> If a service is in another namespace you would need to use it's full DNS name. It's template and example is following:</p>
<ul>
<li>template: <code>service-name.namespace.svc.cluster.local</code></li>
<li>example: <code>fluentd.kube-system.svc.cluster.local</code></li>
</ul>
<p>You can also use service of type <code>ExternalName</code> to map the full DNS name of your service to a shorter version like below:</p>
<hr />
<p>Assuming that (example):</p>
<ul>
<li>You have created a <code>nginx-namespace</code> namespace:
<ul>
<li><code>$ kubectl create namespace nginx-namespace</code></li>
</ul>
</li>
<li>You have an <code>nginx</code> <code>Deployment</code> inside the <code>nginx-namespace</code> and a service associated with it:
<ul>
<li><code>$ kubectl create deployment nginx --image=nginx --namespace=nginx-namespace</code></li>
<li><code>$ kubectl expose deployment nginx --port=80 --type=ClusterIP --namespace=nginx-namespace</code></li>
</ul>
</li>
<li>You want to communicate with <code>nginx</code> <code>Deployment</code> from another namespace (i.e. <code>default</code>)</li>
</ul>
<p>You have an option to communicate with above pod:</p>
<ul>
<li>By the IP address of a <code>Pod</code>
<ul>
<li><code>10.98.132.201</code></li>
</ul>
</li>
<li>By a (full) DNS service name
<ul>
<li><code>nginx.nginx-namespace.svc.cluster.local</code></li>
</ul>
</li>
<li>By an <code>ExternalName</code> type of service that points to a a (full) DNS service name
<ul>
<li><code>nginx-service</code></li>
</ul>
</li>
</ul>
<p>The example of <code>ExternalName</code> type of service:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: default # &lt;- the same as the pod communicating with the service
spec:
  type: ExternalName
  externalName: nginx.nginx-namespace.svc.cluster.local
</code></pre>
<p>You can pass this information to the pod by either:</p>
<ul>
<li>Environment variable: <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">Kubernetes.io: Define environment variable container</a></li>
<li>ConfigMap: <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Kubernetes.io: Configure pod configmap</a></li>
</ul>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://stackoverflow.com/a/44329470/12257134">Stackoverflow.com: Service located in another namespace</a></em></li>
</ul>
