# Load balancing data from Filebeat to Logstash using Nginx

**URL:** <https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473>\
**Category:** Logstash\
**Created:** [May 3, 2019, 6:06am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473 "2019-05-03T06:06:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [May 3, 2019, 6:06am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/1 "2019-05-03T06:06:20Z")

</div>

Hello,

I have ELK cluster with 3 nodes for elasticsearch(master+data) , 2 nodes for logstash(Active and Passive) and 2 for kibana.I have to utilize both the logstash nodes.

I am using filebeat to parse logs and send data to logstash.  
Since there are multiple sources, I am thinking of using nginx as a load balancer.(filebeat -\> nginx -\> logstash1 and logstash2).

I know filebeat can do load balancing and it cannot send output data to nginx, but i want to try it using nginx. Can this be done?  
I have tried this but getting "ERR Failed to publish events caused by: lumberjack protocol error" in filebeat. I have setup the logstash upstream servers in nginx.

Thanks!

---

<div class="post-metadata">

**Author:** ![A\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a_b/32/17104_2.png) [@A\_B](https://discuss.elastic.co/u/A_B)\
**Post date:** [May 3, 2019, 1:09pm UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/2 "2019-05-03T13:09:04Z")

</div>

Hi @Nikhil04,

It might work if you use `nginx` in TCP/streaming mode... But why? As you said, Filebeat can loadbalance over several Logstash instances and Filebeat can back off if Logstash is not keeping up. You probably loose that going through `nginx`

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [May 6, 2019, 7:20am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/3 "2019-05-06T07:20:33Z")

</div>

Hi @A_B,

I am just trying this out to see which load balancing method (filebeat or nginx) will better server my purpose.As you may know, nginx also has more additional features and I would like to test them here.

But first I need to make it work using nginx as LB, I have tried using nginx in TCP/stream mode but it is giving error that it cannot publish events "ERR Failed to publish events caused by: lumberjack protocol error", I am giving IP and port of nginx in filebeat output.

Regards,  
Nikhil

---

<div class="post-metadata">

**Author:** ![A\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a_b/32/17104_2.png) [@A\_B](https://discuss.elastic.co/u/A_B)\
**Post date:** [May 6, 2019, 7:41am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/4 "2019-05-06T07:41:56Z")

</div>

After a quick web search it looks like Filebeat is ~not~ using TCP. The actual network protocol is _lumberjack protocol_. This means `nginx` does not know how to handle it.

I could be completely wrong as I spent all of 2 minutes researching it...

---

<div class="post-metadata">

**Author:** ![A\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a_b/32/17104_2.png) [@A\_B](https://discuss.elastic.co/u/A_B)\
**Post date:** [May 6, 2019, 7:45am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/5 "2019-05-06T07:45:25Z")

</div>

Spent 2 more minutes looking at it as I got curious...

From [Beats protocol](https://discuss.elastic.co/t/beats-protocol/54296)

```
The lumberjack protocol sits on TCP. With TLS support you have one of TCP/lumberjack or TCP/TLS/lumberjack.
```

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [May 7, 2019, 12:33pm UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/6 "2019-05-07T12:33:05Z")

</div>

Hi @A_B

Thanks for the update.  
So do you mean nginx as load balancer sitting between logstash and filebeat should work?

---

<div class="post-metadata">

**Author:** ![A\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a_b/32/17104_2.png) [@A\_B](https://discuss.elastic.co/u/A_B)\
**Post date:** [May 7, 2019, 12:45pm UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/7 "2019-05-07T12:45:49Z")

</div>

No I don't think it can work. But I could be wrong. I have not tested it and have no plans of doing it either so this is partially speculation on my part though 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 4, 2019, 12:45pm UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/8 "2019-06-04T12:45:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 25, 2021, 9:28am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/9 "2021-07-25T09:28:19Z")

</div>



---

<div class="post-metadata">

**Author:** ![saeedahadian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeedahadian/32/92095_2.png) [@saeedahadian](https://discuss.elastic.co/u/saeedahadian)\
**Post date:** [July 26, 2021, 8:05am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/10 "2021-07-26T08:05:31Z")

</div>

You can use Nginx as the load balancer for logstash but as @A_B mentioned, you need to do this on TCP level because filebeat uses `lumberjack` to communicate with logstash which is a protocol that sits on the top of TCP. For TCP load-balancing in Nginx you need to use the `stream` block instead of the `http` block. Your configuration should look something like this:

```auto
stream {
    upstream logstash {
        server logstash-1:5044;
        server logstash-2:5044;
    }

    server {
        listen 5044;
        listen [::]:5044;

        proxy_pass logstash;
    }
}

```

Here I used Docker to create multiple logstash containers that each one uses port 5044. Feel free to change it based on your setup.

---

<div class="post-metadata">

**Author:** ![cknz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cknz/32/9640_2.png) [@cknz](https://discuss.elastic.co/u/cknz)\
**Post date:** [August 9, 2021, 9:35am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/11 "2021-08-09T09:35:16Z")

</div>

Since I don't think its been mentioned in this post yet... some drawbacks that should be made clear:

- Logging often makes use of the source IP (this is particularly true of LAN-based logging protocols such as syslog; less so of logging-as-a-service). Reverse-proxying this Beats traffic will cause Logstash (beats input) to see all traffic as coming from Nginx
- You won't be able to use SSL/TLS client certificates...
- This is very unlikely to be the happy path with regard to tools like Fleet and the Elastic Agent, if that is of interest.
- An alternative design, often desired with TCP reverse-proxying in general, is to use the PROXY protocol, which means that both Nginx (or HAProxy, where this came from) _and_ the backend input (beats input) needs to support PROXY protocol... but that functionality [is not implemented yet in the stock Logstash inputs](https://github.com/logstash-plugins/logstash-input-beats/issues/290).

Alternative designs include:

- using floating IPs (eg. Pacemaker) to float various dedicated service IPs around a cluster a cluster [while not really necessary for Beats, this does prove very useful for other types of logging endpoints, such as for Syslog.
- using DNS Round Robin (really _not_ recommended as a lot of things don't tend to lookup the DNS name very often, particularly network equipment).
- GSLB (see DNS Round Robin, just run faster, unless your head is already firmly stuck in the clouds, in which case it could well be your best friend.)
- layer-4 load-balancing, which is effectively playing tricks with NAT.

Designs to avoid:

- anything where you point clients at Elasticsearch directly [this is a concern regarding trust boundaries]
- anything where you point clients at Kafka directly [concern regarding trust, versioning and robustness]

I would strongly urge you to ensure that you have some sort of persistent queue, either what Logstash provides, or using tools like Kafka (which bring other architectural opportunities).

My own experience tells me that Pacemaker is really quite useful for floating some IPs around a small cluster; I use that to surface logstash (beats), as well as various rsyslogd instances. All of these put their data into Kafka, which then gets consumed by another logstash which does the enrichment and passed it into Elasticsearch. This will very likely be too big and complex for small deployments, but gives a lot of flexibility and control, particularly if you want a place to filter data before sending it into the likes of Elastic Service. Elasticsearch indexing should be your slowest part of your pipeline, so I wouldn't be concerned about scaling out Logstash for performance reasons (just know how not not make regular expressions that suck).

PS. Also worth noting is that 'Beats' is being renamed to 'Elastic Agent'... no idea what implications that would have protocol wise.

If you haven't already, check your thinking against [Deploying and Scaling Logstash | Logstash Reference [7.14] | Elastic](https://www.elastic.co/guide/en/logstash/current/deploying-and-scaling.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 7:06am UTC](https://discuss.elastic.co/t/load-balancing-data-from-filebeat-to-logstash-using-nginx/179473/12 "2022-11-04T07:06:22Z")

</div>


