Skip to content

Instantly share code, notes, and snippets.

@tyrell
Last active April 3, 2017 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrell/8552a8761d98a67f9dbb1d808df35734 to your computer and use it in GitHub Desktop.
Save tyrell/8552a8761d98a67f9dbb1d808df35734 to your computer and use it in GitHub Desktop.
Django + PostgreSQL (Persisted) on Openshift

Introduction

The steps below assume that you are using minishift (https://github.com/minishift/minishift) to run an Openshift cluster locally.

Prefereably, these steps need to be carried out before initiating an application with the template above. If not, the PostgreSQL database pod will not start up and you will have to re-deploy the PostgreSQL pod after carrying out the steps below. It's not a major problem. But why panick unnecessarily? :)

Step 1 : Add Django with PostgreSQL 'persistent' template to Openshift

The Django persistent template can be found at https://github.com/openshift/django-ex

`wget https://raw.githubusercontent.com/openshift/django-ex/master/openshift/templates/django-postgresql-persistent.json`

`oc create -f django-postgresql-persistent.json`

Step 2: Mount persistent volume to openshift to be used by PostgreSQL

1. Log in as administrator

`oc login -u system:admin`

2. Create a persistent volume

The example configuration file below creates a persisted volume named 'pv-postgres'.

The PostgreSQL instance from the template will create a storage claim named 'postgresql' in my openshift environment.

When the application is instantiated, I am planning to nominate 1GB of storage for PostgreSQL. Since I have given the same capacity of 1GB to my persistent volume, openshift will automatically bind the 'postgresql' claim to my 'pv-postgres' volume.

Save the below persistent volume configuration to a file named persistent-config.json.

This configuration creates a persistent volume using the host path /var/lib/minishift/pv-postgres. In a production like environment, this will be an NFS mount.

NOTE: Indentation is important when you save the content below into your file.

apiVersion: "v1"
kind: "PersistentVolume"
metadata: 
    name: "pv-postgres"
spec: 
    accessModes: 
        - "ReadWriteOnce"
    capacity: 
        storage: "1Gi"
hostPath: 
    path: "/var/lib/minishift/pv-postgres"

Now, from the same directory as the file above, run the following oc command.

'oc create -f persistent-config.json` 

This creates the directory /var/lib/minishift/pv-postgres inside the minishift Docker host.

3. Confirm that it’s bound to the persistent volume claim

`oc get pv` (Lists persistent volumes)

`oc get pvc` (Lists persistent volume claims)

4. Confirm that the host directory is writeable (for some reason it was owned by root in my MAC)

`minishift ssh`

`cd /var/lib/minishift/`

`sudo chmod 777 pv-postgres` (Caution: Do 777 only in your local setup. This makes the directory world writeable)

Step 3

Finally - Create your application using the django postgresql template via “Add to Project” wizard.

The screenshot below (openshift-persistent-volume.png) shows how your openshift console's "Storage" view will look like, if all went well.

License

Copyright (c) 2017 Tyrell Perera tyrell.perera@gmail.com Licensed under the MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment