From 91d092f298cbf31a3733d52777caa517530e9636 Mon Sep 17 00:00:00 2001 From: jsimonweb Date: Tue, 8 May 2018 15:18:28 -0700 Subject: [PATCH 1/2] Add code sample for Google Cloud Memorystore. --- memorystore/redis/README.md | 16 +++++ memorystore/redis/app.yaml | 25 ++++++++ memorystore/redis/gce_deployment/deploy.sh | 58 +++++++++++++++++ .../redis/gce_deployment/startup-script.sh | 63 +++++++++++++++++++ memorystore/redis/gce_deployment/teardown.sh | 20 ++++++ memorystore/redis/gke_deployment/Dockerfile | 26 ++++++++ .../redis/gke_deployment/visit-counter.yaml | 39 ++++++++++++ memorystore/redis/package.json | 18 ++++++ memorystore/redis/server.js | 40 ++++++++++++ 9 files changed, 305 insertions(+) create mode 100644 memorystore/redis/README.md create mode 100644 memorystore/redis/app.yaml create mode 100644 memorystore/redis/gce_deployment/deploy.sh create mode 100644 memorystore/redis/gce_deployment/startup-script.sh create mode 100644 memorystore/redis/gce_deployment/teardown.sh create mode 100644 memorystore/redis/gke_deployment/Dockerfile create mode 100644 memorystore/redis/gke_deployment/visit-counter.yaml create mode 100644 memorystore/redis/package.json create mode 100644 memorystore/redis/server.js diff --git a/memorystore/redis/README.md b/memorystore/redis/README.md new file mode 100644 index 0000000000..1b9643c616 --- /dev/null +++ b/memorystore/redis/README.md @@ -0,0 +1,16 @@ +# Getting started with Googe Cloud Memorystore +Simple HTTP server example to demonstrate connecting to Google Cloud Memorystore. +This sample uses the [node_redis client](https://github.com/NodeRedis/node_redis). + +## Running on GCE + +Follow the instructions in [this guide](TODO-UPDATE-LINK) to deploy the sample application on a GCE VM. + +## Running on GKE + +Follow the instructions in [this guide](TODO-UPDATE-LINK) to deploy the sample application on GKE. + +## Running on Google App Engine Flex + +Follow the instructions in [this guide](TODO-UPDATE-LINK) to deploy the sample application on GKE. + diff --git a/memorystore/redis/app.yaml b/memorystore/redis/app.yaml new file mode 100644 index 0000000000..39df59c40c --- /dev/null +++ b/memorystore/redis/app.yaml @@ -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 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 app_yaml] diff --git a/memorystore/redis/gce_deployment/deploy.sh b/memorystore/redis/gce_deployment/deploy.sh new file mode 100644 index 0000000000..b8817932e0 --- /dev/null +++ b/memorystore/redis/gce_deployment/deploy.sh @@ -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 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 deploy_sh] diff --git a/memorystore/redis/gce_deployment/startup-script.sh b/memorystore/redis/gce_deployment/startup-script.sh new file mode 100644 index 0000000000..88ec106fa3 --- /dev/null +++ b/memorystore/redis/gce_deployment/startup-script.sh @@ -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 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 startup_script_sh] diff --git a/memorystore/redis/gce_deployment/teardown.sh b/memorystore/redis/gce_deployment/teardown.sh new file mode 100644 index 0000000000..d72147b4ae --- /dev/null +++ b/memorystore/redis/gce_deployment/teardown.sh @@ -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 teardown_sh] +gcloud compute instances delete my-instance + +gcloud compute firewall-rules delete allow-http-server-8080 +# [END teardown_sh] diff --git a/memorystore/redis/gke_deployment/Dockerfile b/memorystore/redis/gke_deployment/Dockerfile new file mode 100644 index 0000000000..660483283f --- /dev/null +++ b/memorystore/redis/gke_deployment/Dockerfile @@ -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 + diff --git a/memorystore/redis/gke_deployment/visit-counter.yaml b/memorystore/redis/gke_deployment/visit-counter.yaml new file mode 100644 index 0000000000..d9623e390d --- /dev/null +++ b/memorystore/redis/gke_deployment/visit-counter.yaml @@ -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//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 + diff --git a/memorystore/redis/package.json b/memorystore/redis/package.json new file mode 100644 index 0000000000..01dd93fb0c --- /dev/null +++ b/memorystore/redis/package.json @@ -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" + } +} + diff --git a/memorystore/redis/server.js b/memorystore/redis/server.js new file mode 100644 index 0000000000..0a191d1eee --- /dev/null +++ b/memorystore/redis/server.js @@ -0,0 +1,40 @@ +/** + * 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 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 server_js] From ed21adedca30e5fd7d475c67386189e8e700d243 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Tue, 8 May 2018 22:06:06 -0700 Subject: [PATCH 2/2] addressing review comments adding links --- memorystore/redis/README.md | 9 ++++----- memorystore/redis/app.yaml | 4 ++-- memorystore/redis/gce_deployment/deploy.sh | 4 ++-- memorystore/redis/gce_deployment/startup-script.sh | 4 ++-- memorystore/redis/gce_deployment/teardown.sh | 4 ++-- memorystore/redis/server.js | 7 +++---- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/memorystore/redis/README.md b/memorystore/redis/README.md index 1b9643c616..1bf67442da 100644 --- a/memorystore/redis/README.md +++ b/memorystore/redis/README.md @@ -1,16 +1,15 @@ # Getting started with Googe Cloud Memorystore -Simple HTTP server example to demonstrate connecting to Google 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](TODO-UPDATE-LINK) to deploy the sample application on a GCE VM. +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](TODO-UPDATE-LINK) to deploy the sample application 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](TODO-UPDATE-LINK) to deploy the sample application on GKE. - +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. diff --git a/memorystore/redis/app.yaml b/memorystore/redis/app.yaml index 39df59c40c..c4676c428e 100644 --- a/memorystore/redis/app.yaml +++ b/memorystore/redis/app.yaml @@ -10,7 +10,7 @@ # 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 app_yaml] +# [START memorystore_app_yaml] runtime: nodejs env: flex @@ -22,4 +22,4 @@ env_variables: # update with Redis instance network name network: name: default -# [END app_yaml] +# [END memorystore_app_yaml] diff --git a/memorystore/redis/gce_deployment/deploy.sh b/memorystore/redis/gce_deployment/deploy.sh index b8817932e0..0cb81d7f42 100644 --- a/memorystore/redis/gce_deployment/deploy.sh +++ b/memorystore/redis/gce_deployment/deploy.sh @@ -13,7 +13,7 @@ # 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 deploy_sh] +# [START memorystore_deploy_sh] if [ -z "$REDISHOST" ]; then echo "Must set \$REDISHOST. For example: REDISHOST=127.0.0.1" exit 1 @@ -55,4 +55,4 @@ gcloud compute firewall-rules create allow-http-server-8080 \ --source-ranges 0.0.0.0/0 \ --target-tags http-server \ --description "Allow port 8080 access to http-server" -# [END deploy_sh] +# [END memorystore_deploy_sh] diff --git a/memorystore/redis/gce_deployment/startup-script.sh b/memorystore/redis/gce_deployment/startup-script.sh index 88ec106fa3..cd5dbe5036 100644 --- a/memorystore/redis/gce_deployment/startup-script.sh +++ b/memorystore/redis/gce_deployment/startup-script.sh @@ -13,7 +13,7 @@ # 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 startup_script_sh] +# [START memorystore_startup_script_sh] set -ex # Talk to the metadata server to get the project id and location of application binary. @@ -60,4 +60,4 @@ EOF supervisorctl reread supervisorctl update -# [END startup_script_sh] +# [END memorystore_startup_script_sh] diff --git a/memorystore/redis/gce_deployment/teardown.sh b/memorystore/redis/gce_deployment/teardown.sh index d72147b4ae..b54a91fb21 100644 --- a/memorystore/redis/gce_deployment/teardown.sh +++ b/memorystore/redis/gce_deployment/teardown.sh @@ -13,8 +13,8 @@ # 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 teardown_sh] +# [START memorystore_teardown_sh] gcloud compute instances delete my-instance gcloud compute firewall-rules delete allow-http-server-8080 -# [END teardown_sh] +# [END memorystore_teardown_sh] diff --git a/memorystore/redis/server.js b/memorystore/redis/server.js index 0a191d1eee..a3e6df09d7 100644 --- a/memorystore/redis/server.js +++ b/memorystore/redis/server.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// [START server_js] +// [START memorystore_server_js] 'use strict'; const http = require('http'); const redis = require('redis'); @@ -23,9 +23,8 @@ const REDISPORT = process.env.REDISPORT || 6379; const client = redis.createClient(REDISPORT, REDISHOST); client.on('error', (err) => console.error('ERR:REDIS:', err)); -//create a server +// create a server http.createServer((req, res) => { - // increment the visit counter client.incr('visits', (err, reply) => { if (err) { @@ -37,4 +36,4 @@ http.createServer((req, res) => { res.end(`Visitor number: ${reply}\n`); }); }).listen(8080); -// [END server_js] +// [END memorystore_server_js]