Connect to a Redis instance

This page describes how to connect to your Redis instance using Telnet and redis-cli. In addition, this page provides instructions for how to connect when using features such as read replicas, AUTH, and in-transit encryption. Finally, this page provides instructions on connecting from a Google Kubernetes Engine environment.

For a list of Google Cloud environments that Memorystore for Redis supports, see Supported environments. Some serverless environments that Memorystore supports require a Serverless VPC Access connector. See Serverless VPC Access connector requirement for more details.

Connecting to a Redis instance from a Compute Engine VM using telnet

You can connect to the Redis instance from any Compute Engine VM that uses the Redis instance's authorized network with a supported RFC 1918 IP address.

  1. If you don't already have a Compute Engine VM that uses that same authorized network as your Redis instance, create one and connect to it by following Quickstart using a Linux VM.

  2. Install telnet using apt-get:

    sudo apt-get install telnet
    
  3. From the terminal, telnet to the IP address of the Redis instance, replacing variables with appropriate values.

    telnet instance-ip-address 6379
    

    If successful, the command will return this result:

    Trying instance-ip-address…
    Connected to instance-ip-address
    
  4. In the telnet session, enter some Redis commands:

    Enter:

    PING
    

    Result:

    PONG
    

    Enter

    SET HELLO WORLD
    

    Result:

    +OK
    

    Enter:

    GET HELLO
    

    Result:

    $5
    WORLD
    

Connecting to a Redis instance from a Compute Engine VM using redis-cli

You can connect to the Redis instance from any Compute Engine VM that uses the Redis instance's authorized network with a supported RFC 1918 IP address.

  1. If you don't already have a Compute Engine VM that uses the authorized network for your Redis instance, create one and connect to it by following Create a VM instance in a specific subnet.

  2. Install redis-cli on the Compute Engine VM by running the following command from the Compute Engine SSH terminal:

    sudo apt-get install redis-tools
    
  3. Run the following command in the Compute Engine terminal, replacing variables with appropriate values.

    redis-cli -h redis-instance-ip-address
    

    You are now connected to your Redis instance using redis-cli.

  4. Once you are done testing your connection to the Redis instance, you should consider deleting the Compute Engine VM you used to connect to the Redis instance. Doing so helps you avoid incurring charges to your Cloud Billing account.

Connecting to a Redis instance that uses AUTH

This section describes how to connect to a Redis instance that has the AUTH feature enabled.

  1. If you don't already have a Compute Engine VM that uses the same authorized network as your Redis instance, create one and connect to it by following Quickstart Using a Linux VM.

  2. Install redis-cli on the Compute Engine VM by running the following command from the Compute Engine SSH terminal:

    sudo apt-get install redis-tools
    
  3. Get your instance's AUTH string

  4. Run the following command to use redis-cli to authenticate and connect to your instance, replacing variables with appropriate values:

    redis-cli -h instance-ip-address -a auth-string
    

    Or:

    redis-cli -h instance-ip-address
    AUTH auth-string
    
  5. Run a Redis SET and GET command to verify that you have established an authenticated connection to your instance.

  6. Once you are done testing your connection to the Redis instance, you should consider deleting the Compute Engine VM you used to connect to the Redis instance. Doing so helps you avoid incurring charges to your Cloud Billing account.

Connecting to a Redis instance using the read endpoint

If your instance has read replicas enabled, you can connect to the read endpoint using the following instructions:

  1. If you don't already have a Compute Engine VM that uses that same authorized network as your Redis instance, create one and connect to it by following Quickstart using a Linux VM.

  2. Install telnet using apt-get:

    sudo apt-get install telnet
    
  3. Find the IP address of your instance's read endpoint by Viewing the read replica information for your instance.

  4. From the terminal, telnet to the read endpoint IP address of the Redis instance, replacing variables with appropriate values.

    telnet read-endpoint-ip 6379
    

    If successful, the command will return this result:

    Trying read-endpoint-ip…
    Connected to read-endpoint-ip
    
  5. In the telnet session, enter some Redis commands:

    Enter:

    PING
    

    Result:

    PONG
    

