commit 54450daee7afa1010343df862453805604fc8e32 Author: Iban Date: Sat May 13 19:15:43 2023 +0200 Final diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a0d159 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# 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: + + 1. Install Kubernetes on your machine. + 2. Install azure-cli: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash + 3. Clone the project repository. + 4. Change into the project directory. + 4. Login in Azure: az login + 4. Execute "./prepare-app.sh" make sure it have execution permission. + 5. Wait for the mysql and web containers to start. + +## To view the application on the web, perform the following steps: + + Copy the IP address at the end of the prepare-app.sh file execution. + In your web browser, navigate to http://. + + +## To clean up the application, perform the following steps: + + Execute "./remove-app.sh" make sure it have execution permission. \ No newline at end of file diff --git a/deployment.yaml b/deployment.yaml new file mode 100644 index 0000000..666bf23 --- /dev/null +++ b/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/namespace.yaml b/namespace.yaml new file mode 100644 index 0000000..a488bb0 --- /dev/null +++ b/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: ibanweb diff --git a/prepare-app.sh b/prepare-app.sh new file mode 100755 index 0000000..13b8555 --- /dev/null +++ b/prepare-app.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +#!/bin/bash + +# Set variables +resourceGroupName="iban-app" +aksClusterName="ibanapp" +nodeCount=1 + +# Create resource group if it does not exist +if [ $(az group exists --name $resourceGroupName) == "false" ] +then + az group create --name $resourceGroupName --location eastus +fi + + +az aks create --resource-group $resourceGroupName --name $aksClusterName --node-count $nodeCount --generate-ssh-keys + + +az aks get-credentials --resource-group $resourceGroupName --name $aksClusterName +kubectl config use-context $aksClusterName + + +# 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 + +# expose phpmyadmin deployment +kubectl expose deployment phpmyadmin --type=LoadBalancer --port=80 --target-port=80 -n ibanweb + +# Get the public IP address of the Kubernetes service +kubectl get service phpmyadmin -n ibanweb -o jsonpath='{.status.loadBalancer.ingress[0].ip}' \ No newline at end of file diff --git a/remove-app.sh b/remove-app.sh new file mode 100755 index 0000000..4096ff5 --- /dev/null +++ b/remove-app.sh @@ -0,0 +1,14 @@ +#!/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 + +# Eliminar recurso AKS +az aks delete --name ibanapp --resource-group iban-app --yes + +# Eliminar recurso de grupo de recursos +az group delete --name iban-app --yes \ No newline at end of file diff --git a/secret.yaml b/secret.yaml new file mode 100644 index 0000000..4b1933d --- /dev/null +++ b/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/service.yaml b/service.yaml new file mode 100644 index 0000000..ec33d1f --- /dev/null +++ b/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/statefulset.yaml b/statefulset.yaml new file mode 100644 index 0000000..c18f5f6 --- /dev/null +++ b/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