Skip to content

Commit 7d5a2c0

Browse files
author
Josh Reynolds
committed
Adding in api infrastructure using kubernetes deployments
1 parent 9799fa8 commit 7d5a2c0

File tree

5 files changed

+115
-12
lines changed

5 files changed

+115
-12
lines changed

README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
# Welcome to the Class
2-
3-
## Prerequites to run this course (and commands to test for them and installation instructions)
4-
5-
NOTE: Most applications can be installed using [homebrew](https://brew.sh/)
1+
# Prequisites
62

3+
NOTE: All applications can also be installed using [homebrew](https://brew.sh/)
74

85
### Required
9-
* Python 3.8+ -- `python3 --version` -- https://docs.python-guide.org/starting/install3/osx/
10-
* Docker Desktop -- `docker --version` -- https://docs.docker.com/docker-for-mac/install/
11-
6+
* Python 3.8+ -- `python3 --version` -- https://docs.python-guide.org/starting/install3/osx/ -- `brew install python3`
7+
* node.js v12+ -- `node --version` -- https://nodejs.org/en/download/package-manager/#macos -- `brew install node`
8+
* Docker Desktop -- `docker --version` -- https://docs.docker.com/docker-for-mac/install/ -- `brew install --cask docker`
129

1310
### Recommended
14-
* VSCode -- `code --version` -- https://code.visualstudio.com/docs/setup/mac
11+
* VSCode -- `code --version` -- https://code.visualstudio.com/docs/setup/mac -- `brew install --cask visual-studio-code`
12+
13+
Note: Any code editor will do. Support for python and javascript is ideal.
14+
15+
### Configuration
1516

17+
Please enable a kubernetes cluster on your local machine using the docker desktop dashboard.
1618

19+
1. From the docker desktop tray select `Dashboard`
20+
2. Select the gear icon in the upper right
21+
3. Select `Kubernetes` from the list on the left
22+
4. Check the `Enable Kubernetes` checkbox -- this will take a minute or two to determine if it is running.
23+
5. Verify installation by runing `kubectl get pods --all-namespaces` and seeing output of running system containers.
1724

25+
If you run in to trouble no sweat. We will have time to cover this.

api/Makefile

+22-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ test: venv
1111
. $(PYBIN)/activate; python -m unittest discover todo/unittests
1212

1313
integration-test: venv
14+
docker-compose up -d db
15+
sleep 8
1416
. $(PYBIN)/activate; python manage.py test todo/integrationtests
17+
docker-compose down
1518

1619
migrations: venv
1720
. $(PYBIN)/activate; python manage.py makemigrations todo
@@ -25,8 +28,8 @@ makesuperuser:
2528
oneclickbuild:
2629
docker-compose up --build
2730

28-
db-only:
29-
docker-compose up db
31+
db-up:
32+
docker-compose up db -d
3033

3134
shutdown:
3235
docker-compose down
@@ -39,4 +42,20 @@ run: venv
3942

4043
clean:
4144
rm -rf venv
42-
find . -iname "*.pyc" -delete
45+
find . -iname "*.pyc" -delete
46+
47+
build-docker:
48+
docker build . --tag class/api
49+
50+
# CD
51+
deploy: test integration-test build-docker
52+
kubectl delete pod -l app=api
53+
kubectl apply -f infrastructure/api.yml
54+
55+
# Localhost access
56+
bind-api-locally:
57+
kubectl port-forward service/api 8000:8000
58+
59+
# Installation and set-up
60+
install-kube-postgres:
61+
kubectl apply -f infrastructure/postgres.yml

api/infrastructure/api.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: api
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: api
10+
template:
11+
metadata:
12+
labels:
13+
app: api
14+
spec:
15+
containers:
16+
- name: api
17+
image: class/api:latest
18+
imagePullPolicy: Never
19+
ports:
20+
- containerPort: 8000
21+
env:
22+
- name: DATABASE_URL
23+
value: postgres://postgres:postgres@postgres:5432/postgres
24+
25+
---
26+
apiVersion: v1
27+
kind: Service
28+
metadata:
29+
name: api
30+
spec:
31+
selector:
32+
app: api
33+
ports:
34+
- port: 8000
35+
targetPort: 8000
36+
type: NodePort

api/infrastructure/postgres.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: postgres
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
service: postgres
10+
template:
11+
metadata:
12+
labels:
13+
service: postgres
14+
spec:
15+
containers:
16+
- name: postgres
17+
image: postgres:latest
18+
ports:
19+
- containerPort: 5432
20+
env:
21+
- name: POSTGRES_DB
22+
value: postgres
23+
- name: POSTGRES_USER
24+
value: postgres
25+
- name: POSTGRES_PASSWORD
26+
value: postgres
27+
28+
---
29+
apiVersion: v1
30+
kind: Service
31+
metadata:
32+
name: postgres
33+
spec:
34+
selector:
35+
service: postgres
36+
ports:
37+
- port: 5432
38+
targetPort: 5432
39+
type: NodePort

api/todo/serializers.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ class TodoSerializer(serializers.ModelSerializer):
1717
title = TitleField()
1818

1919
class Meta:
20+
2021
model = Todo
2122
fields = ('id', 'title', 'description', 'priority', 'completed')

0 commit comments

Comments
 (0)