Update on Dec 20th, 2022: this blog article is now on Medium.

By default the cartservice of the Online Boutique sample stores its data in an in-cluster Redis database. Using a fully managed database service outside your GKE cluster such as Memorystore (Redis) could bring more resiliency, scalability and more security.

Reduce latency with scalable, secure, and highly available in-memory service for Redis.

In this article, let’s see how you can connect the Online Boutique sample to Google Cloud Memorystore (Redis).

Architecture overview.

Objectives

  • Create a Google Kubernetes Engine (GKE) cluster
  • Provision a Memorystore (Redis) instance
  • Deploy the Online Boutique sample connected to the Memorystore (Redis) instance

Costs

This tutorial uses billable components of Google Cloud, including the following:

Use the pricing calculator to generate a cost estimate based on your projected usage.

Before you begin

This guide assumes that you have owner IAM permissions for your Google Cloud project. In production, you do not require owner permission.

  1. Select or create a Google Cloud project.

  2. Verify that billing is enabled for your project.

Set up your environment

Initialize the common variables used throughout this tutorial:

PROJECT_ID=FIXME-WITH-YOUR-PROJECT-ID
REGION=us-east5
ZONE=us-east5-a

To avoid repeating the --project in the commands throughout this tutorial, let’s set the current project:

gcloud config set project ${PROJECT_ID}

Enable the required APIs in your project

Enable the required APIs in your project:

gcloud services enable \
    redis.googleapis.com \
    container.googleapis.com

Create a GKE cluster

CLUSTER=memorystore-with-onlineboutique
gcloud container clusters create ${CLUSTER} \
    --zone ${ZONE} \
    --machine-type=e2-standard-4 \
    --num-nodes 4 \
    --network default

Provision the Memorystore (Redis) instance

REDIS_NAME=memorystore-with-onlineboutique
gcloud redis instances create ${REDIS_NAME} \
    --size 1 \
    --region ${REGION} \
    --zone ${ZONE} \
    --redis-version redis_6_x \
    --network default

Notes:

  • You can connect to a Memorystore (Redis) instance from GKE clusters that are in the same region and use the same network as your instance.
  • You cannot connect to a Memorystore (Redis) instance from a GKE cluster without VPC-native/IP aliasing enabled.

Wait for the Memorystore (Redis) instance to be succesfully provisioned and then get the connection information (private IP address and port):

REDIS_IP=$(gcloud redis instances describe ${REDIS_NAME} \
    --region ${REGION} \
    --format 'get(host)')
REDIS_PORT=$(gcloud redis instances describe ${REDIS_NAME} \
    --region ${REGION} \
    --format 'get(port)')

Deploy Online Boutique connected to the Memorystore (Redis) instance

Deploy the Online Boutique sample apps without the default in-cluster Redis database, and now pointing to the Memorystore (Redis) instance:

NAMESPACE=onlineboutique
helm upgrade onlineboutique oci://us-docker.pkg.dev/online-boutique-ci/charts/onlineboutique \
    --install \
    --create-namespace \
    -n ${NAMESPACE} \
    --set cartDatabase.inClusterRedis.create=false \
    --set cartDatabase.connectionString=${REDIS_IP}:${REDIS_PORT}

After all the apps have successfully been deployed, you could navigate to the Online Boutique website by clicking on the link below:

echo -n "http://" && kubectl get svc frontend-external -n ${NAMESPACE} -o json | jq -r '.status.loadBalancer.ingress[0].ip'

And voilà, that’s how easy you can connect your Online Boutique sample apps to a Memorystore (Redis) database, congrats!

Cleaning up

To avoid incurring charges to your Google Cloud account, you can delete the resources used in this tutorial.

Delete the GKE cluster:

gcloud container clusters delete ${CLUSTER} \
    --zone ${ZONE}

Delete the Memorystore (redis) instance:

gcloud redis instances delete ${REDIS_NAME}

Conclusion

Having the database outside of your GKE cluster with a managed service can bring you more resiliency, scalability and security. This setup allows complex scenarios like having your apps spreaded across multiple clusters, etc.

Further resources

Happy sailing, cheers!