Deploy Postgres to Kubernetes#

In class you have created the three fundamental components of an application:

  1. A pod definition to define your executable code, CPU and memory requests.

  2. A service definition to make your pod reachable on the network.

  3. A persistent volume claim to hold your precious user data.

To complete this milestone you have to verify that all of the components work together. The Postgres database should have a pod, service and PVC definition alongside of the manifests from the last milestone. The deployment directory should look like this:

$ tree deployment
├── pod.yaml
├── service.yaml
├── postgres-pod.yaml
├── postgres-pvc.yaml
└── postgres-service.yaml

Verify the Postgres Pod#

Here are some tips to save you time getting Postgres running. Verify these settings, they should be consistent with the Use a Persistent Volume Claim and Make a Pod and Service for Postgres labs.

  1. Use the image:

  2. The Postgres container uses the following environment variables. Set them in your pod definition. You can find the full list of Postgres environment variables in the documentation.

    1. Set POSTGRES_DB to mysite

    2. Set POSTGRES_PASSWORD to django

  3. Postrgres stores its data in /var/lib/postgresql. Be sure to mount a PersistentVolume into that location.

Updates for the Django Pod#

In the last milestone you used the stateless container you deployed to CloudRun. In this milestone you should replace that container image with the one from the Production Database Milestone. In deployment/pod.yaml update the pod to set the following environment variables:

  1. Set DB_NAME to the same thing as POSTGRES_DB in the Postgres container.

  2. Set DB_USER to postgres (the admin user in Postgres)

  3. Set DB_PASSWORD to the same thing as POSTGRES_PASSWORD in the Postgres container.

  4. Set DB_HOST to the name of the Postgres service in the file deployment/postgres-service.yaml


  1. A Pod definition for Django that includes:

    1. Reasonable resource requests:

      1. At least 250m but not more than 1000m CPUs

      2. At least 128Mi of memory but not more than 1Gi of memory

    2. A liveness probe that checks that Django is serving requests

      1. The probe should check the /polls URL of your appliction

  2. A Service definition for Django with type LoadBalancer

  3. A Pod definition for Postgres that includes:

    1. Reasonable resource requests:

      1. At least 500m but not more than 1000m CPUs

      2. At least 256Mi of memory but not more than 1Gi of memory

    2. A liveness probe is not required but would be a nice touch

  4. A Service definition for Postgres with type ClusterIP

  5. A PersistentVolume for Postgres mounted on /var/lib/postgresql

Test Procedure#

When you submit the milestone I will check out your code then run the equivalent of the following commands in your repo:

$ kubectl apply -f deployment/postgres-pvc.yaml
$ kubectl apply -f deployment/postgres-pod.yaml
$ kubectl apply -f deployment/postgres-service.yaml
$ kubectl apply -f deployment/pod.yaml
$ kubectl apply -f deployment/service.yaml

Since my database will be blank I will initialize it from your codebase:

$ kubectl port-forward service/postgres 5432:5432 &
$ export SECRET_KEY=blahblah
$ export DB_NAME=mysite
$ export DB_USER=postgres
$ export DB_PASSWORD=django
$ export DB_HOST=localhost
$ python3 ./ migrate 
$ python3 ./ createsuperuser 

With the DB initialized I will access your application using the IP address assigned to the LoadBalancer.

Turn In#

  1. Tag this milestone milestone-7.x.

  2. A URL to your release. It should look like this: