Connect to a Redis instance Stay organized with collections Save and categorize content based on your preferences.
This page describes how to connect to your Redis instance using Telnet andredis-cli. In addition, this page provides instructions for how to connectwhen using features such as read replicas, AUTH, and in-transit encryption.Finally, this page provides instructions on connecting from a Google Kubernetes Engineenvironment.
For a list of Google Cloud environments that Memorystore for Redis supports, seeSupported environments. Someserverless environments that Memorystore supports require aServerless VPC Access connector. SeeServerless VPC Access connector requirementfor more details.
Note: If your instance usescustomer-managed encryption keys (CMEK)and theCloud Key Management Service API is unavailable because of anoutage, then when you reconnect to your instance, you might need to use adifferent IP address.Connect to a Redis instance from a Compute Engine VM using telnet
You can connect to the Memorystore for Redis instance from any Compute EngineVM that uses the instance's authorized network with asupported RFC 1918 IP address.
If you don't have a Compute Engine VM that uses that same authorizednetwork as your instance, then create one and connect to the VM by using SSH. Todo this, follow the steps inCreate a Linux VM instance in Compute Engine.
Use
apt-getto installtelnet:sudo apt-get install telnetFrom the terminal, telnet to the IP address of the instance. ReplaceVARIABLES with appropriate values.
telnetINSTANCE_IP_ADDRESS 6379
If you can connect to the instance, then the command returns this result:
TryingINSTANCE_IP_ADDRESS…Connected toINSTANCE_IP_ADDRESS
In the telnet session, enter some Redis commands:
Enter:
PING
Result:
PONG
Enter
SET HELLO WORLD
Result:
+OK
Enter:
GET HELLO
Result:
$5WORLD
Connect to a Redis instance from a Compute Engine VM usingredis-cli
You can connect to the Redis instance from any Compute Engine VMthat uses the Redis instance's authorized network with asupported RFC 1918 IP address.
If you don't already have a Compute Engine VM that uses the authorizednetwork for your Redis instance, create one and connect to it by followingCreate a VM instance in a specific subnet.
Install
redis-clion the Compute Engine VM by running the followingcommand from the Compute Engine SSH terminal:sudo apt-get install redis-tools
Run the following command in theCompute Engine terminal,replacingvariables with appropriate values.
redis-cli -hredis-instance-ip-address
You are now connected to your Redis instance using
redis-cli.Once you are done testing your connection to the Redis instance, you shouldconsiderdeleting the Compute Engine VMyou used to connect to the Redis instance. Doing so helps you avoid incurringcharges to your Cloud Billing account.
Connect to a Redis instance that uses AUTH
This section describes how to connect to a Redis instance that has theAUTHfeature enabled.
If you don't already have a Compute Engine VM that uses the same authorizednetwork as your Redis instance, create one and connect to it by followingQuickstart Using a Linux VM.
Install
redis-clion the Compute Engine VM by running the followingcommand from the Compute Engine SSH terminal:sudo apt-get install redis-tools
Get your instance's AUTH string
Run the following command to use
redis-clito authenticate and connect toyour instance, replacingvariables with appropriate values:redis-cli -hinstance-ip-address -aauth-string
Or:
redis-cli -hinstance-ip-addressAUTHauth-string
Run a Redis
SETandGETcommand to verify that you have established anauthenticated connection to your instance.Once you are done testing your connection to the Redis instance, you shouldconsiderdeleting the Compute Engine VMyou used to connect to the Redis instance. Doing so helps you avoid incurringcharges to your Cloud Billing account.
Connect to a Redis instance using the read endpoint
If your instance hasread replicasenabled, you can connect to the read endpoint using the following instructions:
If you don't already have a Compute Engine VM that uses that sameauthorized network as your Redis instance, create one and connect to it byfollowingQuickstart using a Linux VM.
Install
telnetusingapt-get:sudo apt-get install telnetTo find the IP address of your instance's read endpoint,view read replica information for your instance.
From the terminal, telnet to the read endpoint IP address of the Redis instance, replacingvariables with appropriate values.
telnetread-endpoint-ip 6379
If successful, the command will return this result:
Tryingread-endpoint-ip…Connected toread-endpoint-ip
In the telnet session, enter some Redis commands:
Enter:
PING
Result:
PONG
Connect to a Redis instance securely by using Stunnel and telnet
This section provides an example of how to connect to a Redis instance that hasin-transit encryption enabled usingStunnel.
If you don't already have a Redis instance that has in-transit encryptionenabled, create one by followingCreating a Redis instance with in-transit encryption.
If you don't already have a Compute Engine VM that uses the same authorizednetwork as the Redis instance from the previous step, create one andconnect to it by followingQuickstart using a Linux VM.
Install the necessary tools for this guide by running the followingcommands:
sudo apt-get install stunnel4
sudo apt-get install telnet
Configure your Compute Engine VM for use with Stunnel:
Make a new file in which to put yourCertificate Authority(CA) by running the following command:
sudo vim /tmp/server_ca.pem
View your Redis instance's Certificate Authority(s) by running thefollowing command in the standard terminal you use to manageGoogle Cloud resources (not the Compute Engine terminal):
gcloud redis instances describeinstance-id --region=region
There may be up to three CAs.
Copy all of the visible Certificate Authorities including the
Note: Make sure that the text of the Certificate Authority iscompletely left justified. There should be no leading whitespace on anyline of the CA.-----BEGIN CERTIFICATE-----line and the-----END CERTIFICATE-----lines, and paste and save it theserver_ca.pemfile you previouslycreated.Make a new file in which to put Stunnel configuration information byrunning the following command:
sudo vim /etc/stunnel/redis-client.conf
Paste the following text in the file, replacingredis-instance-ipwith the Redis instance's IP address:
output=/tmp/stunnel.logCAfile=/tmp/server_ca.pemclient=yespid=/var/run/stunnel.pidverifyChain=yessslVersion=TLSv1.2[redis]accept=127.0.0.1:6378connect=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
Restart Stunnel by running the following commands:
Note: You can use thesudo systemctl restart stunnel4
ps aux | grep stunnelcommand beforeand after restarting stunnel to confirm that the restart was successful.If the stunell process ID, shown by theps aux | grep stunnelcommand, is different then you know stunell has been restartedsuccessfully.
To connect to your Redis instance using Telnet, run the following command:
telnet localhost 6378
In the Telnet session, ping the Redis instance:
Enter:
PING
Result:
PONG
Connect from a local machine by using port forwarding
It can be helpful to connect to your Redis instance from your local machineduring development. To do so you must create a Compute Engine instance to usefor port forwarding to the Redis instance.
Create a Compute Engine instance by running the following command:
gcloud compute instances createCOMPUTE_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-micromachinetypefor cost-effectiveness. You can choose other machine types.Open a new terminal on your local machine.
To create an SSH tunnel that port forwards traffic through theCompute Engine VM, run the following command:
gcloud compute sshCOMPUTE_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 islocated.
- REDIS_INSTANCE_IP_ADDRESS is the IP address of your Redisinstance.
To test the connection, open a new terminal window and run the followingcommand:
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 theRedis instance using this open SSH tunnel. If you have not already installed
redis-cli, install it so you can run this command.The SSH tunnel remains open as long as you keep the terminal window with theSSH tunnel connection up and running.
Connect to a Redis instance from a Google Kubernetes Engine cluster
Note: To connect to a Redis instance that uses theprivate services access connect mode you must haveVPC-native/IP aliasing enabled on your GKE cluster. To useGKE to connect to a Redis instance that uses thedirect peering connect mode,use the workaround in step 5.To verify if your cluster uses IP aliasing, usegcloud container clusters describe.
You can connect to your Redis instance from Google Kubernetes Engineclusters that use the same authorized network as your Redis instance.
Connect from a pod running a bash shell
- Go to the GKE page in the Google Cloud console.
GKE - Click 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, seeCreating a VPC-native cluster.
- Click theConnect button to the right of your cluster's name, then click theRun in Cloud Shell button in the window that appears.
- Configure
kubectlcommand line access by running the following command:gcloud container clusters get-credentialsCLUSTER_NAME --zone=CLUSTER_ZONE --project=PROJECT_ID
CLUSTER_NAMEis the name of your cluster.CLUSTER_ZONEis the zone your cluster is in. Must also be the zone your Redis instance is in.PROJECT_IDis the project where your cluster and your Redis instances exist.
kubeconfig generated forCLUSTER_NAME - If your cluster is version 1.8 or higherand 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, seeCreating a VPC-native cluster.Workaround
Run the following commands, replacing
RESERVED_IP_RANGEwith the reserved IP range of your instance:git clone https://github.com/bowei/k8s-custom-iptables.git
cd k8s-custom-iptables/
TARGETS="RESERVED_IP_RANGE" ./install.sh
If you don't know the reserved IP range of your instance, you can find out by entering this command:
gcloud redis instances describeINSTANCE_ID --region=REGION - Use the following command to connect to a bash shell:
kubectl run -i --tty busybox --image=busybox -- sh
- Telnet to your Redis instance using the host address and port number:
telnetHOST-IPPORT
- Enter a basic Redis command to verify the connection:
PING+PONG
- 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
Connect to a Redis instance from a Google Kubernetes Engine pod
Before you begin, you need a GKE cluster. If you don'talready have a cluster, create one with IP aliases enabled. For instructions on how to create a clusterwith IP aliases enabled, seeCreating a VPC-native cluster.
- From the top-right corner of the console, clickActivate Cloud Shell
button. - Configure
kubectlcommand line access by running the following command:gcloud container clusters get-credentialsCLUSTER_NAME --zoneCLUSTER_ZONE --projectPROJECT_ID
CLUSTER_NAMEis the name of your cluster.CLUSTER_ZONEis the zone your cluster is in. Must also be the zone your Redis instance is in.PROJECT_IDis the project where your cluster and your Redis instances exist.
kubeconfig generated forCLUSTER_NAME - If your cluster is version 1.8 or higherand 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, seeCreating a VPC-native cluster.Workaround
Run the following commands, replacing
RESERVED_IP_RANGEwith the reserved IP range of your instance:git clone https://github.com/bowei/k8s-custom-iptables.git
cd k8s-custom-iptables/
TARGETS="RESERVED_IP_RANGE" ./install.sh
If you don't know the reserved IP range of your instance, you can find out by entering this command:
gcloud redis instances describeINSTANCE_ID --region [REGION] - Use the following command to start a Redis pod running the
redis-cli:kubectl run -i --tty redisbox --image=redis:7.2 -- sh
- Run a
redis-clicommand, replacingHOST-IPwith the host IP address of your Redis instance:redis-cli -hHOST-IP info
- Optionally, run aRedis benchmark command, again replacing
HOST-IPwith 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 -hHOST-IP -q
- 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:
- Compute Engine
- GKE
- Cloud Run
- App Engine standard environment
- App Engine flexible environment
- Cloud Run functions
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.