This commit is contained in:
Iban 2023-05-13 19:15:43 +02:00
commit 54450daee7
8 changed files with 204 additions and 0 deletions

47
README.md Normal file
View File

@ -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://<web-service-ip>.
## To clean up the application, perform the following steps:
Execute "./remove-app.sh" make sure it have execution permission.

25
deployment.yaml Normal file
View File

@ -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

4
namespace.yaml Normal file
View File

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: ibanweb

41
prepare-app.sh Executable file
View File

@ -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}'

14
remove-app.sh Executable file
View File

@ -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

8
secret.yaml Normal file
View File

@ -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

12
service.yaml Normal file
View File

@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: ibanweb
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysql

53
statefulset.yaml Normal file
View File

@ -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