Connecting to a Redis instance

You can access Redis instances from Compute Engine VM instances, Google Kubernetes Engine clusters, Cloud Functions, the App Engine flexible environment, and the App Engine standard environment.

Before you can access Cloud Memorystore with Cloud Functions, you need follow the setup instructions at Connecting to internal resources in a VPC network.

Also, before you can access Cloud Memorystore with the App Engine standard environment, you need to follow the setup instructions at Connecting to internal resources in a VPC network.

Connecting to a Redis instance from a Compute Engine VM

You can connect to the Redis instance from any Compute Engine VM instance located within the same project, region and network as the Redis instance.

  1. Follow the instructions at Quickstart Using a Linux VM to create and connect to a Compute Engine VM instance. Make sure that you create your Compute Engine VM instance in the same region as your Redis instance. If you used a Debian 8 image to create your VM instance as described in the quickstart, install telnet using apt-get:

    sudo apt-get install telnet
    
  2. From the terminal, telnet to the IP address of the Redis instance, substituting your own instance's IP address and port number.

    telnet 10.0.0.27 6379
    Trying 10.0.0.27…
    Connected to 10.0.0.27
    
  3. In the telnet session, enter some Redis commands:

    PING
    +PONG
    SET HELLO WORLD
    +OK
    GET HELLO
    $5
    WORLD
    

You can also use the Redis client to issue commands to the Redis instance. For more information on how to use the Redis client, see the Redis Quick Start Guide.

Connecting to a Redis instance from a Google Kubernetes Engine cluster

You can connect to your Redis instances from Google Kubernetes Engine clusters that are in the same region and use the same network as your instance.

Connecting from a pod running a bash shell

  1. Go to the GKE page in the GCP Console.
    GKE
  2. Click on the GKE cluster you'd like to connect from. If you don't already have a cluster, create one in the same zone and region as your Redis instance with IP aliases enabled. For instructions on how to create a cluster with IP aliases enabled, see the IP aliases documentation.
  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 the IP aliases documentation.
  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 in the same zone and region as your Redis instance with IP aliases enabled. For instructions on how to create a cluster with IP aliases enabled, see the IP Aliases documentation.

  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 the IP aliases documentation.
  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:

Kunde den här sidan hjälpa dig? Berätta:

Skicka feedback om ...

Google Cloud Memorystore for Redis