Skip to content

Commit 839a22a

Browse files
committed
Merge pull request #155 from GoogleCloudPlatform/django_gke
Add Django GKE Polls Sample
2 parents 0494d09 + bd00a88 commit 839a22a

23 files changed

+712
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.dockerignore
2+
Dockerfile
3+
db.sqlite3
4+
__pycache__
5+
*.pyc
6+
*.pyo
7+
*.pyd
8+
.Python
9+
env
10+
pip-log.txt
11+
pip-delete-this-directory.txt
12+
.tox
13+
.coverage
14+
.coverage.*
15+
.cache
16+
nosetests.xml
17+
coverage.xml
18+
*,cover
19+
*.log
20+
.git
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2015, 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 docker]
15+
16+
# The Google App Engine python runtime is Debian Jessie with Python installed
17+
# and various os-level packages to allow installation of popular Python
18+
# libraries. The source is on github at:
19+
# https://github.com/GoogleCloudPlatform/python-docker
20+
FROM gcr.io/google_appengine/python
21+
22+
# Create a virtualenv for the application dependencies.
23+
# # If you want to use Python 3, add the -p python3.4 flag.
24+
RUN virtualenv /env
25+
ENV PATH /env/bin:$PATH
26+
27+
ADD requirements.txt /app/requirements.txt
28+
RUN /env/bin/pip install -r /app/requirements.txt
29+
ADD . /app
30+
31+
CMD gunicorn -b :$PORT mysite.wsgi
32+
# [END docker]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 polls \
9+
--scope "https://www.googleapis.com/auth/userinfo.email","cloud-platform"
10+
11+
.PHONY: create-bucket
12+
create-bucket:
13+
gsutil mb gs://$(GCLOUD_PROJECT)
14+
gsutil defacl set public-read gs://$(GCLOUD_PROJECT)
15+
16+
.PHONY: build
17+
build:
18+
docker build -t gcr.io/$(GCLOUD_PROJECT)/polls .
19+
20+
.PHONY: push
21+
push: build
22+
gcloud docker push gcr.io/$(GCLOUD_PROJECT)/polls
23+
24+
.PHONY: template
25+
template:
26+
sed -i ".tmpl" "s/\$$GCLOUD_PROJECT/$(GCLOUD_PROJECT)/g" polls.yaml
27+
28+
.PHONY: deploy
29+
deploy: push template
30+
kubectl create -f polls.yaml
31+
32+
.PHONY: update
33+
update:
34+
kubectl rolling-update polls --image=gcr.io/${GCLOUD_PROJECT}/polls
35+
36+
.PHONY: delete
37+
delete:
38+
kubectl delete rc polls
39+
kubectl delete service polls
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Getting started with Django on Google Container Engine
2+
3+
This repository is an example of how to run a [Django](https://www.djangoproject.com/)
4+
app on Google Container Engine. It uses the [Writing your first Django app](https://docs.djangoproject.com/en/1
5+
.9/intro/tutorial01/) Polls application as the example app to deploy. From here on out, we refer to this app as
6+
the 'polls' application.
7+
8+
## Pre-requisites
9+
10+
1. Create a project in the [Google Cloud Platform Console](https://console.cloud.google.com).
11+
12+
2. [Enable billing](https://console.cloud.google.com/project/_/settings) for your project.
13+
14+
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.
15+
16+
4. Install the [Google Cloud SDK](https://cloud.google.com/sdk)
17+
18+
$ curl https://sdk.cloud.google.com | bash
19+
$ gcloud init
20+
21+
5. Install [Docker](https://www.docker.com/).
22+
23+
## Makefile
24+
25+
Several commands listed below are provided in simpler form via the Makefile. Many of them use the GCLOUD_PROJECT
26+
environment variable, which will be picked up from your gcloud config. Make sure you set this to the correct project,
27+
28+
gcloud config set project <your-project-id>
29+
30+
### Create a cluster
31+
32+
Create a cluster for the bookshelf application:
33+
34+
gcloud container clusters create bookshelf \
35+
--scope "https://www.googleapis.com/auth/userinfo.email","cloud-platform" \
36+
--num-nodes 2
37+
gcloud container clusters get-credentials bookshelf
38+
39+
The scopes specified in the `--scope` argument allows nodes in the cluster to access Google Cloud Platform APIs, such as the Cloud Datastore API.
40+
41+
### Create a Cloud Storage bucket
42+
43+
The bookshelf application uses [Google Cloud Storage](https://cloud.google.com/storage) to store image files. Create a bucket for your project:
44+
45+
gsutil mb gs://<your-project-id>
46+
gsutil defacl set public-read gs://<your-project-id>
47+
48+
49+
## Setup the database
50+
51+
This tutorial assumes you are setting up Django using a SQL database, which is the easiest way to run Django. If you have an existing SQL database running, you can use that, but if not, these are the instructions for creating a managed MySQL instance using CloudSQL.
52+
53+
54+
* Create a [CloudSQL instance](https://console.cloud.google.com/project/_/sql/create)
55+
56+
* In the instances list, click your Cloud SQL instance.
57+
58+
* Click Access Control.
59+
60+
* In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an
61+
IPv4 address. It will take a moment to initialize the new IP address.
62+
63+
* Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button .
64+
65+
* In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses.
66+
67+
* Click Save.
68+
69+
Note: setting allowed networks to 0.0.0.0/0 opens your SQL instance to traffic from any computer. For production databases, it's highly recommended to limit the authorized networks to only IP ranges that need access.
70+
71+
* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw
72+
with a strong, unique password.
73+
74+
`gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw`
75+
76+
* Create a Database And User
77+
78+
* Using the root password created in the last step to create a new database, user, and password using your preferred MySQL client. Alternatively, follow these instructions to create the database and user from the console.
79+
* From the CloudSQL instance in the console, click New user.
80+
* Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly
81+
generated password.
82+
* Click Add.
83+
* Click Databases and then click New database.
84+
* For Name, enter the name of your database (for example, "polls"), and click Add.
85+
86+
Once you have a SQL host, configuring mysite/settings.py to point to your database. Change `your-database-name`,
87+
`your-database-user`, `your-database-host` , and `your-database-password` to match the settings created above. Note the
88+
instance name is not used in this configuration, and the host name is the IP address you created.
89+
90+
91+
## Running locally
92+
93+
First make sure you have Django installed. It's recommended you do so in a
94+
[virtualenv](https://virtualenv.pypa.io/en/latest/). The requirements.txt
95+
contains just the Django dependency.
96+
97+
pip install -r requirements.txt
98+
99+
Once the database is setup, run the migrations.
100+
101+
python manage.py migrate
102+
103+
If you'd like to use the admin console, create a superuser.
104+
105+
python manage.py createsuperuser
106+
107+
The app can be run locally the same way as any other Django app.
108+
109+
python manage.py runserver
110+
111+
Now you can view the admin panel of your local site at
112+
113+
http://localhost:8080/admin
114+
115+
# Deploying To Google Container Engine (Kubernetes)
116+
117+
## Build the polls container
118+
119+
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/).
120+
121+
docker build -t gcr.io/your-project-id/polls .
122+
gcloud docker push gcr.io/your-project-id/polls
123+
124+
Alternatively, this can be done using
125+
126+
make push
127+
128+
129+
## Deploy to the application
130+
131+
### Serve the static content
132+
133+
Collect all the static assets into the static/ folder.
134+
135+
python manage.py collectstatic
136+
137+
When DEBUG is enabled, Django can serve the files directly from that folder. For production purposes, you should
138+
serve static assets from a CDN. Here are instructions for how to do this using Google Cloud Storage.
139+
140+
Upload it to CloudStorage using the `gsutil rsync` command
141+
142+
gsutil rsync -R static/ gs://<your-gcs-bucket>/static
143+
144+
Now your static content can be served from the following URL:
145+
146+
http://storage.googleapis.com/<your-gcs-bucket/static/
147+
148+
Change the `STATIC_URL` in mysite/settings.py to reflect this new URL by uncommenting
149+
the appropriate line and replacing `<your-cloud-bucket>`
150+
151+
### Create the Kubernetes resources
152+
153+
This application is represented in a single Kubernetes config, called `polls`. First, replace the
154+
GCLOUD_PROJECT in `polls.yaml` with your project ID. Alternatively, run `make template` with your
155+
GCLOUD_PROJECT environment variable set.
156+
157+
kubectl create -f polls.yaml
158+
159+
Alternatively this create set can be done using the Makefile
160+
161+
make deploy
162+
163+
Once the resources are created, there should be 3 `polls` pods on the cluster. To see the pods and ensure that
164+
they are running:
165+
166+
kubectl get pods
167+
168+
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:
169+
170+
kubectl logs pod-id
171+
172+
Once the pods are ready, you can get the public IP address of the load balancer:
173+
174+
kubectl get services polls
175+
176+
You can then browse to the public IP address in your browser to see the bookshelf application.
177+
178+
When you are ready to update the replication controller with a new image you built, the following command will do a
179+
rolling update
180+
181+
kubectl rolling-update polls --image=gcr.io/${GCLOUD_PROJECT}/polls
182+
183+
which can also be done with the `make update` command.
184+
185+
186+
## Issues
187+
188+
Please use the Issue Tracker for any issues or questions.
189+
190+
## Contributing changes
191+
192+
* See [CONTRIBUTING.md](CONTRIBUTING.md)
193+
194+
195+
## Licensing
196+
197+
* See [LICENSE](LICENSE)

container_engine/django_tutorial/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# Copyright 2015 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
import sys
18+
19+
if __name__ == "__main__":
20+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
21+
22+
from django.core.management import execute_from_command_line
23+
24+
execute_from_command_line(sys.argv)

container_engine/django_tutorial/mysite/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)