From 89dd90e255837e24f6fff7e909b46b2a79f2b944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Assun=C3=A7=C3=A3o?= Date: Wed, 26 Jun 2024 13:53:28 -0300 Subject: [PATCH] Improvements --- app/Providers/AppServiceProvider.php | 8 -- docker/entrypoint.sh | 67 ++++++----- k8s/cluster-issuer.yaml | 15 +++ k8s/configmap.yaml | 44 +++++++ k8s/deployment.yaml | 170 +++++++++++++++++++++++++++ k8s/ingress.yaml | 25 ++++ k8s/pvc.yaml | 12 ++ k8s/service.yaml | 15 +++ 8 files changed, 316 insertions(+), 40 deletions(-) mode change 100644 => 100755 docker/entrypoint.sh create mode 100644 k8s/cluster-issuer.yaml create mode 100644 k8s/configmap.yaml create mode 100644 k8s/deployment.yaml create mode 100644 k8s/ingress.yaml create mode 100644 k8s/pvc.yaml create mode 100644 k8s/service.yaml diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index af29594..8c534b5 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -19,14 +19,6 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // https://github.com/laravel/framework/issues/33238#issuecomment-897063577 - Event::listen(MigrationsStarted::class, function () { - DB::statement('SET SESSION sql_require_primary_key=0'); - }); - - Event::listen(MigrationsEnded::class, function () { - DB::statement('SET SESSION sql_require_primary_key=1'); - }); } /** diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh old mode 100644 new mode 100755 index e49f4a9..a04a6b2 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -17,37 +17,40 @@ run_setup_tasks() { php artisan migrate --force || true } -if [ -d "/var/www/html/vendor" ] ; then - # Run setup tasks if in automatic mode - if [ "$CONTAINER_MODE" = "automatic" ]; then - run_setup_tasks - fi +# Check if vendor directory exists +while [ ! -d "/var/www/html/vendor" ]; do + echo "WARNING: The directory /var/www/html/vendor does not exist yet. Please run the \"composer install\" command to ensure that all necessary dependencies are properly installed." + echo "Retrying in 180 seconds..." + sleep 180s +done - # Execute role-specific commands - case "$CONTAINER_ROLE" in - app) - echo "INFO: Running octane..." - exec $ARTISAN octane:start --server=frankenphp --host=0.0.0.0 --port=8000 - ;; - worker) - echo "INFO: Running the queue..." - exec $ARTISAN queue:work -vv --no-interaction --tries=3 --sleep=5 --timeout=300 --delay=10 - ;; - horizon) - echo "INFO: Running the horizon..." - exec $ARTISAN horizon - ;; - scheduler) - while true; do - echo "INFO: Running scheduled tasks." && exec $ARTISAN schedule:run --verbose --no-interaction & - sleep 60s - done - ;; - *) - echo "Could not match the container role \"$CONTAINER_ROLE\"" - exit 1 - ;; - esac -else - echo "WARNING: The directory /var/www/html/vendor does not exist yet. Please run the composer install command to ensure that all necessary dependencies are properly installed." +# Run setup tasks if in automatic mode +if [ "$CONTAINER_MODE" = "automatic" ]; then + run_setup_tasks fi + +# Execute role-specific commands +case "$CONTAINER_ROLE" in + app) + echo "INFO: Running octane..." + exec $ARTISAN octane:start --host=0.0.0.0 --port=8000 + ;; + worker) + echo "INFO: Running the queue..." + exec $ARTISAN queue:work -vv --no-interaction --tries=3 --sleep=5 --timeout=300 --delay=10 + ;; + horizon) + echo "INFO: Running the horizon..." + exec $ARTISAN horizon + ;; + scheduler) + while true; do + echo "INFO: Running scheduled tasks." && exec $ARTISAN schedule:run --verbose --no-interaction & + sleep 60s + done + ;; + *) + echo "Could not match the container role \"$CONTAINER_ROLE\"" + exit 1 + ;; +esac diff --git a/k8s/cluster-issuer.yaml b/k8s/cluster-issuer.yaml new file mode 100644 index 0000000..7d449f6 --- /dev/null +++ b/k8s/cluster-issuer.yaml @@ -0,0 +1,15 @@ +apiVersion: cert-manager.io/v1 +kind: ClusterIssuer +metadata: + name: letsencrypt + namespace: cert-manager +spec: + acme: + server: https://acme-v02.api.letsencrypt.org/directory + email: devs@codions.com + privateKeySecretRef: + name: letsencrypt-tls + solvers: + - http01: + ingress: + class: nginx diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml new file mode 100644 index 0000000..5e3a729 --- /dev/null +++ b/k8s/configmap.yaml @@ -0,0 +1,44 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: projectname-config + namespace: projectname +data: + APP_NAME: "Filament K8s Demo" + APP_ENV: local + APP_KEY: base64:41O0zXX5PDtUAfz2FBXAKG1LRPp9lUzy+kZxTpu3wKo= + APP_DEBUG: "true" + APP_URL: http://localhost:8000 + + LOG_CHANNEL: stdout + LOG_DEPRECATIONS_CHANNEL: "null" + LOG_LEVEL: debug + + DB_CONNECTION: mysql + DB_HOST: mysql + DB_PORT: "3306" + DB_DATABASE: workshop_k8s + DB_USERNAME: workshop_k8s + DB_PASSWORD: secret + + BROADCAST_DRIVER: log + CACHE_DRIVER: redis + FILESYSTEM_DISK: local + QUEUE_CONNECTION: redis + SESSION_DRIVER: redis + SESSION_LIFETIME: "120" + + MEMCACHED_HOST: "127.0.0.1" + + REDIS_HOST: redis + REDIS_PASSWORD: secret + REDIS_PORT: "6379" + + MAIL_MAILER: smtp + MAIL_HOST: mailpit + MAIL_PORT: "1025" + MAIL_USERNAME: "null" + MAIL_PASSWORD: "null" + MAIL_ENCRYPTION: "null" + MAIL_FROM_ADDRESS: "hello@example.com" + MAIL_FROM_NAME: "${APP_NAME}" diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..04e166e --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,170 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: projectname + name: projectname + namespace: projectname +spec: + minReadySeconds: 5 + replicas: 3 + revisionHistoryLimit: 1 + selector: + matchLabels: + app: projectname +# strategy: +# rollingUpdate: +# maxSurge: 1 +# maxUnavailable: 50% +# type: RollingUpdate + template: + metadata: + labels: + app: projectname + spec: + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchExpressions: + - key: app + operator: In + values: + - projectname + topologyKey: kubernetes.io/hostname + weight: 100 + initContainers: + - args: + - /bin/bash + - -c + - (php /var/www/html/artisan config:cache || true) && (php /var/www/html/artisan view:clear || true) && (php /var/www/html/artisan migrate --force || true) + envFrom: + - configMapRef: + name: projectname-config + env: + - name: ENABLE_PHPFPM + value: "false" + - name: ENABLE_NGINX + value: "false" + image: ghcr.io/lighttecnologia/cartoriosmaranhao-new/central:develop + imagePullPolicy: Always + name: artisan + volumeMounts: + - mountPath: /var/www/storage + name: vultr-volume + + containers: + - name: app + envFrom: + - configMapRef: + name: projectname-config + image: ghcr.io/lighttecnologia/cartoriosmaranhao-new/central:develop + imagePullPolicy: Always + ports: + - containerPort: 80 + livenessProbe: + initialDelaySeconds: 10 + periodSeconds: 15 + tcpSocket: + port: 80 + timeoutSeconds: 30 + readinessProbe: + initialDelaySeconds: 10 + periodSeconds: 10 + tcpSocket: + port: 80 + resources: {} + volumeMounts: + - mountPath: /var/www/storage + name: vultr-volume + + imagePullSecrets: + - name: dockerconfigjson-github-com + + volumes: + - name: vultr-volume + persistentVolumeClaim: + claimName: projectname-pvc + +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: projectname-worker + name: projectname-worker + namespace: projectname +spec: + replicas: 1 + revisionHistoryLimit: 1 + selector: + matchLabels: + app: projectname-worker + template: + metadata: + labels: + app: projectname-worker + spec: + containers: + - name: projectname-worker + image: ghcr.io/lighttecnologia/cartoriosmaranhao-new/central:develop + imagePullPolicy: Always + command: + - php + args: + - artisan + - queue:work + - --queue=default + - --max-jobs=200 + envFrom: + - configMapRef: + name: projectname-config + env: + - name: ENABLE_PHPFPM + value: "false" + - name: ENABLE_NGINX + value: "false" + resources: {} + +--- + +apiVersion: batch/v1 +kind: CronJob +metadata: + name: projectname-cron + namespace: projectname +spec: + concurrencyPolicy: Replace + failedJobsHistoryLimit: 1 + jobTemplate: + spec: + template: + metadata: + labels: + app: cron + spec: + containers: + - args: + - /bin/bash + - -c + - php artisan schedule:run + envFrom: + - configMapRef: + name: projectname-config + env: + - name: ENABLE_PHPFPM + value: "false" + - name: ENABLE_NGINX + value: "false" + image: ghcr.io/lighttecnologia/cartoriosmaranhao-new/central:develop + imagePullPolicy: Always + name: artisan-schedule + resources: {} + restartPolicy: Never + imagePullSecrets: + - name: dockerconfigjson-github-com + schedule: "* * * * *" + startingDeadlineSeconds: 30 + successfulJobsHistoryLimit: 1 diff --git a/k8s/ingress.yaml b/k8s/ingress.yaml new file mode 100644 index 0000000..a84aefc --- /dev/null +++ b/k8s/ingress.yaml @@ -0,0 +1,25 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: projectname-ingress + namespace: projectname + annotations: + cert-manager.io/cluster-issuer: "letsencrypt" + ingress.kubernetes.io/force-ssl-redirect: "true" +spec: + ingressClassName: nginx + rules: + - host: "projectname.example.com" + http: + paths: + - pathType: Prefix + path: "/" + backend: + service: + name: projectname-svc + port: + number: 80 + tls: + - hosts: + - "projectname.example.com" + secretName: letsencrypt-tls diff --git a/k8s/pvc.yaml b/k8s/pvc.yaml new file mode 100644 index 0000000..40c6b14 --- /dev/null +++ b/k8s/pvc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: projectname-pvc + namespace: projectname +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 40Gi + storageClassName: vultr-block-storage-hdd diff --git a/k8s/service.yaml b/k8s/service.yaml new file mode 100644 index 0000000..fd5e0d4 --- /dev/null +++ b/k8s/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: projectname + name: projectname-svc + namespace: projectname +spec: + selector: + app: projectname + type: ClusterIP + ports: + - protocol: TCP + port: 80 + targetPort: 80