Skip to content

Commit 179baa8

Browse files
committed
Merge branch 'dev'
2 parents d7fb25f + b15ee63 commit 179baa8

13 files changed

Lines changed: 460 additions & 34 deletions

app/Dockerfile

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
# Stage 1: Build dependencies
1+
# Stage 1: Build Python dependencies
22
FROM python:3.10-alpine AS build
3-
COPY requirements.txt .
3+
WORKDIR /api/app
4+
5+
# Install build dependencies for psycopg2
6+
RUN apk add --no-cache gcc musl-dev postgresql-dev
7+
8+
# Copy requirements and install dependencies
9+
COPY requirements.txt ./
410
RUN pip install --user --no-cache-dir -r requirements.txt \
5-
&& find /root/.local -name '*.pyc' -delete \
6-
&& find /root/.local -name '__pycache__' -delete
11+
&& find /root/.local -name '*.pyc' -delete \
12+
&& find /root/.local -name '__pycache__' -delete
713

8-
# Stage 2: Main app
14+
# Stage 2: Main application image
915
FROM python:3.10-alpine AS main
10-
WORKDIR /app
16+
WORKDIR /api
17+
18+
# Install PostgreSQL client for pg_isready
19+
RUN apk add --no-cache postgresql-client
20+
21+
# Copy installed Python packages from build stage
1122
COPY --from=build /root/.local /root/.local
12-
COPY . .
23+
24+
# Copy the entire app folder
25+
COPY . ./app
26+
27+
# Add pip binaries to PATH
1328
ENV PATH=/root/.local/bin:$PATH
14-
ENV FLASK_APP=wsgi.py
29+
ENV FLASK_APP=app/wsgi.py
30+
31+
# Expose port
1532
EXPOSE 5000
1633

17-
# Use Gunicorn instead of Flask dev server
18-
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "wsgi:app", "--workers", "2"]
34+
# Start Gunicorn server
35+
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app.wsgi:app", "--workers", "2"]
File renamed without changes.
File renamed without changes.

migrations/versions/34041a3fcbde_initial_migration.py renamed to app/migrations/versions/34041a3fcbde_initial_migration.py

File renamed without changes.
File renamed without changes.

docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
container_name: postgres-container
3535
restart: always
3636
env_file:
37-
- ./.env.prod
37+
- ${ENV_FILE}
3838
ports:
3939
- "5432:5432"
4040
volumes:

k8s/application.yaml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# ----------------------
2+
# Namespace for application
3+
# ----------------------
4+
apiVersion: v1
5+
kind: Namespace
6+
metadata:
7+
name: student-api
8+
---
9+
# ----------------------
10+
# ConfigMap for Flask API
11+
# ----------------------
12+
apiVersion: v1
13+
kind: ConfigMap
14+
metadata:
15+
name: flask-config
16+
namespace: student-api
17+
data:
18+
POSTGRES_HOST: "postgres"
19+
POSTGRES_PORT: "5432"
20+
POSTGRES_DB: "studentdb"
21+
---
22+
# ----------------------
23+
# Deployment for Flask REST API
24+
# ----------------------
25+
apiVersion: apps/v1
26+
kind: Deployment
27+
metadata:
28+
name: flask-api
29+
namespace: student-api
30+
spec:
31+
replicas: 2
32+
selector:
33+
matchLabels:
34+
app: flask-api
35+
template:
36+
metadata:
37+
labels:
38+
app: flask-api
39+
spec:
40+
nodeSelector:
41+
type: application
42+
initContainers:
43+
- name: db-migrations
44+
image: akhilthyadi/flask-app:7.0.0
45+
workingDir: /api/app
46+
env:
47+
- name: FLASK_APP
48+
value: "wsgi.py"
49+
- name: POSTGRES_USER
50+
valueFrom:
51+
secretKeyRef:
52+
name: postgres-secret
53+
key: POSTGRES_USER
54+
- name: POSTGRES_PASSWORD
55+
valueFrom:
56+
secretKeyRef:
57+
name: postgres-secret
58+
key: POSTGRES_PASSWORD
59+
- name: POSTGRES_HOST
60+
valueFrom:
61+
configMapKeyRef:
62+
name: flask-config
63+
key: POSTGRES_HOST
64+
- name: POSTGRES_PORT
65+
valueFrom:
66+
configMapKeyRef:
67+
name: flask-config
68+
key: POSTGRES_PORT
69+
- name: POSTGRES_DB
70+
valueFrom:
71+
configMapKeyRef:
72+
name: flask-config
73+
key: POSTGRES_DB
74+
command:
75+
- sh
76+
- -c
77+
- |
78+
echo "Waiting for Postgres at $POSTGRES_HOST:$POSTGRES_PORT..."
79+
until pg_isready -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER; do
80+
sleep 2
81+
done
82+
echo "Postgres is ready, running migrations..."
83+
flask db upgrade
84+
containers:
85+
- name: flask-api
86+
image: akhilthyadi/flask-app:7.0.0
87+
ports:
88+
- containerPort: 5000
89+
env:
90+
- name: POSTGRES_USER
91+
valueFrom:
92+
secretKeyRef:
93+
name: postgres-secret
94+
key: POSTGRES_USER
95+
- name: POSTGRES_PASSWORD
96+
valueFrom:
97+
secretKeyRef:
98+
name: postgres-secret
99+
key: POSTGRES_PASSWORD
100+
- name: POSTGRES_HOST
101+
valueFrom:
102+
configMapKeyRef:
103+
name: flask-config
104+
key: POSTGRES_HOST
105+
- name: POSTGRES_PORT
106+
valueFrom:
107+
configMapKeyRef:
108+
name: flask-config
109+
key: POSTGRES_PORT
110+
- name: POSTGRES_DB
111+
valueFrom:
112+
configMapKeyRef:
113+
name: flask-config
114+
key: POSTGRES_DB
115+
---
116+
# ----------------------
117+
# Service to expose Flask API
118+
# ----------------------
119+
apiVersion: v1
120+
kind: Service
121+
metadata:
122+
name: flask-api-service
123+
namespace: student-api
124+
spec:
125+
selector:
126+
app: flask-api
127+
ports:
128+
- protocol: TCP
129+
port: 80
130+
targetPort: 5000
131+
type: NodePort

