You can only connect to your Redis instance from Google Kubernetes Engine clusters that use the same authorized network as the Redis instance.
Setup
If you have already installed the Google Cloud CLI and have created a Redis instance, 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.
Preparing your GKE cluster
If you have not created a GKE cluster, create one using the following commands for the Google Cloud CLI:
Designate the project for this sample application in
gcloud
.gcloud config set project [PROJECT_ID]
Set the Compute Engine zone configuration variable in
gcloud
.gcloud config set compute/zone [ZONE]
Create a GKE cluster called
visitcount-cluster
.gcloud container clusters create visitcount-cluster --num-nodes=3 --enable-ip-alias
If you did not create the cluster via
gcloud
, use this command to retrieve the cluster credentials:gcloud container clusters get-credentials [CLUSTER_NAME] --zone [CLUSTER_ZONE] --project [PROJECT_ID]
- CLUSTER_NAME is the name of your GKE cluster.
- CLUSTER_ZONE is the zone your cluster is in.
- PROJECT_ID is the project where your cluster and your Redis instances exist.
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.
Run these commands, replacing RESERVED_IP_RANGE with 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 cd ..
If you don't know the reserved IP range of your instance, you can find out via the console (advanced options) or by entering this command:
gcloud redis instances describe INSTANCE_ID --region=REGION
For more information about IP aliases, including how to create a cluster with this setting enabled, see the IP aliases documentation.
Sample application
This sample HTTP server application establishes a connection to a Redis instance from a Google Kubernetes Engine cluster.
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.
Building the container image
Build and push the container image to Container Registry:
cp gke_deployment/Dockerfile .
export PROJECT_ID="$(gcloud config get-value project -q)"
docker build -t gcr.io/${PROJECT_ID}/visit-counter:v1 .
gcloud docker -- push gcr.io/${PROJECT_ID}/visit-counter:v1
Deploying your application to Google Kubernetes Engine
Updategke_deployment/visit-counter.yaml
replacing <PROJECT_ID>
with your Google Cloud Project ID. This file contains the configuration for the deployment and service.
Go
To avoid hard-coding the Redis instance IP, you can create a redishost ConfigMap:
export REDISHOST_IP=XXX.XXX.XXX.XXX
kubectl create configmap redishost --from-literal=REDISHOST=${REDISHOST_IP}
Verify the configuration using the following command:
kubectl get configmaps redishost -o yaml
Java
Node.js
To avoid hard-coding the Redis instance IP, you can create a redishost ConfigMap:
export REDISHOST_IP=XXX.XXX.XXX.XXX
kubectl create configmap redishost --from-literal=REDISHOST=${REDISHOST_IP}
Verify the configuration using the following command:
kubectl get configmaps redishost -o yaml
Python
To avoid hard-coding the Redis instance IP, you can create a redishost ConfigMap:
export REDISHOST_IP=XXX.XXX.XXX.XXX
kubectl create configmap redishost --from-literal=REDISHOST=${REDISHOST_IP}
Verify the configuration using the following command:
kubectl get configmaps redishost -o yaml
Apply the configuration to your cluster:
kubectl apply -f gke_deployment/visit-counter.yaml
Determine the [EXTERNAL-IP] address for this sample app by running this command:
kubectl get service visit-counter
See your app hosted at http://[EXTERNAL-IP] via your browser, or send a GET request via cURL or your browser:
curl http://[EXTERNAL-IP]
Removing the IP tables entry for the Redis instance
If you followed step three of the section of this walkthrough called Preparing your GKE cluster, you
installed the reserved IP range of your Redis instance to your
GKE instance's IP tables. If you want to remove this Redis
IP range entry from the IP tables of your GKE instance, run
the following command from the k8s-custom-iptables/
directory:
./uninstall.sh