To set up a Laravel project using Kubernetes, you can follow these general steps. This guide will help you get started with deploying a Laravel application along with Redis, MySQL, and Meilisearch on a Kubernetes cluster. We'll also touch on using managed services and deploying multiple websites.
Prerequisites
- Kubernetes Cluster: You need access to a Kubernetes cluster. You can use a local setup like Minikube or a cloud provider like Google Kubernetes Engine (GKE), Amazon EKS, or Azure AKS.
- kubectl: Ensure you have
kubectlinstalled and configured to interact with your cluster. - Docker: You need Docker to build your application images.
Steps
1. Dockerize Your Laravel Application
Create a Dockerfile for your Laravel application:
FROM php:8.1-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www
# Change current user to www
USER www-data
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
2. Create Kubernetes Manifests
Create a deployment.yaml for your Laravel application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel-app
spec:
replicas: 1
selector:
matchLabels:
app: laravel
template:
metadata:
labels:
app: laravel
spec:
containers:
- name: laravel
image: your-dockerhub-username/laravel-app:latest
ports:
- containerPort: 9000
Create a service.yaml for your Laravel application:
apiVersion: v1
kind: Service
metadata:
name: laravel-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 9000
selector:
app: laravel
3. Deploy MySQL, Redis, and Meilisearch
For simplicity, you can use Helm charts or managed services for these components. Here's an example of deploying MySQL using a Kubernetes manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: yourpassword
ports:
- containerPort: 3306
For Redis and Meilisearch, you can follow similar steps or use managed services provided by your cloud provider.
4. Deploy Multiple Websites
To deploy multiple websites, you can create separate deployments and services for each Laravel application. Ensure each deployment has a unique label and service name.
5. Use Managed Services
If you prefer managed services for MySQL and Redis, you can configure your Laravel application to connect to these services by updating the environment variables in your Kubernetes deployment.
Conclusion
This is a basic setup to get you started with deploying a Laravel application on Kubernetes. For a production-ready setup, consider using Helm for managing your deployments, setting up proper networking, and implementing security best practices. Additionally, you might want to explore using Ingress controllers for routing traffic to multiple applications.