Skip to content

Commit

Permalink
feat: initial commit - commit old project
Browse files Browse the repository at this point in the history
Signed-off-by: Muriel Paraire <[email protected]>
  • Loading branch information
MurielParaire committed Jun 12, 2024
0 parents commit 8f2b1ed
Show file tree
Hide file tree
Showing 22 changed files with 459 additions and 0 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Project instructions

Take old project and implement monitoring / observability / dashboarding.


# Push the docker image to ghcr

## login to ghcr

```bash
docker login ghcr.io -u USERNAME
```

## build image

```bash
cd web
docker build -t ghcr.io/do3-2023/mpa-kube/<web/api>:<version> .
```

## test image

```bash
docker run -itp 8080:8080 ghcr.io/do3-2023/mpa-kube/<web/api>:<version>
```

## Push on repo github

```bash
docker push ghcr.io/do3-2023/mpa-kube/<web/api>:<version>
```

# Deploy the application

Please place yourself at the root folder for the following commands.

## Deploy the database

```bash
kubectl apply -f database/infra
```

## Deploy the api

```
kubectl apply -f api/infra
```

## Deploy the web application

```
kubectl apply -f web/infra
```

# Access the application :

You will need to forward the listening port of the web application to access it on [localhost:8000](http://localhost:8000).
```bash
kubectl port-forward $(kubectl get pods -n mpa-frontend | tail -n 1 | cut -d ' ' -f 1) 8000:8000 -n mpa-frontend
```

The line `$(kubectl get pods -n mpa-frontend | tail -n 1 | cut -d ' ' -f 1)` simply gets the ip of the first (and in this case only) pod.


64 changes: 64 additions & 0 deletions api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
import json
import psycopg2
from flask import Flask

app = Flask(__name__)

conn = 0

POSTGRES_USER = os.environ.get("POSTGRES_USER")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD")
POSTGRES_DB = os.environ.get("POSTGRES_DB")
POSTGRES_HOST = os.environ.get("POSTGRES_HOST")

if not POSTGRES_USER or not POSTGRES_PASSWORD or not POSTGRES_DB or not POSTGRES_HOST:
raise ValueError("Missing database configuration env variables")

try:
# Establishing the database connection
conn = psycopg2.connect(
database=POSTGRES_DB,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host=POSTGRES_HOST,
port=5432
)

except Exception as err1:
raise RuntimeError("Cannot connect to the database") from err1

# Open a cursor to perform database operations
cur = conn.cursor()

def init():
cur.execute("select exists(select * from information_schema.tables where table_name='greetings')")
if not cur.fetchone()[0]:
cur.execute("CREATE TABLE greetings (id SERIAL PRIMARY KEY, greeting character varying(255) NOT NULL);")
cur.execute("INSERT INTO greetings (greeting) VALUES ('Hello world !')")

init()

""" Healthcheck """
@app.route("/healthz")
def health():
try:
cur.execute("SELECT * FROM greetings LIMIT(1)")
return "", 200
except BaseException as err:
print(err)
return "", 500


@app.route("/hello")
def getWord():
try:
cur.execute("SELECT greeting FROM greetings LIMIT(1)")
greetings = cur.fetchone()
except Exception as err:
return str(err), 500
return json.dumps(greetings), 200


if __name__ == '__main__':
app.run(host="0.0.0.0", port=3000)
10 changes: 10 additions & 0 deletions api/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.11

COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt

EXPOSE 3000

CMD ["python", "api.py"]
7 changes: 7 additions & 0 deletions api/infra/01_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: mpa-backend
spec: {}
status: {}
8 changes: 8 additions & 0 deletions api/infra/02_secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: db-secrets
namespace: mpa-backend
type: Opaque
data:
password:
54 changes: 54 additions & 0 deletions api/infra/03_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: mpa-backend
spec:
replicas: 1
selector:
matchLabels:
name: api
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
name: api
spec:
containers:
- name: api
image: ghcr.io/do3-2023/mpa-kube/api:v0.2
imagePullPolicy: Always
env:
- name: POSTGRES_DB
value: tp
- name: POSTGRES_USER
value: myuser
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-secrets
key: password
- name: POSTGRES_HOST
value: db.mpa-database.svc.cluster.local
ports:
- name: http
containerPort: 3000
protocol: TCP
livenessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 15
timeoutSeconds: 3
periodSeconds: 10
readinessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 30
timeoutSeconds: 3
periodSeconds: 10
14 changes: 14 additions & 0 deletions api/infra/04_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: api
namespace: mpa-backend
spec:
ports:
- name: api
port: 3000
protocol: TCP
targetPort: 3000
selector:
name: api
type: ClusterIP
2 changes: 2 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg2
flask
12 changes: 12 additions & 0 deletions database/data/init.psql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE person (
id INT8 NOT NULL PRIMARY KEY AUTO_INCREMENT,
last_name TEXT NOT NULL,
phone_number TEXT NOT NULL,
location TEXT NOT NULL,
);

INSERT INTO person
(last_name, phone_number, location)
VALUES
('John', '0702030405', 'Marseille'),
('Doe', '0603040506', 'Montpellier');
7 changes: 7 additions & 0 deletions database/infra/01_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: mpa-database
spec: {}
status: {}
8 changes: 8 additions & 0 deletions database/infra/02_secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: db-secrets
namespace: mpa-database
type: Opaque
data:
password:
50 changes: 50 additions & 0 deletions database/infra/03_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
namespace: mpa-database
spec:
replicas: 1
selector:
matchLabels:
name: db
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
name: db
spec:
containers:
- name: db
image: postgres:15
imagePullPolicy: Always
ports:
- name: db
containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_DB
value: tp
- name: POSTGRES_USER
value: myuser
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-secrets
key: password
readinessProbe:
exec:
command: ['psql', '-w', '-U', 'myuser', '-d', 'tp']
initialDelaySeconds: 15
timeoutSeconds: 3
periodSeconds: 10
livenessProbe:
exec:
command: ['psql', '-w', '-U', 'myuser', '-d', 'tp']
initialDelaySeconds: 30
timeoutSeconds: 3
periodSeconds: 10
14 changes: 14 additions & 0 deletions database/infra/04_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: db
namespace: mpa-database
spec:
ports:
- name: db
port: 5432
protocol: TCP
targetPort: 5432
selector:
name: db
type: ClusterIP
19 changes: 19 additions & 0 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM denoland/deno:alpine-1.34.3

LABEL org.opencontainers.image.source="https://github.com/do3-2023/mpa-kube"

WORKDIR /app

USER deno

COPY deps.ts .

RUN deno cache deps.ts

ADD . .

EXPOSE 8080

RUN deno cache main.ts

CMD [ "run", "--allow-net", "--allow-read", "--allow-env", "main.ts" ]
5 changes: 5 additions & 0 deletions web/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"dev": "deno run --allow-net --allow-read --allow-env --watch main.ts"
}
}
5 changes: 5 additions & 0 deletions web/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { serve } from "https://deno.land/[email protected]/http/server.ts";

export { renderFileToString } from "https://deno.land/x/[email protected]/mod.ts";

export { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
11 changes: 11 additions & 0 deletions web/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TP Kubernetes</title>
</head>
<body>
<%= message %>
</body>
</html>
7 changes: 7 additions & 0 deletions web/infra/01_namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: mpa-frontend
spec: {}
status: {}
Empty file added web/infra/02_secrets.yaml
Empty file.
Loading

0 comments on commit 8f2b1ed

Please sign in to comment.