Step-by-Step Guide: Laravel on Kubernetes in AWS - ANSNEW

Step-by-Step Guide: Laravel on Kubernetes in AWS

Laravel on Kubernetes in AWS
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 with nginx
  • 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!
Yamin Hossain Shohan
Researcher, DevOps Engineer and Digital Content Creator

I’m a researcher, programmer, and content creator, combining tech and creativity to craft engaging stories and digital content.

Copyright Disclaimer

All the information is published in good faith and for general information purpose only. We does not make any warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on ansnew.com is strictly at your own risk. We will not be liable for any losses and/or damages in connection with the use of our website. Please read our complete disclaimer. And we do not hold any copyright over the article multimedia materials. All credit goes to the respective owner/creator of the pictures, audios and videos. We also accept no liability for any links to other URLs which appear on our website. If you are a copyright owner or an agent thereof, and you believe that any material available on our services infringes your copyrights, then you may submit a written copyright infringement notification using the contact details

(0) Comments on "Step-by-Step Guide: Laravel on Kubernetes in AWS"

* Most comments will be posted if that are on-topic and not abusive
Back To Top