Deploy a Laravel Application on AWS EKS: A Step-By-Step Guide

laravel kubernetes, laravel eks

Let’s walk through how to run a Laravel project on AWS Elastic Kubernetes Service (EKS).

AWS EKS makes working with Kubernetes easier by handling all the heavy lifting—setup, operation, and maintenance—so you can focus on what really matters: building great applications. It’s reliable too, with multi-AZ (availability zone) deployments that keep everything running smoothly, even when traffic spikes.

Plus, EKS plays nicely with other AWS services like RDS, S3, and IAM, which makes managing your app’s security and storage a breeze. Another big win? EKS handles updates and patches for you automatically. That means less downtime, fewer headaches, and always running the latest, safest version of Kubernetes. It’s a great option for teams looking to scale with confidence.

aws eks, laravel

Prerequisites

Before we get started, here are the prerequisites:

  1. Basic understanding of AWS services.
  2. Basic understanding of Laravel, PHP’s framework.
  3. Basic understanding of Docker and Kubernetes.
  4. AWS Account with necessary permissions.
  5. AWS CLI and kubectl installed on your system.
  6. Docker installed on your system.
  7. An existing Laravel project.

Step 1: Dockerize Your Laravel Project

The first step is to create a Dockerfile at the root of your Laravel project. The Dockerfile specifies the Docker image and includes all the necessary dependencies to run your Laravel application. Below is a simple example:

FROM php:7.4-fpm

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd

# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

COPY . /var/www

# Install dependencies
RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8080

EXPOSE 8080

Step 2: Build and Push the Docker Image to AWS ECR

Next, we need to build a Docker image from the Dockerfile and push it to AWS Elastic Container Registry (ECR).

See also  Laravel - How to send mail using Amazon SES

First, create a repository on ECR:

aws ecr create-repository --repository-name laravel-app --region <region>

Build the Docker image:

docker build -t laravel-app .

Authenticate Docker to the Amazon ECR:

aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com

Tag the Docker image:

docker tag laravel-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/laravel-app:latest

Push the Docker image:

docker push <account-id>.dkr.ecr.<region>.amazonaws.com/laravel-app:latest

Step 3: Set Up AWS EKS

Now, we will create a Kubernetes cluster using AWS EKS. For this, you need to create a VPC (or you can use an existing one), and then create an EKS cluster.

First, set up the VPC, subnets, and security groups with the necessary IAM roles. You can use AWS Management Console, AWS CLI, or CloudFormation for this.

Next, use the AWS Management Console to create an EKS cluster:

  1. Open the Amazon EKS console at https://console.aws.amazon.com/eks/.
  2. Choose Create cluster.
  3. For Name, enter a unique name for your cluster.
  4. For Kubernetes version, choose the version you want to use.
  1. For Cluster service role, choose the IAM role that allows the Amazon EKS service to manage other AWS services on your behalf.
  2. For VPC, select the VPC you created for this cluster.
  3. Choose Create.

After creating the cluster, you’ll need to set up the kubectl command-line tool with the necessary permissions. This can be done by updating your kubeconfig file with the information about your cluster:

aws eks --region <region> update-kubeconfig --name <cluster_name>

Step 4: Create a Deployment and Service in EKS

After setting up the EKS cluster, we will create a deployment that runs our Docker container. Create a deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: laravel-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: laravel-app
  template:
    metadata:
      labels:
        app: laravel-app
    spec:
      containers:
      - name: laravel-app
        image: <account-id>.dkr.ecr.<region>.amazonaws.com/laravel-app:latest
        ports:
        - containerPort: 8080

This deployment creates three replicas of our Docker container. Now, apply the deployment:

kubectl apply -f deployment.yaml

Next, create a service to expose our application to the internet. Create a service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: laravel-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: laravel-app

This service creates a LoadBalancer that listens to HTTP traffic (port 80) and forwards it to our application (port 8080). Now, apply the service:

kubectl apply -f service.yaml

Step 5: Access Your Laravel Application

After creating the service, it might take a few minutes for AWS to set up the LoadBalancer. Once done, you can get the URL of your application with:

kubectl get service laravel-service

Look for the “EXTERNAL-IP” in the output. This is the URL of your Laravel application.

See also  Using the Laravel Seeder: Tips, Tricks, and Best Practices

Now you can access your Laravel application from any web browser using the provided URL.

Congratulations! You’ve successfully deployed your Laravel application on AWS EKS!

Conclusion

We’ve walked through how to Dockerize a Laravel app, create and push a Docker image to AWS ECR, set up AWS EKS, and deploy the application using Kubernetes. Finally, we accessed the Laravel app to confirm everything worked.

AWS EKS provides a scalable, reliable environment for running Laravel in production. If you’re looking for a solution that simplifies Kubernetes management while offering high performance and seamless AWS integrations, it’s a solid choice for your next deployment.

Leave a Comment