Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioassuncao committed Jun 26, 2024
1 parent 00fd8bd commit 89dd90e
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 40 deletions.
8 changes: 0 additions & 8 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

/**
Expand Down
67 changes: 35 additions & 32 deletions docker/entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions k8s/cluster-issuer.yaml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
privateKeySecretRef:
name: letsencrypt-tls
solvers:
- http01:
ingress:
class: nginx
44 changes: 44 additions & 0 deletions k8s/configmap.yaml
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"
MAIL_FROM_NAME: "${APP_NAME}"
170 changes: 170 additions & 0 deletions k8s/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions k8s/ingress.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions k8s/pvc.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions k8s/service.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89dd90e

Please sign in to comment.