Make a Pod and Service for Postgres#
In this lab you’ll make a pod for the Postgres container so your application can use storage. The completed pod and service from this lab will be a starting point for when you work on the next milestone.
Pod Definition#
Start with a simple pod definition derived from the Kubernetes documentation. Copy and paste this YAML into a file named deployment/postgres-pod.yaml
.
apiVersion: v1
kind: Pod
metadata:
name: postgres
labels:
app: postgres
spec:
containers:
- name: postgres
image: docker.io/postgres:14.1
ports:
- containerPort: 5432
env:
- name: POSTGRES_PASSWORD
value: "django"
- name: POSTGRES_DB
value: "mysite"
Service Definition#
Copy and paste the following into deployment/postgres-service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
type: ClusterIP
selector:
app: postgres
ports:
- protocol: TCP
port: 5432
targetPort: 5432
Deploy the Pod and Service#
Deploy your pod with the command:
$ kubectl apply -f deployment/postgres-pod.yaml
Deploy your service with the command:
$ kubectl apply -f deployment/postgres-service.yaml
Verify the deployment and service:
$ kubectl get all
Access the Service#
Access the service using the port-forward
command:
$ kubectl port-forward service/postgres 5432:5432
On your dev box you should be able to initialize the empty database using Django:
$ export SECRET_KEY=blahblah
$ export DB_NAME=mysite
$ export DB_USER=postgres
$ export DB_PASSWORD=django
$ export DB_HOST=localhost
$ python3 ./manage.py migrate
Access the Pod#
Create a shell in your Postgres pod and look around:
$ kubectl exec -it pod/postgres -- /bin/bash
Inside the container check for the database files.
root@postgres:/app# ls -l /var/lib/postgresql/data
Next week we’ll move this directory into permanent storage.