Deploy Postgres to Kubernetes#
In class you have created the three fundamental components of an application:
A pod definition to define your executable code, CPU and memory requests.
A service definition to make your pod reachable on the network.
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 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.
Use the image:
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.
Set POSTGRES_DB to
Set POSTGRES_PASSWORD to
Postrgres stores its data in
/var/lib/postgresql. Be sure to mount a
PersistentVolumeinto 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:
DB_NAMEto the same thing as
POSTGRES_DBin the Postgres container.
postgres(the admin user in Postgres)
DB_PASSWORDto the same thing as
POSTGRES_PASSWORDin the Postgres container.
DB_HOSTto the name of the Postgres service in the file
Poddefinition for Django that includes:
Reasonable resource requests:
At least 250m but not more than 1000m CPUs
At least 128Mi of memory but not more than 1Gi of memory
A liveness probe that checks that Django is serving requests
The probe should check the
/pollsURL of your appliction
Servicedefinition for Django with type
Poddefinition for Postgres that includes:
Reasonable resource requests:
At least 500m but not more than 1000m CPUs
At least 256Mi of memory but not more than 1Gi of memory
A liveness probe is not required but would be a nice touch
Servicedefinition for Postgres with type
PersistentVolumefor Postgres mounted on
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 ./manage.py migrate $ python3 ./manage.py createsuperuser
With the DB initialized I will access your application using the IP address assigned to the
Tag this milestone
A URL to your release. It should look like this: