Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 2 additions & 179 deletions container_engine/django_tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,187 +5,10 @@ app on Google Container Engine. It uses the [Writing your first Django app](http
.9/intro/tutorial01/) Polls application as the example app to deploy. From here on out, we refer to this app as
the 'polls' application.

## Pre-requisites

1. Create a project in the [Google Cloud Platform Console](https://console.cloud.google.com).
# Tutorial
See our [Django on Container Engine](https://cloud.google.com/python/django/container-engine) tutorial for instructions for setting up and deploying this sample application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's secrets in here but the tutorial I see now doesn't have secret anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bill and i just discussed this, will update on Tuesday


2. [Enable billing](https://console.cloud.google.com/project/_/settings) for your project.

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.

4. Install the [Google Cloud SDK](https://cloud.google.com/sdk)

$ curl https://sdk.cloud.google.com | bash
$ gcloud init

5. Install [Docker](https://www.docker.com/).

## Makefile

Several commands listed below are provided in simpler form via the Makefile. Many of them use the GCLOUD_PROJECT
environment variable, which will be picked up from your gcloud config. Make sure you set this to the correct project,

gcloud config set project <your-project-id>

### Create a cluster

Create a cluster for the bookshelf application:

gcloud container clusters create bookshelf \
--scope "https://www.googleapis.com/auth/userinfo.email","cloud-platform" \
--num-nodes 2
gcloud container clusters get-credentials bookshelf

The scopes specified in the `--scope` argument allows nodes in the cluster to access Google Cloud Platform APIs, such as the Cloud Datastore API.

### Create a Cloud Storage bucket

The bookshelf application uses [Google Cloud Storage](https://cloud.google.com/storage) to store image files. Create a bucket for your project:

gsutil mb gs://<your-project-id>
gsutil defacl set public-read gs://<your-project-id>


## Setup the database

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.


* Create a [CloudSQL instance](https://console.cloud.google.com/project/_/sql/create)

* In the instances list, click your Cloud SQL instance.

* Click Access Control.

* In the IP address subsection, click Request an IPv4 address to enable access to the Cloud SQL instance through an
IPv4 address. It will take a moment to initialize the new IP address.

* Also under Access Control, in the Authorization subsection, under Allowed Networks, click the add (+) button .

* In the Networks field, enter 0.0.0.0/0. This value allows access by all IP addresses.

* Click Save.

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.

* Alternatively, the instance can be created with the gcloud command line tool as follows, substituting `your-root-pw
with a strong, unique password.

`gcloud sql instances create <instance_name> --assign-ip --authorized-networks=0.0.0.0/0 set-root-password=your-root-pw`

* Create a Database And User

* 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.
* From the CloudSQL instance in the console, click New user.
* Enter a username and password for the application. For example, name the user "pythonapp" and give it a randomly
generated password.
* Click Add.
* Click Databases and then click New database.
* For Name, enter the name of your database (for example, "polls"), and click Add.

Once you have a SQL host, configuring mysite/settings.py to point to your database. Change `your-database-name`,
`your-database-user`, `your-database-host` , and `your-database-password` to match the settings created above. Note the
instance name is not used in this configuration, and the host name is the IP address you created.


## Running locally

First make sure you have Django installed. It's recommended you do so in a
[virtualenv](https://virtualenv.pypa.io/en/latest/). The requirements.txt
contains just the Django dependency.

pip install -r requirements.txt

Once the database is setup, run the migrations.

python manage.py migrate

If you'd like to use the admin console, create a superuser.

python manage.py createsuperuser

The app can be run locally the same way as any other Django app.

python manage.py runserver

Now you can view the admin panel of your local site at

http://localhost:8080/admin

# Deploying To Google Container Engine (Kubernetes)

## Build the polls container

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/).

docker build -t gcr.io/your-project-id/polls .
gcloud docker push gcr.io/your-project-id/polls

Alternatively, this can be done using

make push


## Deploy to the application

### Serve the static content

Collect all the static assets into the static/ folder.

python manage.py collectstatic

When DEBUG is enabled, Django can serve the files directly from that folder. For production purposes, you should
serve static assets from a CDN. Here are instructions for how to do this using Google Cloud Storage.

Upload it to CloudStorage using the `gsutil rsync` command

gsutil rsync -R static/ gs://<your-gcs-bucket>/static

Now your static content can be served from the following URL:

http://storage.googleapis.com/<your-gcs-bucket/static/

Change the `STATIC_URL` in mysite/settings.py to reflect this new URL by uncommenting
the appropriate line and replacing `<your-cloud-bucket>`

### Create the Kubernetes resources

This application is represented in a single Kubernetes config, called `polls`. First, replace the
GCLOUD_PROJECT in `polls.yaml` with your project ID. Alternatively, run `make template` with your
GCLOUD_PROJECT environment variable set.

kubectl create -f polls.yaml

Alternatively this create set can be done using the Makefile

make deploy

Once the resources are created, there should be 3 `polls` pods on the cluster. To see the pods and ensure that
they are running:

kubectl get pods

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:

kubectl logs pod-id

Once the pods are ready, you can get the public IP address of the load balancer:

kubectl get services polls

You can then browse to the public IP address in your browser to see the bookshelf application.

When you are ready to update the replication controller with a new image you built, the following command will do a
rolling update

kubectl rolling-update polls --image=gcr.io/${GCLOUD_PROJECT}/polls

which can also be done with the `make update` command.


## Issues

Please use the Issue Tracker for any issues or questions.

## Contributing changes

Expand Down
15 changes: 9 additions & 6 deletions container_engine/django_tutorial/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
# SECURITY WARNING: If you deploy a Django app to production, make sure to set
# an appropriate host here.
# See https://docs.djangoproject.com/en/1.10/ref/settings/
ALLOWED_HOSTS = ['*']

# Application definition

Expand Down Expand Up @@ -78,10 +81,10 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your-database-name>',
'USER': '<your-database-user>',
'PASSWORD': '<your-database-password>',
'HOST': '<your-cloudsql-host>',
'NAME': 'polls',
'USER': os.getenv('DATABASE_USER'),
'PASSWORD': os.getenv('DATABASE_PASSWORD'),
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Expand All @@ -105,7 +108,7 @@

# [START staticurl]
STATIC_URL = '/static/'
# STATIC_URL = 'https://storage.googleapis.com/<your-bucket-id>/static/'
# STATIC_URL = 'https://storage.googleapis.com/<your-gcs-bucket>/static/'
# [END staticurl]

STATIC_ROOT = 'static/'
54 changes: 47 additions & 7 deletions container_engine/django_tutorial/polls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
# instances of the bookshelf app are running on the cluster.
# For more info about Pods see:
# https://cloud.google.com/container-engine/docs/pods/
# For more info about Replication Controllers:
# https://cloud.google.com/container-engine/docs/replicationcontrollers/
# For more info about Deployments:
# https://kubernetes.io/docs/user-guide/deployments/

# [START replication_controller]
apiVersion: v1
kind: ReplicationController
# [START kubernetes_deployment]
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: polls
labels:
Expand All @@ -39,14 +39,54 @@ spec:
containers:
- name: polls-app
# Replace with your project ID or use `make template`
image: gcr.io/$GCLOUD_PROJECT/polls
image: gcr.io/<your-project-id>/polls
# This setting makes nodes pull the docker image every time before
# starting the pod. This is useful when debugging, but should be turned
# off in production.
imagePullPolicy: Always
env:
# [START cloudsql_secrets]
- name: DATABASE_USER
valueFrom:
secretKeyRef:
name: cloudsql
key: username
- name: DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: cloudsql
key: password
# [END cloudsql_secrets]
ports:
- containerPort: 8080
# [END replication_controller]

# [START proxy_container]
- image: b.gcr.io/cloudsql-docker/gce-proxy:1.05
name: cloudsql-proxy
command: ["/cloud_sql_proxy", "--dir=/cloudsql",
"-instances=<your-cloudsql-connection-string>=tcp:3306",
"-credential_file=/secrets/cloudsql/credentials.json"]
volumeMounts:
- name: cloudsql-oauth-credentials
mountPath: /secrets/cloudsql
readOnly: true
- name: ssl-certs
mountPath: /etc/ssl/certs
- name: cloudsql
mountPath: /cloudsql
# [END proxy_container]
# [START volumes]
volumes:
- name: cloudsql-oauth-credentials
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are these secrets created?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bill and i just discussed this, will update on Tuesday

secret:
secretName: cloudsql-oauth-credentials
- name: ssl-certs
hostPath:
path: /etc/ssl/certs
- name: cloudsql
emptyDir:
# [END volumes]
# [END kubernetes_deployment]

---

Expand Down