From 00fd8bd450a12116646410f4d09d5eea3d058b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Assun=C3=A7=C3=A3o?= Date: Sat, 13 Apr 2024 15:34:26 -0300 Subject: [PATCH] Improvements --- .env.example | 2 +- .k8s/mysql/deploy.yml | 111 ------------------------------------- .k8s/redis/deploy.yml | 88 ----------------------------- README.md | 3 +- docs/create-aks-cluster.md | 38 +++++++++++-- docs/local-development.md | 31 +++++++++++ 6 files changed, 68 insertions(+), 205 deletions(-) delete mode 100644 .k8s/mysql/deploy.yml delete mode 100644 .k8s/redis/deploy.yml create mode 100644 docs/local-development.md diff --git a/.env.example b/.env.example index b5a8cf2..542d349 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ APP_NAME="Filament K8s Demo" APP_ENV=local -APP_KEY= +APP_KEY=base64:41O0zXX5PDtUAfz2FBXAKG1LRPp9lUzy+kZxTpu3wKo= APP_DEBUG=true APP_URL=http://localhost:8000 diff --git a/.k8s/mysql/deploy.yml b/.k8s/mysql/deploy.yml deleted file mode 100644 index 78d924d..0000000 --- a/.k8s/mysql/deploy.yml +++ /dev/null @@ -1,111 +0,0 @@ -# Create mysql-ns Namespace -apiVersion: v1 -kind: Namespace -metadata: - name: mysql-ns ---- -# Create Resource Quota for mysql-ns Namespace -apiVersion: v1 -kind: ResourceQuota -metadata: - name: mysql-ns-rq -spec: - hard: - requests.cpu: "1000m" - requests.memory: 2Gi - limits.cpu: "2000m" - limits.memory: 4Gi ---- -# Create Storage Class with Azure File to Azure Kubernetes Service -kind: StorageClass -apiVersion: storage.k8s.io/v1 -metadata: - name: mysql-sc-azurefile -provisioner: file.csi.azure.com -allowVolumeExpansion: true -mountOptions: - - file_mode=0777 - - mfsymlinks - - uid=999 - - dir_mode=0777 - - gid=999 - - actimeo=30 - - cache=strict - - nobrl -parameters: - skuName: Standard_LRS ---- -# Create MySQL to Azure Kubernetes Service -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: mysql-pv-claim - namespace: mysql-ns -spec: - storageClassName: mysql-sc-azurefile - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 20Gi ---- -apiVersion: v1 -kind: Secret -metadata: - name: mysql-secret - namespace: mysql-ns -type: Opaque -stringData: - MYSQL_ROOT_PASSWORD: "YourSecureMySQLPassword" #Replace with a proper password ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: mysql - namespace: mysql-ns -spec: - selector: - matchLabels: - app: mysql - strategy: - type: Recreate - template: - metadata: - labels: - app: mysql - spec: - containers: - - image: mysql:8.0 - name: mysql - envFrom: - - secretRef: - name: mysql-secret - resources: - requests: - cpu: 500m - memory: 1Gi - limits: - cpu: 1 - memory: 2Gi - ports: - - containerPort: 3306 - name: mysql - volumeMounts: - - name: mysql-persistent-storage - mountPath: /var/lib/mysql - volumes: - - name: mysql-persistent-storage - persistentVolumeClaim: - claimName: mysql-pv-claim ---- -apiVersion: v1 -kind: Service -metadata: - name: mysql - namespace: mysql-ns -spec: - ports: - - port: 3306 - type: ClusterIP - selector: - app: mysql diff --git a/.k8s/redis/deploy.yml b/.k8s/redis/deploy.yml deleted file mode 100644 index 6091dbd..0000000 --- a/.k8s/redis/deploy.yml +++ /dev/null @@ -1,88 +0,0 @@ -# Create Redis Namespace -apiVersion: v1 -kind: Namespace -metadata: - name: redis-ns ---- -# Create Resource Quota for redis-ns Namespace -apiVersion: v1 -kind: ResourceQuota -metadata: - name: redis-ns-rq -spec: - hard: - requests.cpu: "500m" - requests.memory: 1Gi - limits.cpu: "1000m" - limits.memory: 2Gi ---- -# Create Storage Class with Azure Disk to Azure Kubernetes Service -kind: StorageClass -apiVersion: storage.k8s.io/v1 -metadata: - name: redis-sc-azuredisk -provisioner: disk.csi.azure.com -volumeBindingMode: WaitForFirstConsumer -allowVolumeExpansion: true ---- -# Create Redis to Azure Kubernetes Service -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: redis-pv-claim - namespace: redis-ns -spec: - storageClassName: redis-sc-azuredisk - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 10Gi ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: redis - namespace: redis-ns -spec: - selector: - matchLabels: - app: redis - template: - metadata: - labels: - app: redis - spec: - containers: - - name: redis - image: redis:7.2 - command: ["redis-server", "--appendonly", "yes"] - resources: - requests: - cpu: 250m - memory: 512Mi - limits: - cpu: 500m - memory: 1Gi - ports: - - containerPort: 6379 - name: redis - volumeMounts: - - name: redis-persistent-storage - mountPath: /data - volumes: - - name: redis-persistent-storage - persistentVolumeClaim: - claimName: redis-pv-claim ---- -apiVersion: v1 -kind: Service -metadata: - name: redis - namespace: redis-ns -spec: - ports: - - port: 6379 - type: ClusterIP - selector: - app: redis diff --git a/README.md b/README.md index b55af2d..cc581ab 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ Project demo for my Laravel and Kubernetes workshop with deployment on Azure Kub This project is a simple demo. I chose to use FrankenPHP with an app server, but alternatively, you could use RoadRunner, Swoole, or Open Swoole. In this setup, there is a Docker Compose file that initializes services locally. ## Start here - * [Create AKS Cluster](./docs/create-aks-cluster.md) + * [Local Development](./docs/local-development.md) + * [Create Azure AKS Cluster](./docs/create-aks-cluster.md) ## Included Items diff --git a/docs/create-aks-cluster.md b/docs/create-aks-cluster.md index f245abb..e6c5513 100644 --- a/docs/create-aks-cluster.md +++ b/docs/create-aks-cluster.md @@ -12,13 +12,17 @@ RESOURCE_GROUP=training-rg SUBNET_NAME=training-aks-subnet VNET_NAME=training-aks-vnet AKS_CLUSTER_NAME=training-aks -K8S_VERSION=1.28 +AKS_K8S_VERSION=1.28 ACR_NAME=acrtrainingaks$RANDOM -CONTAINER_TAG=project-training-aks:v1.0.0 CONTAINER_REGISTRY=$ACR_NAME.azurecr.io -APP_NAMESPACE=project-training-ns AKS_TAGS='project=training' +REDIS_NAME_SERVER='trainingredisdemoserver' +MYSQL_NAME_SERVER='trainingmysqldemoserver' +MYSQL_ADMIN_USER='admin-user' +MYSQL_ADMIN_PASSWORD='M&P@Ssw0rd(x)&%321' + + # Log in using Azure CLI az login @@ -56,7 +60,7 @@ az aks create \ --load-balancer-sku standard \ --enable-addons monitoring \ --location $REGION_NAME \ - --kubernetes-version $K8S_VERSION \ + --kubernetes-version $AKS_K8S_VERSION \ --network-plugin azure \ --vnet-subnet-id $SUBNET_ID \ --service-cidr 10.2.0.0/24 \ @@ -95,6 +99,8 @@ az aks update \ Optionally, you can build a Docker image of your service using the az acr command, which will handle the build and push to the Azure Container Registry. Of course, you can also use any container registry. Learn more in the [official documentation](https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/). ```bash +CONTAINER_TAG=project-training-aks:v1.0.0 + az acr build \ --registry $ACR_NAME \ --image $CONTAINER_TAG . @@ -182,3 +188,27 @@ spec: - "domain-or-subdomain-herer.com" # Don't forget to change it to a valid domain or subdomain secretName: letsencrypt-tls ``` + +### Create an Azure Database for MySQL server +If you want to learn more, access the [official documentation](https://learn.microsoft.com/en-us/azure/mysql/single-server/quickstart-create-mysql-server-database-using-azure-cli). + +```bash +az mysql server create \ + --resource-group $RESOURCE_GROUP \ + --name $MYSQL_NAME_SERVER \ + --location $REGION_NAME \ + --admin-user $MYSQL_ADMIN_USER \ + --admin-password $MYSQL_ADMIN_PASSWORD \ + --sku-name GP_Gen5_2 +``` + +### Create an Azure Redis caches +If you want to learn more, access the [official documentation](https://learn.microsoft.com/en-us/cli/azure/redis?view=azure-cli-latest). +```bash +az redis create \ + --location $REGION_NAME \ + --name $REDIS_NAME_SERVER \ + --resource-group $RESOURCE_GROUP \ + --sku Basic \ + --vm-size c0 +``` diff --git a/docs/local-development.md b/docs/local-development.md new file mode 100644 index 0000000..b312cd8 --- /dev/null +++ b/docs/local-development.md @@ -0,0 +1,31 @@ +## Step-by-Step Installation: + +### Step 1: +The first time you start the system, create the `.env` file from the `.env.example` file using the following command: + +```bash +cp .env.example .env +``` + +### Step 2: +Next, start the containers and build the application container using the command: + +```bash +docker-compose up -d --build +``` + +### Step 3: +Check if the `vendor` folder has been created or already exists. If not, install PHP dependencies with the command: + +```bash +docker-compose exec app composer install +``` + +### Step 4: +Wait a few seconds until the MySQL container is ready, and then execute the command to configure the database and perform the initial data load: + +```bash +docker-compose exec app php artisan migrate --seed +``` + +With these steps, the system will be ready to be used for the first time.