Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 62c6113

Browse files
committed
wip implement static site service and deployment
1 parent 421a097 commit 62c6113

File tree

15 files changed

+224
-17
lines changed

15 files changed

+224
-17
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
documentation/
2+
cloudformation/
3+
.vscode
4+
cypress/videos/
5+
gitlab-runner
6+
kubernetes
7+
node_modules
8+
**/node_modules
9+
notes

backend/backend/settings/minikube.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from .development import * # noqa
22

3+
print("loading minikube settings...")
4+
35
DATABASES = {
46
'default': {
57
'ENGINE': 'django.db.backends.postgresql_psycopg2',
121 KB
Loading

documentation/docs/topics/minikube/README.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Notice that the `DOCKER_HOST` is pointing to the minikube VM on docker's default
4747
With these environment variables set, let's build the Django container image with the following command:
4848

4949
```bash
50-
docker-compose build backend
50+
docker build -t backend:<TAG> -f backend/scripts/dev/Dockerfile backend/
5151
```
5252

5353
**`deployment.yml`**
@@ -184,4 +184,88 @@ spec:
184184
185185
::: warning storageClassName
186186
Clicking on the `storageClassName` gave a 404 error
187-
:::
187+
:::
188+
189+
::: warning Authentication failure
190+
Message with postgres authentication failure
191+
:::
192+
193+
After changing the `postgres` user password, the migrations are able to run successfully:
194+
195+
```bash
196+
k exec django-784d668c8b-9gbf7 -it -- ./manage.py migrat
197+
e
198+
loading minikube settings...
199+
Operations to perform:
200+
Apply all migrations: accounts, admin, auth, contenttypes, sessions, social_django
201+
Running migrations:
202+
Applying contenttypes.0001_initial... OK
203+
Applying contenttypes.0002_remove_content_type_name... OK
204+
Applying auth.0001_initial... OK
205+
Applying auth.0002_alter_permission_name_max_length... OK
206+
Applying auth.0003_alter_user_email_max_length... OK
207+
Applying auth.0004_alter_user_username_opts... OK
208+
Applying auth.0005_alter_user_last_login_null... OK
209+
Applying auth.0006_require_contenttypes_0002... OK
210+
Applying auth.0007_alter_validators_add_error_messages... OK
211+
Applying auth.0008_alter_user_username_max_length... OK
212+
Applying auth.0009_alter_user_last_name_max_length... OK
213+
Applying auth.0010_alter_group_name_max_length... OK
214+
Applying auth.0011_update_proxy_permissions... OK
215+
Applying accounts.0001_initial... OK
216+
Applying admin.0001_initial... OK
217+
Applying admin.0002_logentry_remove_auto_add... OK
218+
Applying admin.0003_logentry_add_action_flag_choices... OK
219+
Applying sessions.0001_initial... OK
220+
Applying social_django.0001_initial... OK
221+
Applying social_django.0002_add_related_name... OK
222+
Applying social_django.0003_alter_email_max_length... OK
223+
Applying social_django.0004_auto_20160423_0400... OK
224+
Applying social_django.0005_auto_20160727_2333... OK
225+
Applying social_django.0006_partial... OK
226+
Applying social_django.0007_code_timestamp... OK
227+
Applying social_django.0008_partial_timestamp... OK
228+
```
229+
230+
::: warning Try this again
231+
Try this again with a clean version of minikube and using the Secrets resource.
232+
:::
233+
234+
I initially started the postgres container with a password set by environment variable. This may have set data in the
235+
236+
::: tip kubectl cheatsheet from kubernetes documentation
237+
[https://kubernetes.io/docs/reference/kubectl/cheatsheet/#viewing-finding-resources](https://kubernetes.io/docs/reference/kubectl/cheatsheet/#viewing-finding-resources)
238+
:::
239+
240+
241+
Show the service in kubernetes:
242+
243+
```
244+
minikube service kubernetes-django-service
245+
```
246+
247+
We have two options for how to run the frontend application in Kubernetes.
248+
249+
1) Servce the static content from Django
250+
2) Serve the static content from nginx and use another service.
251+
252+
Serving the static content from Django was not working, so I'm building another deployment/service for frontend. For this to work, we need to tell Quasar about the address for the Django service. There are two ways to do this:
253+
254+
1) DNS
255+
2) Environment variables
256+
257+
258+
## Use environment variables to get service IP/Host
259+
260+
This won't be possible with out static front end site.
261+
262+
## Use DNS for services
263+
264+
DNS will be easier since we are building static assets outside of the context of Kubernetes and the environment variables that it injects at runtime.
265+
266+
```
267+
/ # curl http://kubernetes-django-service:8000/api/
268+
{"message": "Root"}
269+
```
270+
271+
We need to set the `DOMAIN_NAME` environment variable to `kubernetes-django-service`, and also set the port, and we should be able to access the backend from frontend AJAX calls.

