Skip to content

Commit 77ad58f

Browse files
committed
adds back optional-kubernetes-engine for the sake of a quicklab
1 parent 8d1ba8d commit 77ad58f

29 files changed

+1987
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo
4+
*.pyd
5+
.Python
6+
env
7+
pip-log.txt
8+
pip-delete-this-directory.txt
9+
.tox
10+
.coverage
11+
.coverage.*
12+
.cache
13+
nosetests.xml
14+
coverage.xml
15+
*,cover
16+
*.log
17+
.git

optional-kubernetes-engine/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
# Install dependencies.
18+
ADD requirements.txt /app/requirements.txt
19+
RUN pip install -r /app/requirements.txt
20+
21+
# Add application code.
22+
ADD . /app
23+
24+
# Instead of using gunicorn directly, we'll use Honcho. Honcho is a python port
25+
# of the Foreman process manager. $PROCESSES is set in the pod manifest
26+
# to control which processes Honcho will start.
27+
CMD honcho start -f /app/procfile $PROCESSES

optional-kubernetes-engine/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
GCLOUD_PROJECT:=$(shell gcloud config list project --format="value(core.project)")
2+
3+
.PHONY: all
4+
all: deploy
5+
6+
.PHONY: create-cluster
7+
create-cluster:
8+
gcloud container clusters create bookshelf \
9+
--scopes "cloud-platform" \
10+
--num-nodes 2
11+
gcloud container clusters get-credentials bookshelf
12+
13+
.PHONY: create-bucket
14+
create-bucket:
15+
gsutil mb gs://$(GCLOUD_PROJECT)
16+
gsutil defacl set public-read gs://$(GCLOUD_PROJECT)
17+
18+
.PHONY: build
19+
build:
20+
docker build -t gcr.io/$(GCLOUD_PROJECT)/bookshelf .
21+
22+
.PHONY: push
23+
push: build
24+
gcloud docker -- push gcr.io/$(GCLOUD_PROJECT)/bookshelf
25+
26+
.PHONY: template
27+
template:
28+
sed -i ".tmpl" "s/\[GCLOUD_PROJECT\]/$(GCLOUD_PROJECT)/g" bookshelf-frontend.yaml
29+
sed -i ".tmpl" "s/\[GCLOUD_PROJECT\]/$(GCLOUD_PROJECT)/g" bookshelf-worker.yaml
30+
31+
.PHONY: create-service
32+
create-service:
33+
kubectl create -f bookshelf-service.yaml
34+
35+
.PHONY: deploy-frontend
36+
deploy-frontend: push template
37+
kubectl create -f bookshelf-frontend.yaml
38+
39+
.PHONY: deploy-worker
40+
deploy-worker: push template
41+
kubectl create -f bookshelf-worker.yaml
42+
43+
.PHONY: deploy
44+
deploy: deploy-frontend deploy-worker create-service
45+
46+
.PHONY: delete
47+
delete:
48+
-kubectl delete -f bookshelf-service.yaml
49+
-kubectl delete -f bookshelf-worker.yaml
50+
-kubectl delete -f bookshelf-frontend.yaml