Securely connecting to a Redis instance using Stunnel and telnet

This section provides an example of how to connect to a Redis instance that has in-transit encryption enabled using Stunnel.

  1. If you don't already have a Redis instance that has in-transit encryption enabled, create one by following Creating a Redis instance with in-transit encryption.

  2. If you don't already have a Compute Engine VM that uses the same authorized network as the Redis instance from the previous step, create one and connect to it by following Quickstart using a Linux VM.

    1. Install the necessary tools for this guide by running the following commands:

      sudo apt-get install stunnel4
      
      sudo apt-get install telnet
      
  3. Configure your Compute Engine VM for use with Stunnel:

    1. Make a new file in which to put your Certificate Authority (CA) by running the following command:

      sudo vim /tmp/server_ca.pem
      
    2. View your Redis instance's Certificate Authority(s) by running the following command in the standard terminal you use to manage Google Cloud resources (not the Compute Engine terminal):

      gcloud redis instances describe instance-id --region=region
      

      There may be up to three CAs.

    3. Copy all of the visible Certificate Authorities including the -----BEGIN CERTIFICATE----- line and the -----END CERTIFICATE----- lines, and paste and save it the server_ca.pem file you previously created.

    4. Make a new file in which to put Stunnel configuration information by running the following command:

      sudo vim /etc/stunnel/redis-client.conf
      

      Paste the following text in the file, replacing redis-instance-ip with the Redis instance's IP address:

      output=/tmp/stunnel.log
      CAfile=/tmp/server_ca.pem
      client=yes
      pid=/var/run/stunnel.pid
      verifyChain=yes
      sslVersion=TLSv1.2
      [redis]
      accept=127.0.0.1:6378
      connect=redis-instance-ip:6378
      

      Start stunnel by running the following command:

      sudo stunnel /etc/stunnel/redis-client.conf
      

      Verify that stunnel is running:

      ps aux | grep stunnel
      

    5. Restart Stunnel by running the following commands:

      sudo systemctl restart stunnel4
      
  4. To connect to your Redis instance using Telnet, run the following command:

    telnet localhost 6378
    
  5. In the Telnet session, ping the Redis instance:

    Enter:

    PING
    

    Result:

    PONG
    

Connecting from a local machine with port forwarding

It can be helpful to connect to your Redis instance from your local machine during development. To do so you must create a Compute Engine instance to use for port forwarding to the Redis instance.

  1. Create a compute engine instance by running the following command:

    gcloud compute instances create COMPUTE_VM_NAME --machine-type=f1-micro --zone=ZONE
    

    Where:

    • COMPUTE_VM_NAME is the name you designate for the Compute Engine VM.
    • ZONE is the zone where you want to provision the VM.

    This command creates a Compute Engine VM with the f1-micro machine type for cost-effectiveness. You can choose other machine types if you wish.

  2. Open a new terminal on your local machine.

  3. To create an SSH tunnel that port forwards traffic through the Compute Engine VM, run the following command:

    gcloud compute ssh COMPUTE_VM_NAME --zone=ZONE -- -N -L 6379:REDIS_INSTANCE_IP_ADDRESS:6379
    

    Where:

    • COMPUTE_VM_NAME is the name of the Compute Engine VM.
    • ZONE is the same zone where the Compute Engine VM is located.
    • REDIS_INSTANCE_IP_ADDRESS is the IP address of your Redis instance.
  4. To test the connection, open a new terminal window and run the following command:

    redis-cli ping
    

    If working properly, the result should return:

    PONG
    

    You are now connected to the Redis instance, and you can send commands to the Redis instance using this open SSH tunnel. If you have not already installed redis-cli, install it so you can run this command.

  5. The SSH tunnel remains open as long as you keep the terminal window with the SSH tunnel connection up and running.

Connecting to a Redis instance from a Google Kubernetes Engine cluster

