From 82401ec37f5c1ad98294b1a2b7e0bc07707477b5 Mon Sep 17 00:00:00 2001 From: Saez Arana Date: Thu, 25 Apr 2024 16:35:23 +0000 Subject: [PATCH] Subir archivos a 'z2' assigment 2 --- z2/Dockerfile | 4 ++ z2/README.md | 46 ++++++++++++++++++++++ z2/create.php | 18 +++++++++ z2/database.php | 9 +++++ z2/delete.php | 20 ++++++++++ z2/edit.php | 21 ++++++++++ z2/index.php | 74 ++++++++++++++++++++++++++++++++++++ z2/info.txt | 49 ++++++++++++++++++++++++ z2/mysql-service.yaml | 11 ++++++ z2/mysql-statefulset.yaml | 39 +++++++++++++++++++ z2/php-deployment.yaml | 19 +++++++++ z2/php-service.yaml | 12 ++++++ z2/prepare-app.sh | 4 ++ z2/prepare-sql.sh | 2 + z2/script.js | Bin 0 -> 1024 bytes z2/script.sql | 7 ++++ z2/secret-sql-password.yaml | 7 ++++ z2/start-app.sh | 6 +++ z2/stop-app.sh | 1 + z2/styles.css | 49 ++++++++++++++++++++++++ 20 files changed, 398 insertions(+) create mode 100644 z2/Dockerfile create mode 100644 z2/README.md create mode 100644 z2/create.php create mode 100644 z2/database.php create mode 100644 z2/delete.php create mode 100644 z2/edit.php create mode 100644 z2/index.php create mode 100644 z2/info.txt create mode 100644 z2/mysql-service.yaml create mode 100644 z2/mysql-statefulset.yaml create mode 100644 z2/php-deployment.yaml create mode 100644 z2/php-service.yaml create mode 100644 z2/prepare-app.sh create mode 100644 z2/prepare-sql.sh create mode 100644 z2/script.js create mode 100644 z2/script.sql create mode 100644 z2/secret-sql-password.yaml create mode 100644 z2/start-app.sh create mode 100644 z2/stop-app.sh create mode 100644 z2/styles.css diff --git a/z2/Dockerfile b/z2/Dockerfile new file mode 100644 index 0000000..ec9759a --- /dev/null +++ b/z2/Dockerfile @@ -0,0 +1,4 @@ +FROM mysql:latest + +# Copia el script SQL a la ubicación dentro del contenedor +COPY script.sql /docker-entrypoint-initdb.d/ \ No newline at end of file diff --git a/z2/README.md b/z2/README.md new file mode 100644 index 0000000..70fa2f6 --- /dev/null +++ b/z2/README.md @@ -0,0 +1,46 @@ +# Notes Manager Application + +This is a simple notes management application developed with php and sql and deployed in kubernetes that allows you to create, read, update, and delete notes easily and efficiently. + +## Key features + +- Create Notes: You can create new notes by specifying a title and content. +- List Notes: View all existing notes in the application. +- Update Notes: Modify the title or content of an existing note. +- Delete Notes: Remove notes that are no longer needed. + +## Technologies Used +- Backend: php container +- Frontend: HTML,CSS +- Database: mysql container e +- Containers: Docker +- Container Orchestrator: Kubernetes + +## Docker images and kubernetes object +- Images: + - php from -> rafasaez/php-sql-app image + - sql from -> rafasaez/sql-app images + +- kubernetes yaml(Inside kubernetes directory): + - mysql-service.yaml -> for the service of mysql + - php-service.yaml -> for the service of php + - mysqk-statefulset.yaml -> for the statefulset of mysql + - php-deployment.yaml -> for the deployment of php + - secret-sql-password -> for the secret object where mysql password is storage +## How to run de application +- Firs you need to install kubernetes on docker +- Go to docker desktop, go to settings, kubernetes and click on enable kubernetes +- Open your terminal +- Run the following + ```bash + bash prepare-app.sh + ``` +- Once is evry image created run the application + ```bash + bash start-app.sh + ``` +- Your application should be running on port 8080 in localhost +- If you want to finish the app just run: + ```bash + bash stop-app.sh + ``` diff --git a/z2/create.php b/z2/create.php new file mode 100644 index 0000000..c21016a --- /dev/null +++ b/z2/create.php @@ -0,0 +1,18 @@ +connect_errno) { + // Print the connection error message + echo "Failed to connect to MySQL: " . mysqli_connect_error(); + // You can also log the error message to a file or other logging mechanism + // error_log("Failed to connect to MySQL: " . mysqli_connect_error()); + exit(); // Exit the script if there's a connection error +} +if($_POST){ + $note = mysqli_real_escape_string($db,$_POST['note']);; + $query = "insert into nota (noteContent) values ('$note')"; + mysqli_query($db,$query); +} +mysqli_close($db); +header("Location:../index.php"); \ No newline at end of file diff --git a/z2/database.php b/z2/database.php new file mode 100644 index 0000000..492d3df --- /dev/null +++ b/z2/database.php @@ -0,0 +1,9 @@ +set_charset('utf8') + return $db; +} \ No newline at end of file diff --git a/z2/delete.php b/z2/delete.php new file mode 100644 index 0000000..a3575a7 --- /dev/null +++ b/z2/delete.php @@ -0,0 +1,20 @@ +connect_errno) { + // Print the connection error message + echo "Failed to connect to MySQL: " . mysqli_connect_error(); + // You can also log the error message to a file or other logging mechanism + // error_log("Failed to connect to MySQL: " . mysqli_connect_error()); + exit(); // Exit the script if there's a connection error +} + + +if($_POST){ + $noteID = $_POST['id'];; + $query = "delete from nota where id = $noteID"; + mysqli_query($db,$query); +} +mysqli_close($db); +header("Location:../index.php"); \ No newline at end of file diff --git a/z2/edit.php b/z2/edit.php new file mode 100644 index 0000000..7cb9be8 --- /dev/null +++ b/z2/edit.php @@ -0,0 +1,21 @@ +connect_errno) { + // Print the connection error message + echo "Failed to connect to MySQL: " . mysqli_connect_error(); + // You can also log the error message to a file or other logging mechanism + // error_log("Failed to connect to MySQL: " . mysqli_connect_error()); + exit(); // Exit the script if there's a connection error +} + + +if($_POST){ + $noteID = $_POST['id'];; + $note = mysqli_real_escape_string($db,$_POST['note']);; + $query = "UPDATE nota SET noteContent = '$note' where id = $noteID"; + mysqli_query($db,$query); +} +mysqli_close($db); +header("Location:../index.php"); \ No newline at end of file diff --git a/z2/index.php b/z2/index.php new file mode 100644 index 0000000..2b856bd --- /dev/null +++ b/z2/index.php @@ -0,0 +1,74 @@ +connect_errno) { + // Print the connection error message + echo "Failed to connect to MySQL: " . mysqli_connect_error(); + // You can also log the error message to a file or other logging mechanism + // error_log("Failed to connect to MySQL: " . mysqli_connect_error()); + exit(); // Exit the script if there's a connection error +} + +$getNotes = "select * from nota" ; +$notes = mysqli_query($db,$getNotes); +if(!$notes){ + echo "Error: " . mysqli_error($db); +} +mysqli_close($db); +?> + + + + + + + + Document + + +
+ +