kubernetes/django/deployment.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec:
1515
containers:
1616
- name: backend
1717
imagePullPolicy: IfNotPresent
18-
image: backend:2
18+
image: backend:12
1919
command: ["./manage.py", "runserver", "0.0.0.0:8000"]
2020
ports:
2121
- containerPort: 8000
@@ -40,6 +40,9 @@ spec:
4040
# name: postgres-credentials
4141
# key: password
4242

43+
- name: DEBUG
44+
value: "True"
45+
4346
- name: POSTGRES_HOST
4447
value: postgres-service
4548

kubernetes/django/service.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: kubernetes-django-service
5+
spec:
6+
selector:
7+
app: django-container
8+
ports:
9+
- protocol: TCP
10+
port: 8000
11+
targetPort: 8000
12+
type: NodePort

kubernetes/frontend/deployment.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apps/v1beta2
2+
kind: Deployment
3+
metadata:
4+
name: frontend-deployment
5+
labels:
6+
app: frontend
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: frontend-container
12+
template:
13+
metadata:
14+
labels:
15+
app: frontend-container
16+
spec:
17+
containers:
18+
- name: frontend
19+
imagePullPolicy: IfNotPresent
20+
image: frontend:5

kubernetes/frontend/service.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: kubernetes-frontend-service
5+
spec:
6+
selector:
7+
app: frontend-container
8+
ports:
9+
- protocol: TCP
10+
port: 80
11+
targetPort: 80
12+
type: NodePort

nginx/minikube/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# build stage
2+
FROM node:10-alpine as build-stage
3+
WORKDIR /app/
4+
ENV DOMAIN_NAME 192.168.99.101:30958
5+
ENV HTTP_PROTOCOL http
6+
COPY quasar/package.json /app/
7+
RUN npm cache verify
8+
RUN npm install -g @quasar/cli
9+
RUN npm install --progress=false
10+
COPY quasar /app/
11+
RUN quasar build -m pwa
12+
13+
# minikube stage
14+
FROM nginx:1.13.12-alpine as minikube
15+
COPY nginx/minikube/minikube.conf /etc/nginx/nginx.conf
16+
COPY --from=build-stage /app/dist/pwa /dist/
17+
EXPOSE 80
18+
CMD ["nginx", "-g", "daemon off;"]

nginx/minikube/minikube.conf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
user nginx;
2+
worker_processes 1;
3+
4+
events {
5+
worker_connections 1024;
6+
}
7+
8+
http {
9+
include /etc/nginx/mime.types;
10+
client_max_body_size 100m;
11+
12+
# upstream backend {
13+
# server backend:8000;
14+
# }
15+
16+
# upstream asgiserver {
17+
# server asgiserver:9000;
18+
# }
19+
20+
server {
21+
listen 80;
22+
charset utf-8;
23+
24+
root /dist/;
25+
index index.html;
26+
27+
# frontend
28+
location / {
29+
try_files $uri $uri/ @rewrites;
30+
}
31+
32+
location @rewrites {
33+
rewrite ^(.+)$ /index.html last;
34+
}
35+
36+
# location /ws/ {
37+
# proxy_pass http://asgiserver;
38+
# proxy_http_version 1.1;
39+
# proxy_set_header Upgrade $http_upgrade;
40+
# proxy_set_header Connection "upgrade";
41+
# # proxy_redirect off;
42+
# }
43+
44+
# # backend urls
45+
# location ~ ^/(admin|api|media|static) {
46+
# proxy_redirect off;
47+
# proxy_pass http://backend;
48+
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
49+
# proxy_set_header Host $http_host;
50+
# }
51+
}
52+
}

0 commit comments

Comments
 (0)