![]() |
Laravel on Kubernetes in AWS |
Deploy Laravel App on Kubernetes using AWS (EKS)
1. Prepare Laravel App
- Configure
.env
for production - Run optimization commands:
php artisan config:cache php artisan route:cache
- Use
php-fpm
withnginx
- Database and storage (e.g. S3) should be external
2. Dockerize Laravel
FROM php:8.2-fpm
RUN apt-get update && apt-get install -y \
build-essential libpng-dev libjpeg-dev libonig-dev libxml2-dev zip unzip curl git
RUN docker-php-ext-install pdo pdo_mysql mbstring exif pcntl bcmath gd
WORKDIR /var/www
COPY . .
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN composer install --optimize-autoloader --no-dev
RUN chown -R www-data:www-data /var/www && chmod -R 755 /var/www
EXPOSE 9000
CMD ["php-fpm"]
3. Push Docker Image
docker build -t yourusername/laravel-app .
docker push yourusername/laravel-app
4. Create Kubernetes Cluster (AWS EKS)
eksctl create cluster --name laravel-cluster --region us-west-2 --nodegroup-name standard-workers
5. Kubernetes Manifests
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel-app
spec:
replicas: 2
selector:
matchLabels:
app: laravel
template:
metadata:
labels:
app: laravel
spec:
containers:
- name: laravel
image: yourusername/laravel-app
ports:
- containerPort: 9000
envFrom:
- configMapRef:
name: laravel-config
volumeMounts:
- name: storage
mountPath: /var/www/storage
volumes:
- name: storage
emptyDir: {}
Service
apiVersion: v1
kind: Service
metadata:
name: laravel-service
spec:
selector:
app: laravel
ports:
- protocol: TCP
port: 80
targetPort: 9000
type: LoadBalancer
6. ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: laravel-config
data:
APP_ENV: production
APP_KEY: your-app-key
DB_HOST: your-db-host
DB_DATABASE: your-db
DB_USERNAME: your-user
DB_PASSWORD: your-password
7. Apply Manifests
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
8. Optional Enhancements
- Database: Use AWS RDS
- Storage: Use AWS S3 (Laravel Filesystem)
- HTTPS: Use AWS ACM with Ingress or ALB
- Autoscaling: Use HPA
- Monitoring: AWS CloudWatch, Prometheus, Grafana
Download ANSNEW APP For Ads Free Expriences!
Comments from Facebook