Linux – Postfix ‘load balance’ sending IPs

emaillinuxpostfix

I got a server with 8 IP addresses to use as a mail server (With PostFix). I want PostFix to rotate the IP and hostname for each message. I found the config parameter

smtp_bind_address = 1.2.3.4

(And there's another one I can't remember that does hostname) But that only lets me bind to one IP/hostname.

Example;
I have these IP's:

1.1.1.1 => mail1.mydomain.com
1.1.1.2 => mail2.mydomain.com
1.1.1.3 => mail3.mydomain.com
[etc]

The first message should be sent from 1.1.1.1, second from 1.1.1.2, third from 1.1.1.3 etc. so just round-robin balancing the avaliable IPs

Is this possible with Postfix?

Best Answer

Postfix can't do that, but you could use iptables' SNAT target in conjunction with the statistics module to rotate your addresses. Something like this should do:

iptables -t nat -A POSTROUTING -p tcp --dport 25 -d <your_dest_mailserver> -m statistic --mode nth --every 8 -j SNAT --to 1.1.1.1
iptables -t nat -A POSTROUTING -p tcp --dport 25 -d <your_dest_mailserver> -m statistic --mode nth --every 8 -j SNAT --to 1.1.1.2
[...]
iptables -t nat -A POSTROUTING -p tcp --dport 25 -d <your_dest_mailserver> -m statistic --mode nth --every 8 -j SNAT --to 1.1.1.8
Related Topic