diff --git a/memorystore/redis/README.md b/memorystore/redis/README.md new file mode 100644 index 0000000000..1bf67442da --- /dev/null +++ b/memorystore/redis/README.md @@ -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. diff --git a/memorystore/redis/app.yaml b/memorystore/redis/app.yaml new file mode 100644 index 0000000000..c4676c428e --- /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 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] diff --git a/memorystore/redis/gce_deployment/deploy.sh b/memorystore/redis/gce_deployment/deploy.sh new file mode 100644 index 0000000000..0cb81d7f42 --- /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 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] diff --git a/memorystore/redis/gce_deployment/startup-script.sh b/memorystore/redis/gce_deployment/startup-script.sh new file mode 100644 index 0000000000..cd5dbe5036 --- /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 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] diff --git a/memorystore/redis/gce_deployment/teardown.sh b/memorystore/redis/gce_deployment/teardown.sh new file mode 100644 index 0000000000..b54a91fb21 --- /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 memorystore_teardown_sh] +gcloud compute instances delete my-instance + +gcloud compute firewall-rules delete allow-http-server-8080 +# [END memorystore_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..a3e6df09d7 --- /dev/null +++ b/memorystore/redis/server.js @@ -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]