Skip to content

Commit 79c3f79

Browse files
jabubakeandrewsg
authored andcommitted
Adding Google Cloud Memorystore sample (GoogleCloudPlatform#1474)
1 parent 4987e3c commit 79c3f79

File tree

9 files changed

+321
-0
lines changed

9 files changed

+321
-0
lines changed

memorystore/redis/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Getting started with Googe Cloud Memorystore
2+
Simple HTTP server example to demonstrate connecting to [Google Cloud Memorystore](https://cloud.google.com/memorystore/docs/redis).
3+
This sample uses the [redis-py client](https://github.com/andymccurdy/redis-py).
4+
5+
## Running on GCE
6+
7+
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.
8+
9+
## Running on GKE
10+
11+
Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gke) to deploy the sample application on GKE.
12+
13+
## Running on Google App Engine Flex
14+
15+
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.

memorystore/redis/app.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START memorystore_app_yaml]
15+
runtime: python
16+
env: flex
17+
entrypoint: gunicorn -b :$PORT main:app
18+
19+
runtime_config:
20+
python_version: 3
21+
22+
# update with Redis instance host IP, port
23+
env_variables:
24+
REDISHOST: redis-ip
25+
REDISPORT: 6379
26+
27+
# update with Redis instance network name
28+
network:
29+
name: default
30+
31+
#[END memorystore_app_yaml]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
# Copyright 2018 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# [START memorystore_deploy_sh]
17+
if [ -z "$REDISHOST" ]; then
18+
echo "Must set \$REDISHOST. For example: REDISHOST=127.0.0.1"
19+
exit 1
20+
fi
21+
22+
if [ -z "$REDISPORT" ]; then
23+
echo "Must set \$REDISPORT. For example: REDISPORT=6379"
24+
exit 1
25+
fi
26+
27+
if [ -z "$GCS_APP_LOCATION" ]; then
28+
echo "Must set \$GCS_APP_LOCATION. For example: GCS_APP_LOCATION=gs://my-bucket/app"
29+
exit 1
30+
fi
31+
32+
if [ -z "$ZONE" ]; then
33+
ZONE=$(gcloud config get-value compute/zone -q)
34+
echo $ZONE
35+
fi
36+
37+
#Upload the tar to GCS
38+
tar -cvf app.tar -C .. requirements.txt main.py
39+
# Copy to GCS bucket
40+
gsutil cp app.tar $GCS_APP_LOCATION
41+
42+
# Create an instance
43+
gcloud compute instances create my-instance \
44+
--image-family=debian-8 \
45+
--image-project=debian-cloud \
46+
--machine-type=g1-small \
47+
--scopes cloud-platform \
48+
--metadata-from-file startup-script=startup-script.sh \
49+
--metadata app-location=$GCS_APP_LOCATION,redis-host=$REDISHOST,redis-port=$REDISPORT \
50+
--zone $ZONE \
51+
--tags http-server
52+
53+
gcloud compute firewall-rules create allow-http-server-8080 \
54+
--allow tcp:8080 \
55+
--source-ranges 0.0.0.0/0 \
56+
--target-tags http-server \
57+
--description "Allow port 8080 access to http-server"
58+
# [END memorystore_deploy_sh]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /bin/bash
2+
3+
# Copyright 2018 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# [START memorystore_startup_script_sh]
17+
set -v
18+
19+
# Talk to the metadata server to get the project id and location of application binary.
20+
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
21+
GCS_APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
22+
REDISHOST=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-host" -H "Metadata-Flavor: Google")
23+
REDISPORT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-port" -H "Metadata-Flavor: Google")
24+
25+
# Install dependencies from apt
26+
apt-get update
27+
apt-get install -yq \
28+
git build-essential supervisor python python-dev python-pip libffi-dev \
29+
libssl-dev
30+
31+
# Install logging monitor. The monitor will automatically pickup logs send to
32+
# syslog.
33+
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
34+
service google-fluentd restart &
35+
36+
gsutil cp $GCS_APP_LOCATION /app.tar
37+
mkdir -p /app
38+
tar -x -f /app.tar -C /app
39+
cd /app
40+
41+
# Install the app dependencies
42+
pip install --upgrade pip virtualenv
43+
virtualenv /app/env
44+
/app/env/bin/pip install -r /app/requirements.txt
45+
46+
# Create a pythonapp user. The application will run as this user.
47+
getent passwd pythonapp || useradd -m -d /home/pythonapp pythonapp
48+
chown -R pythonapp:pythonapp /app
49+
50+
# Configure supervisor to run the Go app.
51+
cat >/etc/supervisor/conf.d/pythonapp.conf << EOF
52+
[program:pythonapp]
53+
directory=/app
54+
environment=HOME="/home/pythonapp",USER="pythonapp",REDISHOST=$REDISHOST,REDISPORT=$REDISPORT
55+
command=/app/env/bin/gunicorn main:app --bind 0.0.0:8080
56+
autostart=true
57+
autorestart=true
58+
user=pythonapp
59+
stdout_logfile=syslog
60+
stderr_logfile=syslog
61+
EOF
62+
63+
supervisorctl reread
64+
supervisorctl update
65+
# [END memorystore_startup_script_sh]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# Copyright 2018 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# [START memorystore_teardown_sh]
17+
gcloud compute instances delete my-instance
18+
19+
gcloud compute firewall-rules delete allow-http-server-8080
20+
# [END memorystore_teardown_sh]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The Google App Engine python runtime is Debian Jessie with Python installed
2+
# and various os-level packages to allow installation of popular Python
3+
# libraries. The source is on github at:
4+
# https://github.com/GoogleCloudPlatform/python-docker
5+
FROM gcr.io/google_appengine/python
6+
7+
# Create a virtualenv for the application dependencies.
8+
# If you want to use Python 2, add the -p python2.7 flag.
9+
RUN virtualenv -p python3.4 /env
10+
11+
# Set virtualenv environment variables. This is equivalent to running
12+
# source /env/bin/activate. This ensures the application is executed within
13+
# the context of the virtualenv and will have access to its dependencies.
14+
ENV VIRTUAL_ENV /env
15+
ENV PATH /env/bin:$PATH
16+
17+
# Note: REDISHOST value here is only used for local testing
18+
# See README.md on how to inject environment variable as ConfigMap on GKE
19+
ENV REDISHOST 127.0.0.1
20+
ENV REDISPORT 6379
21+
22+
# Install dependencies.
23+
ADD requirements.txt /app/requirements.txt
24+
RUN pip install -r /app/requirements.txt
25+
26+
# Add application code.
27+
ADD . /app
28+
29+
CMD ["gunicorn", "-b", "0.0.0.0:8080", "main:app"]
30+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: visit-counter
5+
labels:
6+
app: visit-counter
7+
spec:
8+
replicas: 1
9+
template:
10+
metadata:
11+
labels:
12+
app: visit-counter
13+
spec:
14+
containers:
15+
- name: visit-counter
16+
image: "gcr.io/<PROJECT-ID>/visit-counter:v1"
17+
env:
18+
- name: REDISHOST
19+
valueFrom:
20+
configMapKeyRef:
21+
name: redishost
22+
key: REDISHOST
23+
ports:
24+
- name: http
25+
containerPort: 8080
26+
---
27+
apiVersion: v1
28+
kind: Service
29+
metadata:
30+
name: visit-counter
31+
spec:
32+
type: LoadBalancer
33+
selector:
34+
app: visit-counter
35+
ports:
36+
- port: 80
37+
targetPort: 8080
38+
protocol: TCP
39+

memorystore/redis/main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2018 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# [START memorystore_main_py]
14+
import logging
15+
import os
16+
17+
from flask import Flask
18+
import redis
19+
20+
app = Flask(__name__)
21+
22+
redis_host = os.environ.get('REDISHOST', 'localhost')
23+
redis_port = int(os.environ.get('REDISPORT', 6379))
24+
redis_client = redis.StrictRedis(host=redis_host, port=redis_port)
25+
26+
27+
@app.route('/')
28+
def index():
29+
value = redis_client.incr('counter', 1)
30+
return 'Visitor number: {}'.format(value)
31+
32+
33+
@app.errorhandler(500)
34+
def server_error(e):
35+
logging.exception('An error occurred during a request.')
36+
return """
37+
An internal error occurred: <pre>{}</pre>
38+
See logs for full stacktrace.
39+
""".format(e), 500
40+
41+
42+
if __name__ == '__main__':
43+
# This is used when running locally. Gunicorn is used to run the
44+
# application on Google App Engine. See entrypoint in app.yaml.
45+
app.run(host='127.0.0.1', port=8080, debug=True)
46+
# [END memorystore_main_py]

memorystore/redis/requirements.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2018 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# [START memorystore_requirements]
14+
Flask==0.12.2
15+
gunicorn==19.7.1
16+
redis==2.10.5
17+
# [END memorystore_requirements]

0 commit comments

Comments
 (0)