Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code sample for Google Cloud Memorystore. #614

Merged
merged 2 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions memorystore/redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Getting started with Googe Cloud Memorystore
Simple HTTP server example to demonstrate connecting to [Google Cloud Memorystore](https://cloud.google.com/memorystore/docs/redis).
This sample uses the [node_redis client](https://github.com/NodeRedis/node_redis).

## Running on GCE

Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gce) to deploy the sample application on a GCE VM.

## Running on GKE

Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gke) to deploy the sample application on GKE.

## Running on Google App Engine Flex

Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex) to deploy the sample application on GAE Flex.
25 changes: 25 additions & 0 deletions memorystore/redis/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2018 Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START memorystore_app_yaml]
runtime: nodejs
env: flex

# update with Redis instance details
env_variables:
REDISHOST: '127.0.0.1'
REDISPORT: '6379'

# update with Redis instance network name
network:
name: default
# [END memorystore_app_yaml]
58 changes: 58 additions & 0 deletions memorystore/redis/gce_deployment/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START memorystore_deploy_sh]
if [ -z "$REDISHOST" ]; then
echo "Must set \$REDISHOST. For example: REDISHOST=127.0.0.1"
exit 1
fi

if [ -z "$REDISPORT" ]; then
echo "Must set \$REDISPORT. For example: REDISPORT=6379"
exit 1
fi

if [ -z "$GCS_APP_LOCATION" ]; then
echo "Must set \$GCS_APP_LOCATION. For example: GCS_APP_LOCATION=gs://my-bucket/app"
exit 1
fi

if [ -z "$ZONE" ]; then
ZONE=$(gcloud config get-value compute/zone -q)
echo $ZONE
fi

#Upload the tar to GCS
tar -cvf app.tar -C .. package.json server.js
# Copy to GCS bucket
gsutil cp app.tar $GCS_APP_LOCATION

# Create an instance
gcloud compute instances create my-instance \
--image-family=debian-8 \
--image-project=debian-cloud \
--machine-type=g1-small \
--scopes cloud-platform \
--metadata-from-file startup-script=startup-script.sh \
--metadata app-location=$GCS_APP_LOCATION,redis-host=$REDISHOST,redis-port=$REDISPORT \
--zone $ZONE \
--tags http-server

gcloud compute firewall-rules create allow-http-server-8080 \
--allow tcp:8080 \
--source-ranges 0.0.0.0/0 \
--target-tags http-server \
--description "Allow port 8080 access to http-server"
# [END memorystore_deploy_sh]
63 changes: 63 additions & 0 deletions memorystore/redis/gce_deployment/startup-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#! /bin/bash

# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START memorystore_startup_script_sh]
set -ex

# Talk to the metadata server to get the project id and location of application binary.
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
GCS_APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
REDISHOST=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-host" -H "Metadata-Flavor: Google")
REDISPORT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-port" -H "Metadata-Flavor: Google")

# Install dependencies from apt
apt-get update
# Install Node.js 9
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
apt-get install -yq ca-certificates supervisor nodejs build-essential


# Install logging monitor. The monitor will automatically pickup logs send to
# syslog.
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
service google-fluentd restart &

gsutil cp $GCS_APP_LOCATION /app.tar
mkdir -p /app
tar -x -f /app.tar -C /app
cd /app
# Install the app dependencies
npm install

# Create a nodeapp user. The application will run as this user.
getent passwd nodeapp || useradd -m -d /home/nodeapp nodeapp
chown -R nodeapp:nodeapp /app

# Configure supervisor to run the Go app.
cat >/etc/supervisor/conf.d/nodeapp.conf << EOF
[program:nodeapp]
directory=/app
environment=HOME="/home/nodeapp",USER="nodeapp",REDISHOST=$REDISHOST,REDISPORT=$REDISPORT
command=node server.js
autostart=true
autorestart=true
user=nodeapp
stdout_logfile=syslog
stderr_logfile=syslog
EOF

supervisorctl reread
supervisorctl update
# [END memorystore_startup_script_sh]
20 changes: 20 additions & 0 deletions memorystore/redis/gce_deployment/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START memorystore_teardown_sh]
gcloud compute instances delete my-instance

gcloud compute firewall-rules delete allow-http-server-8080
# [END memorystore_teardown_sh]
26 changes: 26 additions & 0 deletions memorystore/redis/gke_deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2018, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM node:6-alpine

ENV REDISHOST redis
ENV REDISPORT 6379

COPY package.json .
RUN npm install

COPY server.js .
CMD node server.js

EXPOSE 8080

39 changes: 39 additions & 0 deletions memorystore/redis/gke_deployment/visit-counter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: visit-counter
labels:
app: visit-counter
spec:
replicas: 1
template:
metadata:
labels:
app: visit-counter
spec:
containers:
- name: visit-counter
image: "gcr.io/<PROJECT_ID>/visit-counter:v1"
env:
- name: REDISHOST
valueFrom:
configMapKeyRef:
name: redishost
key: REDISHOST
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: visit-counter
spec:
type: LoadBalancer
selector:
app: visit-counter
ports:
- port: 80
targetPort: 8080
protocol: TCP

18 changes: 18 additions & 0 deletions memorystore/redis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "memorystore-redis",
"description": "An example of using Memorystore(Redis) with Node.js",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": ">=6"
},
"devDependencies": {
"lint": "^1.1.2"
},
"dependencies": {
"redis": "2.6.5"
}
}

39 changes: 39 additions & 0 deletions memorystore/redis/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2018 Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START memorystore_server_js]
'use strict';
const http = require('http');
const redis = require('redis');

const REDISHOST = process.env.REDISHOST || 'localhost';
const REDISPORT = process.env.REDISPORT || 6379;

const client = redis.createClient(REDISPORT, REDISHOST);
client.on('error', (err) => console.error('ERR:REDIS:', err));

// create a server
http.createServer((req, res) => {
// increment the visit counter
client.incr('visits', (err, reply) => {
if (err) {
console.log(err);
res.status(500).send(err.message);
return;
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Visitor number: ${reply}\n`);
});
}).listen(8080);
// [END memorystore_server_js]