Notes manager

+
+ + + +
+
+ +
+
+ + + + +
+ +
+ + +
+ +
+ + +
+ + +
+ + \ No newline at end of file diff --git a/z2/info.txt b/z2/info.txt new file mode 100644 index 0000000..403802d --- /dev/null +++ b/z2/info.txt @@ -0,0 +1,49 @@ +#Para que podamos coger la imagen creada de docker necesitamos subirla al repositorio +#Para ello creamos la imagen como +docker build -t rafasaez/nombre-repositorio +#luego mandamos la imagen a docker hub +docker push rafasaez/nombre-repositorio + +#objecto secreto +apiVersion: v1 +kind: Secret +metadata: + name: mysql-secret +type: Opaque +data: + #En base64 ponemos nuestra contraseña encriptada + password: base64_encoded_passwordecho + +#Para encriptar se encripta en base64 por default al usar secret +echo -n 'your_password' | base64 + + +#para iteractuar con la base de datos +kubectl exec -i mysql-0 -n z2 -- mysql -h mysql-service.z2.svc.cluster.local -P80 -u root -p password < sql/script.sql + + +#problemas +#Cuando añado la informacion de los volumenes de la configuracion inicial no funciona el state ful set +#en volumemounts + - name: init-scripts + mountPath: /docker-entrypoint-initdb.d + volumeClaimTemplates: + - metadata: + name: init-scripts + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi + + +#Funciona la app finalmente obte por meter la config de la base de datos directamente en el docker file para crear una nueva imagen de sql con la base configurada +#Volver a hacer lo de la ssh key + +#Conectar a la base de datos +#kubectl exec -it -n z2 mysql-0 -- mysql -u root -p notas +#Solo me deja entrar a mysql cuando ejecuto el comando de arriba +#Cuando hago delete namespace no se me guarda la informacion que tengo en la base de datos + +#cambiar a retain +kubectl patch pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}' \ No newline at end of file diff --git a/z2/mysql-service.yaml b/z2/mysql-service.yaml new file mode 100644 index 0000000..6cafef4 --- /dev/null +++ b/z2/mysql-service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: mysql-service +spec: + selector: + app: mysql + ports: + - protocol: TCP + port: 3306 + targetPort: 3306 diff --git a/z2/mysql-statefulset.yaml b/z2/mysql-statefulset.yaml new file mode 100644 index 0000000..cbd118c --- /dev/null +++ b/z2/mysql-statefulset.yaml @@ -0,0 +1,39 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mysql +spec: + serviceName: mysql-service + replicas: 1 + selector: + matchLabels: + app: mysql + template: + metadata: + labels: + app: mysql + spec: + containers: + - name: mysql + image: rafasaez/sql-app + ports: + - containerPort: 3306 + env: + - name: MYSQL_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: mysql-secret + key: password + volumeMounts: + - name: mysql-persistent-storage + mountPath: /var/lib/mysql + #Crea los pv necerarios + volumeClaimTemplates: + - metadata: + name: mysql-persistent-storage + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi + \ No newline at end of file diff --git a/z2/php-deployment.yaml b/z2/php-deployment.yaml new file mode 100644 index 0000000..62c2ce1 --- /dev/null +++ b/z2/php-deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: php +spec: + replicas: 1 + selector: + matchLabels: + app: php + template: + metadata: + labels: + app: php + spec: + containers: + - name: php + image: rafasaez/php-sql-app:latest + ports: + - containerPort: 80 \ No newline at end of file diff --git a/z2/php-service.yaml b/z2/php-service.yaml new file mode 100644 index 0000000..b1f2b16 --- /dev/null +++ b/z2/php-service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: php-service +spec: + selector: + app: php + ports: + - protocol: TCP + port: 8080 + targetPort: 80 + type: LoadBalancer \ No newline at end of file diff --git a/z2/prepare-app.sh b/z2/prepare-app.sh new file mode 100644 index 0000000..ea8a3c1 --- /dev/null +++ b/z2/prepare-app.sh @@ -0,0 +1,4 @@ +docker build -t rafasaez/php-sql-app . +docker push rafasaez/php-sql-app +cd sql +bash prepare-sql.sh diff --git a/z2/prepare-sql.sh b/z2/prepare-sql.sh new file mode 100644 index 0000000..8d0afeb --- /dev/null +++ b/z2/prepare-sql.sh @@ -0,0 +1,2 @@ +docker build -t rafasaez/sql-app . +docker push rafasaez/sql-app diff --git a/z2/script.js b/z2/script.js new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/z2/script.sql b/z2/script.sql new file mode 100644 index 0000000..8c03acf --- /dev/null +++ b/z2/script.sql @@ -0,0 +1,7 @@ +create database notas; +use notas; + +CREATE TABLE nota ( + id INT AUTO_INCREMENT PRIMARY KEY, + noteContent VARCHAR(60) +); \ No newline at end of file diff --git a/z2/secret-sql-password.yaml b/z2/secret-sql-password.yaml new file mode 100644 index 0000000..9e34cf3 --- /dev/null +++ b/z2/secret-sql-password.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: mysql-secret +type: Opaque +data: + password: cGFzc3dvcmQ= \ No newline at end of file diff --git a/z2/start-app.sh b/z2/start-app.sh new file mode 100644 index 0000000..56fde80 --- /dev/null +++ b/z2/start-app.sh @@ -0,0 +1,6 @@ +kubectl create namespace z2 +kubectl apply -f kubernetes/secret-sql-password.yaml -n z2 +kubectl apply -f kubernetes/mysql-statefulset.yaml -n z2 +kubectl apply -f kubernetes/mysql-service.yaml -n z2bas +kubectl apply -f kubernetes/php-deployment.yaml -n z2 +kubectl apply -f kubernetes/php-service.yaml -n z2 diff --git a/z2/stop-app.sh b/z2/stop-app.sh new file mode 100644 index 0000000..83617d8 --- /dev/null +++ b/z2/stop-app.sh @@ -0,0 +1 @@ +kubectl delete namespace z2 \ No newline at end of file diff --git a/z2/styles.css b/z2/styles.css new file mode 100644 index 0000000..3886c9d --- /dev/null +++ b/z2/styles.css @@ -0,0 +1,49 @@ +/* Utilities*/ +.button{ + display: inl; + color:white; + text-decoration: none; + font-weight: bold; + text-align: center; + padding: 1rem; + border-radius: 2rem; + text-transform: uppercase; + margin: 1rem 1rem; + border: none; +} +.greenButton{ + background-color: green; +} +.redButton{ + background-color: red; + width: -moz-available; +} +input[type="text"]{ + padding: 1rem; + border-radius: 2rem +} + +.contenedor{ + display: flex; + flex-direction: column; + align-items: center; +} +.note{ + background-color: bisque; + display: grid; + grid-template-columns: repeat(2,1fr); + align-items: center; + width: min-content; + padding:1rem; + border-radius: 2rem; + margin-top: 1rem; +} +.addNote{ + width: 20rem; +} +.editForm{ + display:none; +} +.contenido-and-edit{ + display: flex; +} \ No newline at end of file