Skip to content

Commit

Permalink
adding code sample for Google Cloud Memorystore
Browse files Browse the repository at this point in the history
  • Loading branch information
jabubake committed May 8, 2018
1 parent de00f10 commit 085792e
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 0 deletions.
17 changes: 17 additions & 0 deletions memorystore/redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Getting started with Googe Cloud Memorystore
Simple HTTP server example to demonstrate connecting to Google Cloud Memorystore.
This sample uses the [Jedis client](https://mvnrepository.com/artifact/redis.clients/jedis).
Please see other client library options [here](https://redis.io/clients#java).

## 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.

17 changes: 17 additions & 0 deletions memorystore/redis/flex_deployment/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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: java
env: flex
# [END app_yaml]
60 changes: 60 additions & 0 deletions memorystore/redis/gce_deployment/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/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 "$GCS_APP_LOCATION" ]; then
if [ -z "$BUCKET"]; then
echo "Must set \$BUCKET. For example: BUCKET=my-bucket-name"
exit 1
fi
GCS_APP_LOCATION="gs://$BUCKET/gce/"
echo $GCS_APP_LOCATION
fi

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

if [ -z "$WAR" ]; then
WAR=visitcounter-1.0-SNAPSHOT.war
fi

#Build the WAR package
cd ..
mvn clean package

#Copy the WAR artifact to the GCS bucket location
gsutil cp -r target/${WAR} ${GCS_APP_LOCATION}

cd gce_deployment

# 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},app-war=$WAR \
--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]
78 changes: 78 additions & 0 deletions memorystore/redis/gce_deployment/startup-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#! /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")
WAR=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-war" -H "Metadata-Flavor: Google")

gsutil cp "$GCS_APP_LOCATION"** .

# Install dependencies from apt
apt-get update
apt install -t jessie-backports -yq openjdk-8-jre-headless ca-certificates-java
apt install -t jessie-backports -yq openjdk-8-jdk

# Make Java8 the default
update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

# Jetty Setup
mkdir -p /opt/jetty/temp
mkdir -p /var/log/jetty

# Get Jetty
curl -L https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.4.v20170414/jetty-distribution-9.4.4.v20170414.tar.gz -o jetty9.tgz
tar xf jetty9.tgz --strip-components=1 -C /opt/jetty

# Add a Jetty User
useradd --user-group --shell /bin/false --home-dir /opt/jetty/temp jetty

cd /opt/jetty
# Add running as "jetty"
java -jar /opt/jetty/start.jar --add-to-startd=setuid
cd /

# very important - by renaming the war to root.war, it will run as the root servlet.
mv $WAR /opt/jetty/webapps/root.war

# Make sure "jetty" owns everything.
chown --recursive jetty /opt/jetty

# Configure the default paths for the Jetty service
cp /opt/jetty/bin/jetty.sh /etc/init.d/jetty
echo "JETTY_HOME=/opt/jetty" > /etc/default/jetty
{
echo "JETTY_BASE=/opt/jetty"
echo "TMPDIR=/opt/jetty/temp"
echo "JAVA_OPTIONS=-Djetty.http.port=8080"
echo "JETTY_LOGS=/var/log/jetty"
} >> /etc/default/jetty


# Reload daemon to pick up new service
systemctl daemon-reload

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

service jetty start
service jetty check

echo "Startup Complete"
# [END 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 teardown_sh]
gcloud compute instances delete my-instance

gcloud compute firewall-rules delete allow-http-server-8080
# [END teardown_sh]
17 changes: 17 additions & 0 deletions memorystore/redis/gke_deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 launcher.gcr.io/google/jetty

ADD target/visitcounter-1.0-SNAPSHOT.war $JETTY_BASE/webapps/root.war
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
78 changes: 78 additions & 0 deletions memorystore/redis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!--
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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.redis</groupId>
<artifactId>visitcounter</artifactId>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.8</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<jetty>9.4.4.v20170414</jetty>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<!-- [START dependencies] -->
<dependencies>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<!-- [END dependencies] -->

<build>
<!-- for hot reload of the web application -->
<outputDirectory>servlet/target/visitcounter-1.0-SNAPSHOT/WEB-INF/classes</outputDirectory>
<plugins>
<!--- Only required to test application locally -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty}</version>
</plugin>
<!-- [START appengine_maven_plugin] -->
<!-- Deployment plugin for App Engine Flexible -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
<!-- [END appengine_maven_plugin] -->
</plugins>
</build>
</project>
Loading

0 comments on commit 085792e

Please sign in to comment.