Skip to content

Commit fddc595

Browse files
author
Jon Wayne Parrott
committed
Adding kubernetes/container engine deployment.
1 parent 5346831 commit fddc595

26 files changed

+1773
-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
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
# The Google App Engine python runtime is Debian Jessie with Python installed
16+
# and various os-level packages to allow installation of popular Python
17+
# libraries. The source is on github at:
18+
# https://github.com/GoogleCloudPlatform/python-docker
19+
FROM gcr.io/google_appengine/python
20+
21+
# Create a virtualenv for the application dependencies.
22+
# If you want to use Python 3, add the -p python3.4 flag.
23+
RUN virtualenv /env
24+
25+
# Set virtualenv environment variables. This is equivalent to running
26+
# source /env/bin/activate. This ensures the application is executed within
27+
# the context of the virtualenv and will have access to its dependencies.
28+
ENV VIRTUAL_ENV /env
29+
ENV PATH /env/bin:$PATH
30+
31+
# Install dependencies.
32+
ADD requirements.txt /app/requirements.txt
33+
RUN pip install -r /app/requirements.txt
34+
35+
# Add application code.
36+
ADD . /app
37+
38+
# Instead of using gunicorn directly, we'll use Honcho. Honcho is a python port
39+
# of the Foreman process manager. $PROCESSES is set by app.yaml / worker.yaml
40+
# to control which processes Honcho will start.
41+
CMD honcho start -f /app/procfile $PROCESSES

optional-container-engine/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.PHONY: all
2+
all: deploy
3+
4+
.PHONY: create-cluster
5+
create-cluster:
6+
gcloud container clusters create bookshelf \
7+
--scope "https://www.googleapis.com/auth/userinfo.email","cloud-platform"
8+
9+
.PHONY: create-bucket
10+
create-bucket:
11+
gsutil mb gs://$(GCLOUD_PROJECT)
12+
gsutil defacl set public-read gs://$(GCLOUD_PROJECT)
13+
14+
.PHONY: build
15+
build:
16+
docker build -t gcr.io/$(GCLOUD_PROJECT)/bookshelf .
17+
18+
.PHONY: push
19+
push: build
20+
gcloud docker push gcr.io/$(GCLOUD_PROJECT)/bookshelf
21+
22+
.PHONY: template
23+
template:
24+
sed -i ".tmpl" "s/\$$GCLOUD_PROJECT/$(GCLOUD_PROJECT)/g" bookshelf-frontend.yaml
25+
sed -i ".tmpl" "s/\$$GCLOUD_PROJECT/$(GCLOUD_PROJECT)/g" bookshelf-worker.yaml
26+
27+
.PHONY: deploy-frontend
28+
deploy-frontend: push template
29+
kubectl create -f bookshelf-frontend.yaml
30+
31+
.PHONY: deploy-worker
32+
deploy-worker: push template
33+
kubectl create -f bookshelf-worker.yaml
34+
35+
.PHONY: deploy
36+
deploy: deploy-frontend deploy-worker
37+
38+
.PHONY: delete
39+
delete:
40+
kubectl delete rc bookshelf-frontend
41+
kubectl delete rc bookshelf-worker
42+
kubectl delete service bookshelf-frontend
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Deploy Bookshelf to Container Engine/Kubernetes
2+
3+
This optional tutorial will walk you through how to deploy the Bookshelf sample application to [Google Container Engine](https://cloud.google.com/container-engine/). This tutorial is also applicable to [Kubernetes](http://kubernetes.io/) outside of Container 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+
--scope "https://www.googleapis.com/auth/userinfo.email","cloud-platform" \
26+
--num-nodes 2
27+
gcloud container clusters get-credentials bookshelf
28+
29+
The scopes specified in the `--scope` argument allows nodes in the cluster to access Google Cloud Platform APIs, such as the Cloud Datastore API.
30+
31+
## Create a Cloud Storage bucket
32+
33+
The bookshelf application uses [Google Cloud Storage](https://cloud.google.com/storage) to store image files. Create a bucket for your project:
34+
35+
gsutil mb gs://<your-project-id>
36+
gsutil defacl set public-read gs://<your-project-id>
37+
38+
## Update config.py
39+
40+
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.
41+
42+
## Build the bookshelf container
43+
44+
Before the application can be deployed to Container Engine, you will need build and push the image to [Google Container Registry](https://cloud.google.com/container-registry/).
45+
46+
docker build -t gcr.io/your-project-id/bookshelf .
47+
gcloud docker push gcr.io/your-project-id/bookshelf
48+
49+
## Deploy to Bookshelf frontend
50+
51+
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.
52+
53+
`bookshelf-frontend.yaml` contains the Kubernetes resource definitions to deploy the frontend. **Update this file with your Project ID**, then use `kubectl` to create these resources on the cluster:
54+
55+
kubectl create -f bookshelf-frontend.yaml
56+
57+
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:
58+
59+
kubectl get pods
60+
61+
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:
62+
63+
kubectl logs pod-id
64+
65+
Once the pods are ready, you can get the public IP address of the load balancer:
66+
67+
kubectl get services bookshelf-frontend
68+
69+
You can then browse to the public IP address in your browser to see the bookshelf application.
70+
71+
# Deploy worker
72+
73+
`bookshelf-worker.yaml` 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. **Update `bookshelf-worker.yaml` file with your Project ID**, then use `kubectl` to create these resources on the cluster:
74+
75+
kubectl create -f bookshelf-worker.yaml
76+
77+
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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# The bookshelf frontend replication controller ensures that at least 3
19+
# instances of the bookshelf app are running on the cluster.
20+
# For more info about Pods see:
21+
# https://cloud.google.com/container-engine/docs/pods/
22+
# For more info about Replication Controllers:
23+
# https://cloud.google.com/container-engine/docs/replicationcontrollers/
24+
apiVersion: v1
25+
kind: ReplicationController
26+
metadata:
27+
name: bookshelf-frontend
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+
- containerPort: 8080
53+
54+
---
55+
56+
# The bookshelf service provides a load-balancing proxy over the bookshelf app
57+
# pods. By specifying the type as a 'LoadBalancer', Container Engine will
58+
# create an external HTTP load balancer.
59+
# For more information about Services see:
60+
# https://cloud.google.com/container-engine/docs/services/
61+
# For more information about external HTTP load balancing see:
62+
# https://cloud.google.com/container-engine/docs/load-balancer
63+
apiVersion: v1
64+
kind: Service
65+
metadata:
66+
name: bookshelf-frontend
67+
labels:
68+
app: bookshelf
69+
tier: frontend
70+
spec:
71+
type: LoadBalancer
72+
ports:
73+
- port: 80
74+
targetPort: 8080
75+
selector:
76+
app: bookshelf
77+
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+
# The bookshelf worker controller ensures that at least 3 instances of the
19+
# bookshelf worker pod are running on the cluster.
20+
# For more info about Pods see:
21+
# https://cloud.google.com/container-engine/docs/pods/
22+
# For more info about Replication Controllers:
23+
# https://cloud.google.com/container-engine/docs/replicationcontrollers/
24+
apiVersion: v1
25+
kind: ReplicationController
26+
metadata:
27+
name: bookshelf-worker
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)