Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8s Deployment #43

Merged
merged 17 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/ai-hint-service/app/.env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
OPENAI_API_KEY="API_KEY"
QUESTION_SERVICE_URL = "http://question:3002"
OPENAI_API_KEY=sk-proj-API_KEY
QUESTION_SERVICE_URL=http://question:3002
2 changes: 1 addition & 1 deletion backend/code-execution-service/worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.10

WORKDIR /app

RUN apt-get update \
RUN apt-get update --fix-missing \
# javascript
&& apt-get install -y nodejs \
# tsc
Expand Down
3 changes: 1 addition & 2 deletions backend/communication-service/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./dev.db"
PORT=3004
PEERJS_PORT = 9000
PEERJS_PORT=9000
2 changes: 1 addition & 1 deletion backend/communication-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ generator client {

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
url = "file:./dev.db"
}

model UserRoomMapping {
Expand Down
5 changes: 3 additions & 2 deletions backend/matching-service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PRISMA_DATABASE_URL="file:./dev.db"
QUESTION_SERVICE="http://question:3002"
QUESTION_SERVICE=http://question:3002
PORT=3003
RABBITMQ_URL=amqp://rabbitmq:5672
1 change: 1 addition & 0 deletions backend/matching-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ COPY package*.json .

RUN npm install
RUN npm install -g prisma
RUN npm install -g ts-node

COPY . .
RUN npx prisma migrate dev --name init
Expand Down
2 changes: 1 addition & 1 deletion backend/matching-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
datasource db {
provider = "sqlite"
url = env("PRISMA_DATABASE_URL")
url = "file:./dev.db"
}

generator client {
Expand Down
2 changes: 1 addition & 1 deletion backend/matching-service/src/rabbitmq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
handleUserRequestWithoutRelaxedConstraints,
} from "./matchingService";

const RABBITMQ_URL = process.env.RABBITMQ_URL || "amqp://localhost";
const RABBITMQ_URL = process.env.RABBITMQ_URL || "amqp://rabbitmq:5672";
const QUEUE_NAME = "topic_queue_math";
const DELAY_EXCHANGE = "delayed_exchange";
const DELAY_QUEUE = "delayed_queue";
Expand Down
4 changes: 2 additions & 2 deletions backend/question-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ app.use(
app.use("/", questionRoutes);

// Start the server
app.listen(PORT, () => {
connectToDB();
app.listen(PORT, async () => {
await connectToDB();
console.log(`Server running on port ${PORT}`);
});
42 changes: 31 additions & 11 deletions backend/question-service/src/util/db.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import mongoose from "mongoose";
import "dotenv/config";

const MAX_RETRIES = 5;
const INITIAL_DELAY = 1000;

export const connectToDB = async () => {
try {
let uri =
process.env.ENV === "PROD"
? process.env.DB_CLOUD_URI
: process.env.DB_LOCAL_URI;
if (!uri) {
throw new Error("No URI provided");
let retries = 0;
let delay = INITIAL_DELAY;

const uri =
process.env.ENV === "PROD"
? process.env.DB_CLOUD_URI
: process.env.DB_LOCAL_URI;
if (!uri) {
throw new Error("No URI provided");
}

while (retries < MAX_RETRIES) {
try {
await mongoose.connect(uri, {});
console.log("Connected to MongoDB");
break;
} catch (err) {
console.log(`Connection attempt ${retries + 1} failed:`, err);

retries += 1;
if (retries < MAX_RETRIES) {
console.log(`Retrying in ${delay / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, delay));
delay *= 2; // Exponential backoff: double the delay
} else {
console.log("Max retries reached. Could not connect to MongoDB.");
throw new Error("Could not connect to MongoDB");
}
}
await mongoose.connect(uri, {});
console.log("Connected to MongoDB");
} catch (err) {
console.log(err);
}
};
102 changes: 82 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,90 @@
services:
question:
build: ./backend/question-service
image: echomo/cs3219-question:test1
ports:
- "3002:3002"
restart: always
environment:
- DB_CLOUD_URI=<> # add your own DB_CLOUD_URI
- PORT=3002
- ENV=PROD
- CODE_EXECUTION_SERVICE_URL=http://code-execution-server:7002

user:
build: ./backend/user-service
image: echomo/cs3219-user:test0
ports:
- "3001:3001"
restart: always
environment:
- DB_CLOUD_URI=<> # add your own DB_CLOUD_URI
- PORT=3001
- ENV=PROD
- JWT_SECRET=secret

code-execution-rabbitmq:
image: rabbitmq:4.0-management
ports:
- "7004:5672"
healthcheck:
test: ["CMD", "rabbitmqctl", "status"]
interval: 10s
timeout: 5s
retries: 5

code-execution-redis:
image: redis:alpine
ports:
- "7001:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5

code-execution-server:
build: ./backend/code-execution-service/server
image: echomo/cs3219-code-exec-server:test0
ports:
- "7002:7002"
restart: always
depends_on:
- code-execution-rabbitmq
- code-execution-redis
code-execution-rabbitmq:
condition: service_healthy
code-execution-redis:
condition: service_healthy
environment:
- RABBITMQ_PORT=5672
- RABBITMQ_HOST=code-execution-rabbitmq
- REDIS_HOST=code-execution-redis
- REDIS_PORT=6379
- USER_SERVICE_URL=http://user:3001

code-execution-worker:
build: ./backend/code-execution-service/worker
image: echomo/cs3219-code-exec-worker:test0
pids_limit: 60
cpus: 1
mem_limit: 150M
restart: on-failure
depends_on:
- code-execution-rabbitmq
- code-execution-redis
code-execution-rabbitmq:
condition: service_healthy
code-execution-redis:
condition: service_healthy
environment:
- RABBITMQ_PORT=5672
- RABBITMQ_HOST=code-execution-rabbitmq
- REDIS_HOST=code-execution-redis
- REDIS_PORT=6379
- COMPILE_SHELL=False
deploy:
replicas: 4
replicas: 1
restart_policy:
condition: any
delay: 3s
window: 5s

rabbitmq:
image: dedsecrattle/rabbitmq-delayed-exchange
restart: always
ports:
- "5672:5672" # RabbitMQ message broker
- "15672:15672" # RabbitMQ management plugin
Expand All @@ -54,44 +95,65 @@ services:
retries: 5

communication:
build: ./backend/communication-service
image: echomo/cs3219-communication:test0
ports:
- "3004:3004"
- "9000:9000"
restart: always
environment:
- PORT=3004
- PEERJS_PORT=9000

matching:
build: ./backend/matching-service
image: echomo/cs3219-matching:test0
depends_on:
rabbitmq:
condition: service_healthy
restart: always
ports:
- "3003:3003"
environment:
DATABASE_URL: "file:./dev.db"
RABBITMQ_URL: "amqp://rabbitmq:5672"
- QUESTION_SERVICE=http://question:3002
- PORT=3003
- RABBITMQ_URL=amqp://rabbitmq:5672

collaboration:
build: ./backend/collaboration-service
image: echomo/cs3219-collaboration:test1
ports:
- "3005:3005"
restart: always
environment:
- LOG_LEVEL=20
- USER_SERVICE_URL=http://user:3001
- MATCHING_SERVICE_URL=http://matching:3003

ai-hint-service:
build: ./backend/ai-hint-service
image: echomo/cs3219-ai-hint:test0
ports:
- "3006:8000"
depends_on:
- question # Assuming 'question' service is needed to fetch question descriptions
- question
environment:
- OPENAI_API_KEY=sk-proj-<> # Add your own OpenAI API key
- QUESTION_SERVICE_URL=http://question:3002

frontend:
build: ./frontend
env_file:
- ./frontend/nginx/.env
image: echomo/cs3219-frontend:test5
environment:
- REACT_APP_USER_SERVICE_URL=http://localhost:3001/
- REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002/
- REACT_APP_MATCHING_SERVICE_URL=http://localhost:3003
- REACT_APP_COMMUNICATION_SERVICE_URL=http://localhost:3004
- REACT_APP_COLLABORATION_SERVICE_URL=http://localhost:3005
- REACT_APP_AI_HINT_URL=http://localhost:3006
- REACT_APP_VIDEO_SERVICE_PORT=9000
ports:
- "80:80"
restart: always
depends_on:
- user
- matching
- question
- collaboration
- rabbitmq
- ai-hint-service # Ensure frontend waits for ai-hint-service
- ai-hint-service
6 changes: 2 additions & 4 deletions frontend/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
PORT=3000

# local server addresses
# REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002
# REACT_APP_USER_SERVICE_URL=http://localhost:3001
Expand All @@ -9,8 +7,8 @@ PORT=3000
# REACT_APP_VIDEO_SERVICE_PORT=9000

# In docker
REACT_APP_QUESTION_SERVICE_URL=http://localhost/api/questions/
REACT_APP_USER_SERVICE_URL=http://localhost/api/users/
REACT_APP_USER_SERVICE_URL=http://localhost:3001/
REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002/
REACT_APP_MATCHING_SERVICE_URL=http://localhost:3003
REACT_APP_COMMUNICATION_SERVICE_URL=http://localhost:3004
REACT_APP_COLLABORATION_SERVICE_URL=http://localhost:3005
Expand Down
23 changes: 17 additions & 6 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ COPY package*.json .
RUN npm install

COPY ./src ./src

COPY ./public ./public

COPY .env .

COPY tsconfig.json .

ARG GENERATE_SOURCEMAP=false
# default arguments for connecting to services, can be overridden by docker-compose
ARG REACT_APP_USER_SERVICE_URL=http://localhost:3001/
ARG REACT_APP_QUESTION_SERVICE_URL=http://localhost:3002/
ARG REACT_APP_MATCHING_SERVICE_URL=http://localhost:3003
ARG REACT_APP_COMMUNICATION_SERVICE_URL=http://localhost:3004
ARG REACT_APP_COLLABORATION_SERVICE_URL=http://localhost:3005
ARG REACT_APP_AI_HINT_URL=http://localhost:3006
ARG REACT_APP_VIDEO_SERVICE_PORT=9000

ENV REACT_APP_USER_SERVICE_URL=$REACT_APP_USER_SERVICE_URL
ENV REACT_APP_QUESTION_SERVICE_URL=$REACT_APP_QUESTION_SERVICE_URL
ENV REACT_APP_MATCHING_SERVICE_URL=$REACT_APP_MATCHING_SERVICE_URL
ENV REACT_APP_COMMUNICATION_SERVICE_URL=$REACT_APP_COMMUNICATION_SERVICE_URL
ENV REACT_APP_COLLABORATION_SERVICE_URL=$REACT_APP_COLLABORATION_SERVICE_URL
ENV REACT_APP_AI_HINT_URL=$REACT_APP_AI_HINT_URL
ENV REACT_APP_VIDEO_SERVICE_PORT=$REACT_APP_VIDEO_SERVICE_PORT


RUN npm run build

FROM nginx:latest

COPY ./nginx /etc/nginx

COPY --from=build /app/build /app
4 changes: 0 additions & 4 deletions frontend/nginx/.env.sample

This file was deleted.

8 changes: 0 additions & 8 deletions frontend/nginx/templates/app.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@ server {
root /app;
try_files $uri /index.html;
}

location /api/questions/ {
proxy_pass ${QUESTION_SERVICE_URL};
}

location /api/users/ {
proxy_pass ${USER_SERVICE_URL};
}
}
Loading