-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial commit - commit old project
Signed-off-by: Muriel Paraire <[email protected]>
- Loading branch information
0 parents
commit 8f2b1ed
Showing
22 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
psycopg2 | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.