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:
docker.io/postgres:14.1
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
mysite
Set POSTGRES_PASSWORD to
django
Postrgres stores its data in
/var/lib/postgresql
. Be sure to mount aPersistentVolume
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:
Set
DB_NAME
to the same thing asPOSTGRES_DB
in the Postgres container.Set
DB_USER
topostgres
(the admin user in Postgres)Set
DB_PASSWORD
to the same thing asPOSTGRES_PASSWORD
in the Postgres container.Set
DB_HOST
to the name of the Postgres service in the filedeployment/postgres-service.yaml
Requirements#
A
Pod
definition 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
/polls
URL of your appliction
A
Service
definition for Django with typeLoadBalancer
A
Pod
definition 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
A
Service
definition for Postgres with typeClusterIP
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 ./manage.py migrate
$ python3 ./manage.py createsuperuser
With the DB initialized I will access your application using the IP address assigned to the LoadBalancer
.
Turn In#
Tag this milestone
milestone-7.x
.A URL to your release. It should look like this:
https://github.com/your-name/your-cis-92-repo/releases/tag/milestone-7.1