3.5. Configuring FTP
File Transport Protocol (FTP) is an old and complex multi-port protocol that presents a distinct set of challenges to an Load Balancer environment. To understand the nature of these challenges, you must first understand some key things about how FTP works.
3.5.1. How FTP Works
With most other server client relationships, the client machine opens up a connection to the server on a particular port and the server then responds to the client on that port. When an FTP client connects to an FTP server it opens a connection to the FTP control port 21. Then the client tells the FTP server whether to establish an active or passive connection. The type of connection chosen by the client determines how the server responds and on what ports transactions will occur.
The two types of data connections are:
- Active Connections
- When an active connection is established, the server opens a data connection to the client from port 20 to a high range port on the client machine. All data from the server is then passed over this connection.
- Passive Connections
- When a passive connection is established, the client asks the FTP server to establish a passive connection port, which can be on any port higher than 10,000. The server then binds to this high-numbered port for this particular session and relays that port number back to the client. The client then opens the newly bound port for the data connection. Each data request the client makes results in a separate data connection. Most modern FTP clients attempt to establish a passive connection when requesting data from servers.
Note
The client determines the type of connection, not the server. This means to effectively cluster FTP, you must configure the LVS routers to handle both active and passive connections.
The FTP client-server relationship can potentially open a large number of ports that Keepalived does not know about.
3.5.2. How This Affects Load Balancer Routing
IPVS packet forwarding only allows connections in and out of the cluster based on it recognizing its port number or its firewall mark. If a client from outside the cluster attempts to open a port IPVS is not configured to handle, it drops the connection. Similarly, if the real server attempts to open a connection back out to the Internet on a port IPVS does not know about, it drops the connection. This means all connections from FTP clients on the Internet must have the same firewall mark assigned to them and all connections from the FTP server must be properly forwarded to the Internet using network packet filtering rules.
Note
In order to enable passive FTP connections, you must have the
ip_vs_ftp
kernel module loaded. Run the following commands as an administrative user at a shell prompt to load this module and and ensure that the module loads on a reboot:
echo "ip_vs_ftp" >> /etc/modules-load.d/ip_vs_ftp.conf systemctl enable systemd-modules-load systemctl start systemd-modules-load
3.5.3. Creating Network Packet Filter Rules
Before assigning any
iptables
rules for the FTP service, review the information in Section 3.4, “Multi-port Services and Load Balancer ” concerning multi-port services and techniques for checking the existing network packet filtering rules.
Below are rules which assign the same firewall mark,
21
, to FTP traffic.
3.5.3.1. Rules for Active Connections
The rules for active connections tell the kernel to accept and forward connections coming to the internal floating IP address on port
20
(the FTP data port).
The following
iptables
command allows the LVS router to accept outgoing connections from the real servers that IPVS does not know about:
/usr/sbin/iptables -t nat -A POSTROUTING -p tcp -s n.n.n.0/24 --sport 20 -j MASQUERADE
In the
iptables
command, n.n.n should be replaced with the first three values for the floating IP for the NAT interface's internal network interface defined virtual_server
section of the keepalived.conf
file.
3.5.3.2. Rules for Passive Connections
The rules for passive connections assign the appropriate firewall mark to connections coming in from the Internet to the floating IP address for the service on a wide range of ports: 10,000 to 20,000.
Warning
If you are limiting the port range for passive connections, you must also configure the FTP server,
vsftpd
, to use a matching port range. This can be accomplished by adding the following lines to /etc/vsftpd.conf
:
pasv_min_port=10000
pasv_max_port=20000
Setting
pasv_address
to override the real FTP server address should not be used since it is updated to the virtual IP address by LVS.
For configuration of other FTP servers, consult the respective documentation.
This range should be a wide enough for most situations; however, you can increase this number to include all available non-secured ports by changing
10000:20000
in the commands below to 1024:65535
.
The following
iptables
commands have the net effect of assigning any traffic addressed to the floating IP on the appropriate ports a firewall mark of 21
, which is in turn recognized by IPVS and forwarded appropriately:
/usr/sbin/iptables -t mangle -A PREROUTING -p tcp -d n.n.n.n/32 --dport 21 -j MARK --set-mark 21
/usr/sbin/iptables -t mangle -A PREROUTING -p tcp -d n.n.n.n/32 --dport 10000:20000 -j MARK --set-mark 21
In the
iptables
commands, n.n.n.n should be replaced with the floating IP for the FTP virtual server defined in the virtual_server
subsection of the keepalived.conf
file.
The commands above take effect immediately, but do not persist through a reboot of the system unless they are saved. To save the changes, enter the following command:
# iptables-save > /etc/sysconfig/iptables
To ensure the
iptables
service is started at system start, enter the following command:
# systemctl enable iptables
You can verify whether the changes persist on a reboot by running the following command and checking whether the changes remain:
# systemctl restart iptables