optional-kubernetes-engine/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Deploy Bookshelf to Google Kubernetes Engine
2+
3+
This optional tutorial will walk you through how to deploy the Bookshelf sample application to [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/). This tutorial is also applicable to [Kubernetes](http://kubernetes.io/) outside of Google Kubernetes Engine, but may require additional steps for external load balancing.
4+
5+
## Pre-requisites
6+
7+
1. Create a project in the [Google Cloud Platform Console](https://console.cloud.google.com).
8+
9+
2. [Enable billing](https://console.cloud.google.com/project/_/settings) for your project.
10+
11+
3. [Enable APIs](https://console.cloud.google.com/flows/enableapi?apiid=datastore,pubsub,storage_api,logging,plus) for your project. The provided link will enable all necessary APIs, but if you wish to do so manually you will need Datastore, Pub/Sub, Storage, and Logging.
12+
13+
4. Install the [Google Cloud SDK](https://cloud.google.com/sdk)
14+
15+
$ curl https://sdk.cloud.google.com | bash
16+
$ gcloud init
17+
18+
5. Install [Docker](https://www.docker.com/).
19+
20+
## Create a cluster
21+
22+
Create a cluster for the bookshelf application:
23+
24+
gcloud container clusters create bookshelf \
25+
--scopes "cloud-platform" \
26+
--num-nodes 2
27+
gcloud container clusters get-credentials bookshelf
28+
29+
The scopes specified in the `--scopes` argument allows nodes in the cluster to access Google Cloud Platform APIs, such as the Cloud Datastore API.
30+
31+
Alternatively, you can use make:
32+
33+
make create-cluster
34+
35+
## Create a Cloud Storage bucket
36+
37+
The bookshelf application uses [Google Cloud Storage](https://cloud.google.com/storage) to store image files. Create a bucket for your project:
38+
39+
gsutil mb gs://<your-project-id>
40+
gsutil defacl set public-read gs://<your-project-id>
41+
42+
Alternatively, you can use make:
43+
44+
make create-bucket
45+
46+
## Update config.py
47+
48+
Modify config.py and enter your Cloud Project ID into the `PROJECT_ID` and `CLOUD_STORAGE_BUCKET` field. The remaining configuration values are only needed if you wish to use a different database or if you wish to enable log-in via oauth2, which requires a domain name.
49+
50+
## Build the bookshelf container
51+
52+
Before the application can be deployed to Kubernetes Engine, you will need build and push the image to [Google Container Registry](https://cloud.google.com/container-registry/).
53+
54+
docker build -t gcr.io/<your-project-id>/bookshelf .
55+
gcloud docker push gcr.io/<your-project-id>/bookshelf
56+
57+
Alternatively, you can use make:
58+
59+
make push
60+
61+
## Deploy the bookshelf frontend
62+
63+
The bookshelf app has two distinct "tiers". The frontend serves a web interface to create and manage books, while the worker handles fetching book information from the Google Books API.
64+
65+
Update `bookshelf-frontend.yaml` with your Project ID or use `make template`. This file contains the Kubernetes resource definitions to deploy the frontend. You can use `kubectl` to create these resources on the cluster:
66+
67+
kubectl create -f bookshelf-frontend.yaml
68+
69+
Alternatively, you can use make:
70+
71+
make deploy-frontend
72+
73+
Once the resources are created, there should be 3 `bookshelf-frontend` pods on the cluster. To see the pods and ensure that they are running:
74+
75+
kubectl get pods
76+
77+
If the pods are not ready or if you see restarts, you can get the logs for a particular pod to figure out the issue:
78+
79+
kubectl logs pod-id
80+
81+
Once the pods are ready, you can get the public IP address of the load balancer:
82+
83+
kubectl get services bookshelf-frontend
84+
85+
You can then browse to the public IP address in your browser to see the bookshelf application.
86+
87+
## Deploy worker
88+
89+
Update `bookshelf-worker.yaml` with your Project ID or use `make template`. This file contains the Kubernetes resource definitions to deploy the worker. The worker doesn't need to serve web traffic or expose any ports, so it has significantly less configuration than the frontend. You can use `kubectl` to create these resources on the cluster:
90+
91+
kubectl create -f bookshelf-worker.yaml
92+
93+
Alternatively, you can use make:
94+
95+
make deploy-worker
96+
97+
Once again, use `kubectl get pods` to check the status of the worker pods. Once the worker pods are up and running, you should be able to create books on the frontend and the workers will handle updating book information in the background.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2015 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License
14+
15+
# This file configures the bookshelf application frontend. The frontend serves
16+
# public web traffic.
17+
18+
apiVersion: extensions/v1beta1
19+
kind: Deployment
20+
metadata:
21+
name: bookshelf-frontend
22+
labels:
23+
app: bookshelf
24+
# The bookshelf frontend replica set ensures that at least 3
25+
# instances of the bookshelf app are running on the cluster.
26+
# For more info about Pods see:
27+
# https://cloud.google.com/kubernetes-engine/docs/pods/
28+
spec:
29+
replicas: 3
30+
template:
31+
metadata:
32+
labels:
33+
app: bookshelf
34+
tier: frontend
35+
spec:
36+
containers:
37+
- name: bookshelf-app
38+
# Replace [GCLOUD_PROJECT] with your project ID or use `make template`.
39+
image: gcr.io/[GCLOUD_PROJECT]/bookshelf
40+
# This setting makes nodes pull the docker image every time before
41+
# starting the pod. This is useful when debugging, but should be turned
42+
# off in production.
43+
imagePullPolicy: Always
44+
# The PROCESSES environment variable is used by Honcho in the
45+
# Dockerfile's CMD to control which processes are started. In this
46+
# case, only the bookshelf process is needed.
47+
env:
48+
- name: PROCESSES
49+
value: bookshelf
50+
# The bookshelf process listens on port 8080 for web traffic by default.
51+
ports:
52+
- name: http-server
53+
containerPort: 8080
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License
14+
15+
# The bookshelf service provides a load-balancing proxy over the bookshelf
16+
# frontend pods. By specifying the type as a 'LoadBalancer', Kubernetes Engine
17+
# will create an external HTTP load balancer.
18+
# For more information about Services see:
19+
# https://cloud.google.com/kubernetes-engine/docs/services/
20+
# For more information about external HTTP load balancing see:
21+
# https://cloud.google.com/kubernetes-engine/docs/load-balancer
22+
apiVersion: v1
23+
kind: Service
24+
metadata:
25+
name: bookshelf-frontend
26+
labels:
27+
app: bookshelf
28+
tier: frontend
29+
spec:
30+
type: LoadBalancer
31+
ports:
32+
- port: 80
33+
targetPort: http-server
34+
selector:
35+
app: bookshelf
36+
tier: frontend
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2015 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License
14+
15+
# This file configures the bookshelf task worker. The worker is responsible
16+
# for processing book requests and updating book information.
17+
18+
apiVersion: extensions/v1beta1
19+
kind: Deployment
20+
metadata:
21+
name: bookshelf-worker
22+
labels:
23+
app: bookshelf
24+
# The bookshelf worker replica set ensures that at least 2 instances of the
25+
# bookshelf worker pod are running on the cluster.
26+
# For more info about Pods see:
27+
# https://cloud.google.com/kubernetes-engine/docs/pods/
28+
spec:
29+
replicas: 2
30+
template:
31+
metadata:
32+
labels:
33+
app: bookshelf
34+
tier: worker
35+
spec:
36+
containers:
37+
- name: bookshelf-app
38+
# Replace [GCLOUD_PROJECT] with your project ID or use `make template`.
39+
image: gcr.io/[GCLOUD_PROJECT]/bookshelf
40+
# This setting makes nodes pull the docker image every time before
41+
# starting the pod. This is useful when debugging, but should be turned
42+
# off in production.
43+
imagePullPolicy: Always
44+
# The PROCESSES environment variable is used by Honcho in the
45+
# Dockerfile's CMD to control which processes are started. In this
46+
# case, only the worker process is needed.
47+
env:
48+
- name: PROCESSES
49+
value: worker

0 commit comments

Comments
 (0)