Skip to content

Commit 0b5ff2e

Browse files
committed
PEER-236: Add K8s configs
Signed-off-by: SeeuSim <[email protected]>
1 parent 4a50ebb commit 0b5ff2e

10 files changed

+279
-3
lines changed

docker-compose.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ services:
294294
matching-service:
295295
condition: service_healthy
296296
restart: true
297+
collab-service:
298+
condition: service_healthy
299+
restart: true
300+
chat-service:
301+
condition: service_healthy
302+
restart: true
297303
networks:
298304
- user-api-network
299305
- question-api-network

k8s/02-chat-api-network.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: chat-api-network-policy
5+
namespace: peerprep
6+
spec:
7+
podSelector:
8+
matchLabels:
9+
peerprep.network.chat-api: "true"
10+
policyTypes:
11+
- Ingress
12+
- Egress
13+
ingress:
14+
- from:
15+
- podSelector:
16+
matchLabels:
17+
peerprep.network.chat-api: "true"
18+
egress:
19+
- to:
20+
- podSelector:
21+
matchLabels:
22+
peerprep.network.chat-api: "true"

k8s/02-chat-db-network.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: NetworkPolicy
3+
metadata:
4+
name: chat-db-network-policy
5+
namespace: peerprep
6+
spec:
7+
podSelector:
8+
matchLabels:
9+
peerprep.network.chat-db: "true"
10+
policyTypes:
11+
- Ingress
12+
- Egress
13+
ingress:
14+
- from:
15+
- podSelector:
16+
matchLabels:
17+
peerprep.network.chat-db: "true"
18+
egress:
19+
- to:
20+
- podSelector:
21+
matchLabels:
22+
peerprep.network.chat-db: "true"

k8s/03-chat-db-deployment.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
apiVersion: apps/v1
2+
kind: StatefulSet
3+
metadata:
4+
name: chat-db
5+
namespace: peerprep
6+
labels:
7+
project: peerprep
8+
peerprep.service: chat-db
9+
spec:
10+
serviceName: chat-db
11+
replicas: 1
12+
selector:
13+
matchLabels:
14+
project: peerprep
15+
peerprep.service: chat-db
16+
template:
17+
metadata:
18+
labels:
19+
project: peerprep
20+
peerprep.service: chat-db
21+
peerprep.network.chat-db: "true"
22+
spec:
23+
restartPolicy: Always
24+
containers:
25+
- name: postgres
26+
image: postgres:16.4
27+
imagePullPolicy: IfNotPresent
28+
env:
29+
- name: PGDATA
30+
value: /data/chat-db
31+
- name: POSTGRES_DB
32+
value: chat
33+
- name: POSTGRES_USER
34+
valueFrom:
35+
secretKeyRef:
36+
name: chat-secret
37+
key: POSTGRES_USER
38+
- name: POSTGRES_PASSWORD
39+
valueFrom:
40+
secretKeyRef:
41+
name: chat-secret
42+
key: POSTGRES_PASSWORD
43+
livenessProbe:
44+
exec:
45+
command:
46+
- /bin/sh
47+
- -c
48+
- |
49+
pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"
50+
periodSeconds: 10
51+
initialDelaySeconds: 30
52+
timeoutSeconds: 10
53+
failureThreshold: 5
54+
ports:
55+
- name: chat-db-5432
56+
containerPort: 5432
57+
volumeMounts:
58+
- name: chat-db-vol
59+
mountPath: /data
60+
volumeClaimTemplates:
61+
- metadata:
62+
name: chat-db-vol
63+
spec:
64+
accessModes: ["ReadWriteOnce"]
65+
storageClassName: "standard" # Update this based on your storage class
66+
resources:
67+
requests:
68+
storage: 500Mi # Define your storage requirement

k8s/03-chat-db-service.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: chat-db
5+
namespace: peerprep
6+
labels:
7+
project: peerprep
8+
peerprep.service: chat-db
9+
spec:
10+
type: ClusterIP
11+
selector:
12+
project: peerprep
13+
peerprep.service: chat-db
14+
ports:
15+
- name: chat-db-5432
16+
port: 5432
17+
targetPort: chat-db-5432

