大网数据广告活动
服务器运维问答

Toggle postfix on and off

· 52CE 我爱测

There's probably an answer to this already, but I just can't figure out how to search for it correctly. Situation: We have a failover situation for two nodes, using keepalived , passing a floating IP between them. The main purpos…

问题原文

原帖链接 · Ross Presser · CC BY-SA 3.0

There's probably an answer to this already, but I just can't figure out how to search for it correctly.

Situation:

We have a failover situation for two nodes, using keepalived, passing a floating IP between them. The main purpose of the nodes is to run haproxy. Each node also runs a local postfix that is solely used to deliver email from keepalived or from haproxy, to an actual operator account, relaying through SendGrid to an external mail domain, as it happens. No mail is accepted from any other machine.

All this is working well. Too well; my mailbox is flooded. I would like to suppress mail from the non-active node, and when failover happens, make postfix suddenly start delivering mail.

So here's the idea.

  • normal mode: postfix accepts mail on port 25 and relays through Sendgrid.
  • silent mode: postfix still accepts mail on port 25, but it drops it on the floor, not relaying it anywhere, not returning failure to the client, not sending a bounce. (Yes, this is terrible behavior for an MTA normally. MTAs shouldn't lie about accepting mail. But this is a restricted situation.)
  • How to tell whether I should be in normal or silent mode: a simple script, probably something like ip addr | grep -q $FLOAT_IP.

So ... I guess the basic question is how does "silent mode" happen. Is it a totally separate postfix config, and I have to restart postfix with the right config?

Would it be better to set up an After-Queue Content Filter?

Is there some other idea?

已采纳解答

原帖链接 · Ross Presser · CC BY-SA 3.0

OK, after the hint from @sebix to "use the discard transport", I have something that seems to be working:

Set up a transport map:

# echo >>/etc/postfix/main.cf 'transport_maps = hash:/etc/postfix/transport'

Set up the default transport map as empty; also set up a file for discard transport

# cat </dev/null >/etc/postfix/transport
# cat </dev/null >/etc/postfix/transport.normal
# echo >/etc/postfix/transport.discard '* discard:'

A bash script /usr/local/bin/keepalived_togglemail.sh that keepalived can invoke to switch postfix on or off:

#!/bin/bash

TYPE=$1
NAME=$2
STATE=$3

case $STATE in
        "MASTER") cp /etc/postfix/transport.normal /etc/postfix/transport
                  postmap /etc/postfix/transport
                  /etc/init.d/postfix reload
                  exit 0
                  ;;
        "BACKUP") cp /etc/postfix/transport.discard /etc/postfix/transport
                  postmap /etc/postfix/transport
                  /etc/init.d/postfix reload
                  exit 0
                  ;;
        "FAULT")  echo FAULT
                  exit 0
                  ;;
        *)        echo "unknown state"
                  exit 1
                  ;;
esac

And a line in /etc/keepalived/keepalived.conf to make it happen. (excerpt):

....
vrrp_instance VRRP1 {
     state BACKUP
     interface ens32
     ...
     notify /usr/local/bin/keepalived_togglemail.sh
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。