k8s/database.yaml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
# Namespace for database
3+
apiVersion: v1
4+
kind: Namespace
5+
metadata:
6+
name: student-api
7+
---
8+
# ConfigMap for database host, port, and DB name
9+
apiVersion: v1
10+
kind: ConfigMap
11+
metadata:
12+
name: postgres-config
13+
namespace: student-api
14+
data:
15+
POSTGRES_HOST: postgres
16+
POSTGRES_PORT: "5432"
17+
POSTGRES_DB: studentdb
18+
---
19+
# PersistentVolumeClaim for Postgres storage
20+
apiVersion: v1
21+
kind: PersistentVolumeClaim
22+
metadata:
23+
name: postgres-pvc
24+
namespace: student-api
25+
spec:
26+
accessModes:
27+
- ReadWriteOnce
28+
resources:
29+
requests:
30+
storage: 1Gi
31+
---
32+
# Deployment for Postgres
33+
apiVersion: apps/v1
34+
kind: Deployment
35+
metadata:
36+
name: postgres
37+
namespace: student-api
38+
spec:
39+
replicas: 1
40+
selector:
41+
matchLabels:
42+
app: postgres
43+
template:
44+
metadata:
45+
labels:
46+
app: postgres
47+
spec:
48+
nodeSelector:
49+
type: database
50+
containers:
51+
- name: postgres
52+
image: postgres:15
53+
ports:
54+
- containerPort: 5432
55+
env:
56+
# Secrets for username & password
57+
- name: POSTGRES_USER
58+
valueFrom:
59+
secretKeyRef:
60+
name: postgres-secret
61+
key: POSTGRES_USER
62+
- name: POSTGRES_PASSWORD
63+
valueFrom:
64+
secretKeyRef:
65+
name: postgres-secret
66+
key: POSTGRES_PASSWORD
67+
# ConfigMap for DB name, host, port
68+
- name: POSTGRES_DB
69+
valueFrom:
70+
configMapKeyRef:
71+
name: postgres-config
72+
key: POSTGRES_DB
73+
- name: POSTGRES_HOST
74+
valueFrom:
75+
configMapKeyRef:
76+
name: postgres-config
77+
key: POSTGRES_HOST
78+
- name: POSTGRES_PORT
79+
valueFrom:
80+
configMapKeyRef:
81+
name: postgres-config
82+
key: POSTGRES_PORT
83+
volumeMounts:
84+
- name: postgres-storage
85+
mountPath: /var/lib/postgresql/data
86+
volumes:
87+
- name: postgres-storage
88+
persistentVolumeClaim:
89+
claimName: postgres-pvc
90+
---
91+
# Service to expose Postgres
92+
apiVersion: v1
93+
kind: Service
94+
metadata:
95+
name: postgres
96+
namespace: student-api
97+
spec:
98+
selector:
99+
app: postgres
100+
ports:
101+
- protocol: TCP
102+
port: 5432
103+
targetPort: 5432
104+
type: ClusterIP

0 commit comments

Comments
 (0)