Make a Pod and Service for Django#
Note
Complete the Ready to Deploy milestone before starting this lab.
In this lab you’ll make a pod for your Django container. For now the pod won’t use an external database, instead selecting the SQLite database for testing purposes. The completed pod from this lab will be a starting point for when you work on the next milestone, Deploy to Kubernetes. You can deploy this pod to Minikube in your dev environment or into the GKE Autopilot cluster you created last week. It’s up to you.
Pod Definition#
Start with a simple pod definition derived from the Kubernetes documentation. Copy and paste this YAML into a file named deployment/mysite-pod.yaml
.
apiVersion: v1
kind: Pod
metadata:
name: mysite
labels:
app: mysite
spec:
containers:
- name: mysite
image: ghcr.io/mike-matera/cis-92:main
imagePullPolicy: Always
ports:
- containerPort: 8000
env:
- name: DB_ENGINE
value: "sqlite"
- name: DJANGO_ADMIN_EMAIL
value: "test@test.test"
- name: DJANGO_ADMIN_USERNAME
value: "test"
- name: DJANGO_SUPERUSER_PASSWORD
value: "test"
Warning
Change ghcr.io/your-name/your-repo:main
to the Docker URL of your GitHub release.
Service Definition#
Copy and paste the following into deployment/mysite-service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: mysite
spec:
type: LoadBalancer
selector:
app: mysite
ports:
- protocol: TCP
port: 80
targetPort: 8000
Deploy the Pod and Service#
Deploy your pod with the command:
$ kubectl apply -f deployment/mysite-pod.yaml
Deploy your service with the command:
$ kubectl apply -f deployment/mysite-service.yaml
Verify the deployment and service:
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mysite 1/1 Running 0 3m23s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7h46m
service/mysite LoadBalancer 10.105.26.245 <pending> 80:30935/TCP 3m16s
Access the Service#
Access the service using the port-forward
command:
$ kubectl port-forward service/mysite 8080:80
Preview the application on localhost:8080
Access the Pod#
Create a shell in your Django pod and verify that the database is in the /db
directory:
$ kubectl exec -it pod/mysite -- /bin/bash
Inside the container check for the database file:
root@mysite:/app# ls -l /db
total 140
-rw-r--r-- 1 root root 143360 Mar 24 18:30 db.sqlite3