You can connect to your Redis instance from Compute Engine VM instances that use the same authorized network as your Redis instance.
Setup
If you have already installed the Google Cloud CLI, created a Redis instance, and created a Cloud Storage bucket you can skip these steps.
Install the gcloud CLI and initialize:
gcloud init
Follow the Quickstart Guide to create a Redis instance. Take note of the zone, IP address, and port of the Redis instance.
Create a Cloud Storage bucket where the application artifact for this sample application will be uploaded. For more, see Create buckets.
Configuring the gcloud settings for the sample application
- Set the
gcloud
default project to your sample app project.gcloud config set project [PROJECT_ID]
Sample application
This sample HTTP server application establishes a connection to a Redis instance from a Compute Engine VM instance.
Clone the repository for your desired programming language and navigate to the folder that contains the sample code:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples
cd golang-samples/memorystore/redis
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/memorystore/redis
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/memorystore/redis
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/memorystore/redis
This sample application increments a Redis counter every time the /
endpoint
is accessed.
Go
This application uses the
github.com/gomodule/redigo/redis
client. Install it by running the following command:
go get github.com/gomodule/redigo/redis
Java
This application is Jetty 3.1 servlet-based.
It uses the Jedis library:
The AppServletContextListener
class is used to create a long-lived
Redis connection pool:
The VisitCounterServlet
class is a web servlet that increments
a Redis counter:
Node.js
This application uses the redis
module.
Python
This application uses Flask
for web serving and the redis-py
package to communicate with the Redis instance.
Deploying the application to a Compute Engine VM
Navigate to the gce_deployment
directory:
cd gce_deployment
The deployment script uploads the artifact to the Cloud Storage path. It then launches a Compute Engine instance, creating a firewall to expose the port 8080. Then the startup script prepares the instance.
Set the REDISHOST
and REDISPORT
environmental variables:
export REDISHOST=[REDISHOST] export REDISPORT=[REDISPORT]
where:
REDISHOST
is the managed Redis instance IP.REDISPORT
is the managed Redis instance port by default 6379.
Set the GCS_BUCKET_NAME
environmental variable:
export GCS_BUCKET_NAME=[BUCKET_NAME]/[PATH]
where:
BUCKET_NAME
is the name of your Cloud Storage bucket.PATH
is an optional path for the directory where you want to store the application artifact.
Here is a sample deployment script that deploys this application on a new Compute Engine VM instance.
Go
Java
Node.js
Python
Run the deployment script:
chmod +x deploy.sh
./deploy.sh
Application startup script
This startup script is used in the sample deployment script to prepare the instance. For more information about start up scripts and viewing startup script execution logs, see Running Startup Scripts.
Go
Java
Node.js
Python
It may take several minutes for the sample application to deploy on the newly created Compute Engine instance.
Once the instance is ready and the startup script has completed execution, go to the Compute Engine instances page and copy the External Ip address.
To view the sample app you deployed, visit http://[EXTERNAL-IP]:8080
You can use the teardown.sh
script to delete the instance and firewall created by the deployment script: