Skip to content

Commit

Permalink
Update: Added K8s & Jenkins CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangsonww committed Oct 15, 2024
1 parent 3fb9891 commit 87542fe
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pipeline {
agent any
stages {
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
}
}
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The **E-Learning Management System** is a web-based platform designed to facilit
- **Frontend**: Built with **Angular** and **Bootstrap**, it offers a user-friendly interface for interacting with the platform.
- **Backend**: Developed using **Django** and **Django REST Framework**, it provides robust **REST APIs** for all the operations.
- **Database**: The system uses **MongoDB** to store data and **Redis** for efficient server-side caching.
- **CI/CD**: The project includes a `Dockerfile` and `docker-compose.yml` for containerization and deployment, as well as a `Jenkinsfile` for CI/CD pipelines and `Kubernetes` configuration files for orchestration.

Because we use **MongoDB**, **Angular**, and **Django**, we call this a **MAD-Stack** application!

Expand Down Expand Up @@ -205,6 +206,8 @@ The project structure is as follows:
```plaintext
Learning-Management-System/
├── LMS-Backend
│ ├── .gitignore
│ ├── Dockerfile
│ ├── manage.py
│ ├── requirements.txt
│ ├── LICENSE
Expand All @@ -231,6 +234,7 @@ Learning-Management-System/
├── LMS-Frontend
│ ├── angular.json
│ ├── package.json
│ ├── Dockerfile
│ ├── README.md
│ ├── LICENSE
│ ├── app/
Expand Down Expand Up @@ -307,7 +311,15 @@ Learning-Management-System/
│ │ └── tsconfig.spec.json
│ ├── LICENSE
│ ├── README.md
├── Kubernetes
│ ├── configmap.yaml
│ ├── backend-deployment.yaml
│ ├── backend-service.yaml
│ ├── frontend-deployment.yaml
│ ├── frontend-service.yaml
├── .gitignore
├── docker-compose.yml
├── Jenkinsfile
├── LICENSE
└── README.md
```
Expand Down Expand Up @@ -604,6 +616,72 @@ The project can be containerized using Docker. The `Dockerfile` and `docker-comp

The above command will build the Docker image and start the containers for the backend, frontend, MongoDB, and Redis. You can access the application at `http://localhost:4200` and the Django REST Framework API at `http://localhost:8000`.

## Kubernetes

The project includes Kubernetes configuration files for deploying the backend and frontend applications. The `Kubernetes` directory contains the following files:

- **configmap.yaml**: Contains the configuration settings for the backend application.
- **backend-deployment.yaml**: Defines the deployment for the backend application.
- **backend-service.yaml**: Defines the service for the backend application.
- **frontend-deployment.yaml**: Defines the deployment for the frontend application.
- **frontend-service.yaml**: Defines the service for the frontend application.

To deploy the applications to a Kubernetes cluster, follow these steps:

1. **Change directory into the `Kubernetes` directory:**

```bash
cd Kubernetes
```

2. **Create the configmap:**

```bash
kubectl apply -f configmap.yaml
```

3. **Create the backend deployment:**

```bash
kubectl apply -f backend-deployment.yaml
```

4. **Create the backend service:**

```bash
kubectl apply -f backend-service.yaml
```

5. **Create the frontend deployment:**

```bash
kubectl apply -f frontend-deployment.yaml
```

6. **Create the frontend service:**

```bash
kubectl apply -f frontend-service.yaml
```

The above commands will create the deployments and services for the backend and frontend applications. You can access the applications using the NodePort or LoadBalancer service IP addresses.

## Jenkins CI/CD

The project includes a `Jenkinsfile` for setting up CI/CD pipelines using Jenkins. The Jenkinsfile defines the stages for building, testing, and deploying the backend and frontend applications.

To set up the CI/CD pipelines using Jenkins, follow these steps:

1. **Install Jenkins on your system or use a cloud-based Jenkins service.**

2. **Create a new Jenkins pipeline project.**

3. **Configure the pipeline to use the Jenkinsfile in the project repository.**

4. **Run the pipeline to build, test, and deploy the applications.**

The Jenkins pipeline will automatically build the Docker images, run the unit tests, and deploy the applications to a Kubernetes cluster.

## Troubleshooting

### Common Issues
Expand Down
31 changes: 31 additions & 0 deletions kubernetes/backend-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
labels:
app: backend
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: lms-backend:latest # Build and push this to a container registry
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: "production"
volumeMounts:
- name: backend-code
mountPath: /app
volumes:
- name: backend-code
hostPath:
path: /home/user/project/backend
12 changes: 12 additions & 0 deletions kubernetes/backend-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 3000
targetPort: 3000
type: ClusterIP # Internal service
7 changes: 7 additions & 0 deletions kubernetes/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: lms-app-config
data:
NODE_ENV: "production"
REACT_APP_BACKEND_URL: "http://backend-service:3000"
31 changes: 31 additions & 0 deletions kubernetes/frontend-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
labels:
app: frontend
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: lms-frontend:latest # Build and push this to a container registry
ports:
- containerPort: 3001
env:
- name: ANGULAR_APP_BACKEND_URL
value: "http://backend-service:3000" # This service will route to the backend service
volumeMounts:
- name: frontend-code
mountPath: /app
volumes:
- name: frontend-code
hostPath:
path: /home/user/project/frontend
12 changes: 12 additions & 0 deletions kubernetes/frontend-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- protocol: TCP
port: 3001
targetPort: 3001
type: NodePort # Expose frontend for external access

0 comments on commit 87542fe

Please sign in to comment.