Disclaimer: This article may contain information about third-party products. Such information is for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the performance and reliability of third-party products, as well as potential impacts of operations on the products.

Introduction

This topic describes how to build a proxy service on an ECS instance so that external devices can connect to an apsaradb for Redis instance over the proxy service.

Background

Check whether the current environment meets the implementation conditions. Perform operations based on the operating system type of the ECS instance.

Prerequisites

To use an ECS instance as a proxy server to access an apsaradb for Redis instance and perform data operations, you can configure port mapping or port forwarding on the ECS instance. Provided that you meet the following prerequisites:

Note: If you encounter any connection problems, see Redis connection troubleshooting.

  • If the apsaradb for Redis instance stays in a Virtual Private Cloud (VPC), the ECS instance and the apsaradb for Redis instance must stay in the same VPC.
  • If the apsaradb for Redis instance belongs to the classic network, the ECS instance and apsaradb for Redis instance belong to the same classic network and run in the same region.
  • You have added the private IP address of the ECS instance that connects to databases to a whitelist of the target apsaradb for Redis instance. For more information, see configure IP whitelists.
  • You have added security group rules to the security group of the ECS instance to allow connections to the ECS instance from the specified public IP address and the private IP address of the apsaradb for Redis instance. For more information about how to configure security group rules, see add security group rules.

Windows-based ECS instances

To access apsaradb for Redis through a Windows system as a proxy, you can use the netsh command to perform port mapping in the Windows ECS. Take port 6379 of ECS instance as an example.

  1. Log on to the Windows Server and run cmd to enter the command line window.
  2. Run the following command to configure port mapping:
    netsh interface portproxy add v4tov4 listenaddress=[$ECS_IP] listenport=6379 connectaddress=[$Redis_Address] connectport=6379
    Note:
    • [$ECS_IP] is the public IP address of the ECS instance. [$Redis_Address] is the internal endpoint address of the apsaradb for Redis instance.
    • The PortProxy feature provided by Microsoft is not an open-source program. If you have any issues when you configure this feature, see PortProxy related to the netsh command or contact Microsoft customer service. You can also configure port mapping in other ways. For example, configure proxy mapping by using PortMap.
    The following command output is returned.
  3. Run the following command to view the mappings of servers.
    netsh interface portproxy show all
  4. After the settings are complete, test the connectivity locally.
    Note: for example, if the Windows Server IP address is 1.1.1.1, run the telnet 1.1.1.1 6379 command to connect to the ECS instance.
    1. Run the redis-cli command to connect to the ECS proxy server.
    2. After connecting to the ECS proxy server, run the following command to enter the password for connecting to the Redis database.
      auth [$Redis_Password]
      Note:[$Redis_Password] is the password used to connect to the apsaradb for Redis instance.
    3. After the client connects to the Redis database, it checks and writes data to the database.
  5. To delete Internet forwarding rules, run the following command to delete mappings that are no longer in use.
    netsh interface portproxy delete v4tov4 listenaddress=[$ECS_IP] listenport=6379

Linux ECS instances.

To use a Linux as a proxy to access apsaradb for Redis, you can install rinetd on your Linux ECS instance.

  1. Log on to the Linux server and run the following commands in sequence to install rinetd:
    wget https://github.com/samhocevar/rinetd/releases/download/v0.70/rinetd-0.70.tar.gz&&tar -xvf rinetd-0.70.tar.gz&&cd rinetd-0.70
    sed -i 's/65536/65535/g' rinetd.c
    mkdir /usr/man
    ./configure
    make&&make install
    Note:
    • If you cannot download the Rinetd installation package from the command prompt, you can search for the corresponding package.
    • Rinetd is an open-source program. If you have any questions, reference Rinetd or contact Rinetd technical support.
  2. Run the following command to edit the rinetd.conf file:
    vi /etc/rinetd.conf
    Add the following content to the configuration file.
    0.0.0.0 6379 [$Redis_Address] 6379 logfile /var/log/rinetd.log
    Note: You can run cat/etc/rinetd.conf to check whether the configuration file is correctly modified.
    The following command output is returned.
  3. Run the following command to start rinetd:
    rinetd
    Note:
    • You can run the following command to set Rinetd to automatically start:
      echo rinetd >>/etc/rc.local
    • If a binding error occurs, you can run the pkilrinetd command to end the process, and then run the rinetd command to rinetd start the rinetd process.
    • After the rinetd process starts normally, run the following command to confirm that the service is running normally:
      netstat -anp | grep 6379
      The following command output is returned.
  4. Test connection conditions on the local PC.
    1. You can run the redis-cli command on your local machine to connect to the Linux server for logon verification. For example, if the IP address of the server on which Rinetd is installed is 1.1.1.1, run the redis-cli -h 1.1.1.1 command to connect to the server . Or by telent command to connect to the Linux ECS server after Operation verification, it is assumed that the Linux server's IP addresses 1.1.1.1, namely, the implementation of telnet 1.1.1.1 6379 command to connect to the server .

    2. After connecting to the ECS server, run the following command to enter the password to connect to Redis.
      auth [$Redis_Password]
    3. After the client connects to the Redis database, it checks and writes data to the database.

Application scope

  • ApsaraDB for Redis