You can connect to your Redis instance from Google Kubernetes Engine clusters that use the same authorized network as your Redis instance.

Connecting from a pod running a bash shell

  1. Go to the GKE page in the Google Cloud console.
    GKE
  2. Click on the GKE cluster you'd like to connect from. If you don't already have a cluster, create one with IP aliases enabled. For instructions on how to create a cluster with IP aliases enabled, see Creating a VPC-native cluster.
  3. Click the Connect button to the right of your cluster's name, then click the Run in Cloud Shell button in the window that appears.
  4. Configure kubectl command line access by running the following command:
    gcloud container clusters get-credentials CLUSTER_NAME --zone=CLUSTER_ZONE --project=PROJECT_ID
    • CLUSTER_NAME is the name of your cluster.
    • CLUSTER_ZONE is the zone your cluster is in. Must also be the zone your Redis instance is in.
    • PROJECT_ID is the project where your cluster and your Redis instances exist.
    You should get the success message: kubeconfig generated for CLUSTER_NAME
  5. If your cluster is version 1.8 or higher and has IP aliases enabled, skip this step. If your cluster is version 1.7 or lower, or your version 1.8 or higher cluster doesn't have IP aliases enabled, follow these workaround steps before trying to connect to your instance: For more information about IP aliases, including how to create a cluster with this setting enabled, see Creating a VPC-native cluster.
  6. Use the following command to connect to a bash shell:
    kubectl run -i --tty busybox --image=busybox -- sh
  7. Telnet to your Redis instance using the host address and port number:
    telnet HOST-IP PORT
  8. Enter a basic Redis command to verify the connection:
    PING
    +PONG
  9. To close the telnet connection, press Control+C (or Command+C on Mac). To exit the bash shell in your pod, enter return. If you are done and no longer need to connect to Redis, run the following command from the /k8s-custom-iptables/ directory:
    ./uninstall.sh

Connecting to a Redis instance from a Google Kubernetes Engine pod

Before you begin, you need a GKE cluster. If you don't already have a cluster, create one with IP aliases enabled. For instructions on how to create a cluster with IP aliases enabled, see Creating a VPC-native cluster.

  1. From the top-right corner of the console, click the Activate Cloud Shell Activate Cloud Shell button.
  2. Configure kubectl command line access by running the following command:
    gcloud container clusters get-credentials CLUSTER_NAME --zone CLUSTER_ZONE --project PROJECT_ID
    • CLUSTER_NAME is the name of your cluster.
    • CLUSTER_ZONE is the zone your cluster is in. Must also be the zone your Redis instance is in.
    • PROJECT_ID is the project where your cluster and your Redis instances exist.
    You should get the success message: kubeconfig generated for CLUSTER_NAME
  3. If your cluster is version 1.8 or higher and has IP aliases enabled, skip this step. If your cluster is version 1.7 or lower, or your version 1.8 or higher cluster doesn't have IP aliases enabled, follow these workaround steps before trying to connect to your instance: For more information about IP aliases, including how to create a cluster with this setting enabled, see Creating a VPC-native cluster.
  4. Use the following command to start a Redis pod running the redis-cli:
    kubectl run -i --tty redisbox --image=gcr.io/google_containers/redis:v1 -- sh
  5. Run a redis-cli command, replacing HOST-IP with the host IP address of your Redis instance:
    redis-cli -h HOST-IP info
  6. Optionally, run a Redis benchmark command, again replacing HOST-IP with the host IP address of your Redis instance:
    redis-benchmark -c 100 -n 100000 -d 1024 -r 100000 -t PING,SET,GET,INCR,LPUSH,RPUSH,LPOP,RPOP,SADD,SPOP,MSET -h HOST-IP -q
  7. To exit the Redis pod, enter return. If you are done and no longer need to connect to Redis, run the following command from the /k8s-custom-iptables/ directory:
    ./uninstall.sh

What's next

Connect to your Redis instance using a sample application that runs on: