Deploying Laravel Applications with Docker and Kubernetes


Deploying Laravel applications using Docker and Kubernetes is a modern and scalable approach to managing and running your web applications. In this guide, we will explore the steps to containerize a Laravel application with Docker and then deploy it to a Kubernetes cluster.


Step 1: Containerizing Your Laravel Application with Docker


Containerization allows you to package your Laravel application and its dependencies into a Docker image. Create a

Dockerfile
in your Laravel project's root directory to define how your application should be built into an image.


# Dockerfile
# Use an official PHP runtime as a parent image
FROM php:7.4-fpm
# Set the working directory in the container
WORKDIR /var/www/html
# Copy the application code into the container
COPY . .
# Install PHP extensions and dependencies
RUN apt-get update && apt-get install -y \
libzip-dev \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip pdo pdo_mysql
# Expose port 9000 to communicate with PHP-FPM
EXPOSE 9000
# Start PHP-FPM
CMD ["php-fpm"]

After creating the Dockerfile, build the Docker image:


docker build -t laravel-app .

Step 2: Create a Docker Compose Configuration (Optional)


If your Laravel application requires additional services like a database or a cache server, you can define them in a

docker-compose.yml
file. Docker Compose allows you to manage multiple containers as a single service.


version: '3'
services:
web:
build: .
ports:
- "80:80"
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel_db

Use

docker-compose up
to start the services defined in the
docker-compose.yml
file.


Step 3: Deploying to Kubernetes


Deploying your Laravel application to Kubernetes involves creating Kubernetes manifests (YAML files) to define your application's pods, services, and other resources. Here's an example of a Kubernetes Deployment and Service for a Laravel application:


# laravel-app-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel-app
spec:
replicas: 3
selector:
matchLabels:
app: laravel-app
template:
metadata:
labels:
app: laravel-app
spec:
containers:
- name: laravel-app
image: laravel-app:latest
ports:
- containerPort: 9000
# laravel-app-service.yml
apiVersion: v1
kind: Service
metadata:
name: laravel-app
spec:
selector:
app: laravel-app
ports:
- protocol: TCP
port: 80
targetPort: 9000
type: LoadBalancer

Apply these manifests to your Kubernetes cluster using

kubectl apply -f laravel-app-deployment.yml
and
kubectl apply -f laravel-app-service.yml
.


Step 4: Managing Environment Variables


Manage your Laravel application's environment variables in Kubernetes using ConfigMaps or Secrets. This allows you to separate configuration from your code and keep sensitive information secure.


Step 5: Scaling and Load Balancing


Kubernetes makes it easy to scale your Laravel application by adjusting the number of replicas in your Deployment. Load balancing is automatically handled by Kubernetes, ensuring that traffic is distributed evenly to all running pods.


Conclusion


Deploying Laravel applications with Docker and Kubernetes offers scalability, reliability, and ease of management. By containerizing your application and leveraging Kubernetes for orchestration, you can efficiently run and scale your Laravel projects in production environments.