iptables Forwarding

Last updated: 2024-11-01 17:05:35

Scenario

TencentDB for Redis® supports public network access in Chengdu, Beijing, Shanghai, and Guangzhou regions. To use public network access in other regions, you can use a CVM instance with a public IP for port forwarding to access TencentDB for Redis® over the public network.
Note:
Port forwarding with iptables is not stable, so we do not recommend this public network access solution in a production environment.


Instructions

1. Log in to the Cloud Server and enable the IP forwarding feature for the CVM instance.
Note:
The CVM and TencentDB instances must be under the same account and in the same VPC in the same region, or both in the classic network.
echo 1 > /proc/sys/net/ipv4/ip_forward
2. Configure the forwarding rule. The following sample code is to forward access requests of 26.xx.x.2:10001 (CVM public IP and customizable port) to a Redis instance whose private IP and port are 10.0.0.5:6379.
iptables -t nat -A PREROUTING -p tcp --dport 10001 -j DNAT --to-destination 10.0.0.5:6379
iptables -t nat -A POSTROUTING -d 10.0.0.5 -p tcp --dport 6379 -j MASQUERADE
3. Configure the CVM security group to allow access to the public network ports of the CVM instance. The security group rules recommend you to only open the source addresses that need to be accessed.
4. To connect to the private network Redis instance via the public network address (in this example, 26.xx.xx.2:10001), use the same connection command as for private network access. For the command, see Connecting with Client Tools.
5. After connecting to TencentDB for Redis®, running the info command and receiving database-related information indicates a successful connection.