k8s/04-chat-svc-deployment.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: chat-service
5+
namespace: peerprep
6+
labels:
7+
project: peerprep
8+
peerprep.service: chat-service
9+
spec:
10+
replicas: 1
11+
selector:
12+
matchLabels:
13+
project: peerprep
14+
peerprep.service: chat-service
15+
strategy:
16+
type: Recreate
17+
template:
18+
metadata:
19+
labels:
20+
project: peerprep
21+
peerprep.service: chat-service
22+
peerprep.network.chat-api: "true"
23+
peerprep.network.chat-db: "true"
24+
spec:
25+
initContainers:
26+
- name: wait-for-postgres
27+
image: postgres:16.4
28+
env:
29+
- name: PGHOST
30+
valueFrom:
31+
secretKeyRef:
32+
name: chat-secret
33+
key: EXPRESS_DB_HOST
34+
- name: PGPORT
35+
valueFrom:
36+
secretKeyRef:
37+
name: chat-secret
38+
key: EXPRESS_DB_PORT
39+
- name: PGPASSWORD
40+
valueFrom:
41+
secretKeyRef:
42+
name: chat-secret
43+
key: POSTGRES_PASSWORD
44+
- name: PGUSER
45+
valueFrom:
46+
secretKeyRef:
47+
name: chat-secret
48+
key: POSTGRES_USER
49+
- name: PGDATABASE
50+
valueFrom:
51+
secretKeyRef:
52+
name: chat-secret
53+
key: POSTGRES_DB
54+
command:
55+
- /bin/sh
56+
- -c
57+
- |
58+
while ! psql -c 'SELECT 1' > /dev/null 2>&1; do
59+
echo "Waiting for $PGDATABASE"
60+
sleep 1
61+
done
62+
echo "$PGDATABASE is ready!"
63+
64+
containers:
65+
- name: chat-express
66+
image: ay2425s1cs3219g16/chat-express:latest
67+
imagePullPolicy: Always
68+
envFrom:
69+
- secretRef:
70+
name: chat-secret
71+
env:
72+
# When load testing, it exposes port 80 by default.
73+
- name: LOAD_TEST_POD
74+
value: http://chat-service-load-test
75+
- name: PEERPREP_UI_HOST
76+
value: http://frontend
77+
livenessProbe:
78+
exec:
79+
command:
80+
- /bin/sh
81+
- -c
82+
- wget --no-verbose --tries=1 --spider http://localhost:9005/health || exit 1
83+
periodSeconds: 30
84+
initialDelaySeconds: 5
85+
timeoutSeconds: 10
86+
failureThreshold: 5
87+
ports:
88+
- name: chat-svc-ctr
89+
containerPort: 9005
90+
resources:
91+
requests:
92+
cpu: 250m
93+
memory: 256Mi
94+
limits:
95+
cpu: 500m
96+
memory: 512Mi
97+

k8s/04-chat-svc-hpa.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: autoscaling/v2
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: chat-service-hpa
5+
namespace: peerprep
6+
spec:
7+
scaleTargetRef:
8+
apiVersion: apps/v1
9+
kind: Deployment
10+
name: chat-service
11+
minReplicas: 1
12+
maxReplicas: 5
13+
metrics:
14+
- type: Resource
15+
resource:
16+
name: cpu
17+
target:
18+
type: Utilization
19+
averageUtilization: 70

k8s/04-chat-svc-service.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: chat-service
5+
namespace: peerprep
6+
labels:
7+
project: peerprep
8+
peerprep.service: chat-service
9+
spec:
10+
type: ClusterIP
11+
selector:
12+
project: peerprep
13+
peerprep.service: chat-service
14+
ports:
15+
- name: chat-svc-prt
16+
port: 9005
17+
targetPort: chat-svc-ctr

k8s/05-frontend-deployment.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ spec:
3636
value: http://collab-service:9003
3737
- name: VITE_MATCHING_SERVICE
3838
value: http://matching-service:9004
39+
- name: VITE_CHAT_SERVICE
40+
value: http://chat-service:9005
3941
command:
4042
- /bin/sh
4143
- -c
@@ -60,6 +62,11 @@ spec:
6062
sleep 2
6163
done &
6264
65+
until wget --no-verboser --tries=1 --spider "$VITE_CHAT_SERVICE/health"; do
66+
echo "Waiting for Chat Service"
67+
sleep 2
68+
done &
69+
6370
wait
6471
echo "All services healthy"
6572
@@ -81,6 +88,8 @@ spec:
8188
value: http://collab-service:9003
8289
- name: VITE_MATCHING_SERVICE
8390
value: http://matching-service:9004
91+
- name: VITE_CHAT_SERVICE
92+
value: http://chat-service:9005
8493
ports:
8594
- name: frontend-ctr
8695
containerPort: 3000

scripts/k8s-up.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ else
88
echo "Namespace exists, creating secrets"
99
fi
1010

11-
pkgs=("backend/user" "backend/question" "backend/collaboration" "backend/matching" "frontend")
11+
pkgs=("backend/user" "backend/question" "backend/collaboration" "backend/matching" "backend/chat" "frontend")
1212

1313
create_secret() {
1414
local secretName=$1
@@ -41,5 +41,4 @@ for pkg in "${pkgs[@]}"; do
4141
create_secret $pkg
4242
done
4343

44-
cd k8s
45-
kubectl apply -f .
44+
kubectl apply -f ./k8s

0 commit comments

Comments
 (0)