From 2646bede01d48cdf1742052726126c6cd051d00c Mon Sep 17 00:00:00 2001 From: Iban Date: Sun, 7 May 2023 21:32:58 +0200 Subject: [PATCH] Assigment 2 finish --- z2/README.md | 45 ++++++++++++++++++++++++++++++++++++++ z2/deployment.yaml | 25 +++++++++++++++++++++ z2/namespace.yaml | 4 ++++ z2/prepare-app.sh | 14 ++++++++++++ z2/secret.yaml | 8 +++++++ z2/service.yaml | 12 ++++++++++ z2/start-app.sh | 7 ++++++ z2/statefulset.yaml | 53 +++++++++++++++++++++++++++++++++++++++++++++ z2/stop-app.sh | 8 +++++++ 9 files changed, 176 insertions(+) create mode 100644 z2/README.md create mode 100644 z2/deployment.yaml create mode 100644 z2/namespace.yaml create mode 100755 z2/prepare-app.sh create mode 100644 z2/secret.yaml create mode 100644 z2/service.yaml create mode 100755 z2/start-app.sh create mode 100644 z2/statefulset.yaml create mode 100755 z2/stop-app.sh diff --git a/z2/README.md b/z2/README.md new file mode 100644 index 0000000..b7f1228 --- /dev/null +++ b/z2/README.md @@ -0,0 +1,45 @@ +# My Web Application + +## Project Description + +This project is a simple web application that provides a user interface to manage a MySQL database. The application consists of a single page that allows the user to perform basic CRUD operations on a list of items stored in the database. +Containers + +## The application consists of the following containers: + + mysql: This container runs the MySQL database server. + web: This container runs the web server and serves the web pages to the user. + + +## The following Kubernetes objects are used in the application: + + PersistentVolume: This object is used to create a named volume for the MySQL data. + StatefulSet: This object is used to manage the mysql container and ensure that there is only one instance running at any given time. + Deployment: This object is used to manage the web container and ensure that there is always at least one instance running. + Service: This object is used to provide access to the mysql and web containers from outside the Kubernetes cluster. + +## Virtual Networks and Named Volumes + +The application uses a virtual network to allow communication between the mysql and web containers. The mysql container stores its data in a named volume called mysql-persistent-storage. + +## Container Configuration + +The mysql container is configured to use the mysql-persistent-storage volume for data storage. The web container is configured to connect to the mysql container using the Kubernetes service mysql. + +## To run the application, perform the following steps: + + Install Kubernetes on your machine. + Clone the project repository. + Change into the project directory. + Execute "./start-app.sh" make sure it have execution permission. + Wait for the mysql and web containers to start: kubectl get pods -n my-app. + +## To view the application on the web, perform the following steps: + + Get the IP address of the web service "kubectl get services -n my-app" or use loclahost. + In your web browser, navigate to http://. + + +## To clean up the application, perform the following steps: + + Execute "./stop-app.sh" make sure it have execution permission. \ No newline at end of file diff --git a/z2/deployment.yaml b/z2/deployment.yaml new file mode 100644 index 0000000..666bf23 --- /dev/null +++ b/z2/deployment.yaml @@ -0,0 +1,25 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: phpmyadmin + namespace: ibanweb +spec: + selector: + matchLabels: + app: phpmyadmin + replicas: 1 + template: + metadata: + labels: + app: phpmyadmin + spec: + containers: + - name: phpmyadmin + image: phpmyadmin/phpmyadmin:latest + env: + - name: PMA_HOST + value: mysql + ports: + - containerPort: 80 + name: http + diff --git a/z2/namespace.yaml b/z2/namespace.yaml new file mode 100644 index 0000000..a488bb0 --- /dev/null +++ b/z2/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: ibanweb diff --git a/z2/prepare-app.sh b/z2/prepare-app.sh new file mode 100755 index 0000000..5df616a --- /dev/null +++ b/z2/prepare-app.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# create namespace +kubectl apply -f namespace.yaml + +# create mysql secrets +kubectl create secret generic mysql-secret --from-literal=password=dromedario -n ibanweb + +# create mysql statefulset and service +kubectl apply -f statefulset.yaml +kubectl apply -f service.yaml + +# create phpmyadmin deployment +kubectl apply -f deployment.yaml \ No newline at end of file diff --git a/z2/secret.yaml b/z2/secret.yaml new file mode 100644 index 0000000..4b1933d --- /dev/null +++ b/z2/secret.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: mysql-secret + namespace: ibanweb +type: Opaque +data: + password: YWxtZW5kcmE= #la contraseƱa es "almendra" en base64 diff --git a/z2/service.yaml b/z2/service.yaml new file mode 100644 index 0000000..ec33d1f --- /dev/null +++ b/z2/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: mysql + namespace: ibanweb +spec: + ports: + - name: mysql + port: 3306 + selector: + app: mysql + diff --git a/z2/start-app.sh b/z2/start-app.sh new file mode 100755 index 0000000..60f5526 --- /dev/null +++ b/z2/start-app.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# prepare app +./prepare-app.sh + +# expose phpmyadmin deployment +kubectl expose deployment phpmyadmin --type=LoadBalancer --port=80 --target-port=80 -n ibanweb \ No newline at end of file diff --git a/z2/statefulset.yaml b/z2/statefulset.yaml new file mode 100644 index 0000000..c18f5f6 --- /dev/null +++ b/z2/statefulset.yaml @@ -0,0 +1,53 @@ +apiVersion: v1 +kind: PersistentVolume +metadata: + name: my-pv +spec: + capacity: + storage: 1Gi + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + storageClassName: slow + hostPath: + path: /data/my-pv/ +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mysql + namespace: ibanweb +spec: + selector: + matchLabels: + app: mysql + serviceName: mysql + replicas: 1 + template: + metadata: + labels: + app: mysql + spec: + containers: + - name: mysql + image: mysql:latest + env: + - name: MYSQL_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: mysql-secret + key: password + ports: + - containerPort: 3306 + name: mysql + volumeMounts: + - name: mysql-persistent-storage + mountPath: /var/lib/mysql + volumeClaimTemplates: + - metadata: + name: mysql-persistent-storage + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi \ No newline at end of file diff --git a/z2/stop-app.sh b/z2/stop-app.sh new file mode 100755 index 0000000..6e1b2d6 --- /dev/null +++ b/z2/stop-app.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# delete app objects +kubectl delete -f deployment.yaml +kubectl delete -f service.yaml +kubectl delete -f statefulset.yaml +kubectl delete secret mysql-secret -n ibanweb +kubectl delete namespace ibanweb \ No newline at end of file