We build a lot of applications but making that scalable is very important as you don’t know when the users are going to increase and if the application crashes at that time it can impact the whole business.
For teams considering legacy application modernization, containerizing and deploying Laravel applications on Kubernetes can be a powerful step toward scalability and cloud-native readiness.
To deploy the application on Kubernetes we have 2 options, we can directly add the application on Container Service for Kubernetes (ACK) and next, we can do that with the native approach of deploying it on ECS.
Some of the prerequisites to have are:
● Docker
● Kubernetes
● Kubectl
Let’s break down the process into smaller steps:
Create the docker image of our Laravel Application.
Here’s an example DockerFile:
# Use the official PHP image as the base image
FROM php:7.4-apache
# Install the required packages and extensions
RUN apt-get update \
&& apt-get install -y git zip unzip libpng-dev libonig-dev libxml2-dev \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd \
&& pecl install redis \
&& docker-php-ext-enable redis
# Copy the application code to the container
COPY . /var/www/html
# Set the document root
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install dependencies
RUN composer install --no-interaction --no-scripts --no-progress --prefer-dist
# Set the working directory
WORKDIR /var/www/html
# Expose port 80
EXPOSE 80
# Start Apache
CMD ["apache2-foreground"]
This Dockerfile installs the necessary packages and extensions and starts with the official PHP image. After that, it installs Composer, installs dependencies, establishes the working directory, exposes port 80, moves the application code to the container, and sets the document root. At last, Apache is launched.
To build the docker image we need to run:
$ docker build -t my-laravel-app .
Configure the Kubernetes Configuration Files for Laravel.
We need to create 3 major files deployment manifest, service manifest and ingress manifest for networking.
The key major thing in the deployment manifest is replica it will help to create as many replicas that we need to scale at the time of high traffic.
Sample Deployment manifest file:
# laravel.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-laravel-app
labels:
app: my-laravel-app
spec:
replicas: 1
selector:
matchLabels:
app: my-laravel-app
template:
metadata:
labels:
app: my-laravel-app
spec:
containers:
- name: my-laravel-app
image: my-laravel-app
ports:
- containerPort: 80
env:
- name: DB_HOST
value: mysql
- name: DB_PORT
value: "3306"
- name: DB_DATABASE
value: my_database
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: mysql-secret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
Service Manifest: It helps to expose the application within the cluster to other pods
There are 2 major things here “Port” and “Target Port”. Port helps to decide on which port we can access the service and target port defines on which port the service is expose to other pods.
Sample service manifest file:
#laravel-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-laravel-app
labels:
app: my-laravel-app
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: my-laravel-app
Ingress Manifest: It helps to expose the application to external world.
Sample Ingress manifest file:
#laravel-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-laravel-app
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: my-laravel-app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-laravel-app
port:
name: http
Now let’s deploy all the manifest file to the cluster
$ kubectl create secret generic mysql-secret --from-literal=username=<username> --from-literal=password=<password>
$ kubectl apply -f laravel.yaml
$ kubectl apply -f laravel-service.yaml
$ kubectl apply -f laravel-ingress.yaml
Hurrah, we have deployed the Laravel application on Kubernetes successfully. Now our application can handle the traffic seamlessly.
One of the best practices is to monitor the Kubernetes cluster so it can send you alerts if any of your services go down.
● Grafana
● FluentD
Do share your thoughts in the comments on how you have scaled your application.
Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
Setting Up a Secure CI/CD Pipeline for Node.js Applications on Alibaba Cloud
Alibaba Clouder - June 4, 2018
Alibaba Clouder - November 22, 2018
Alibaba Clouder - May 14, 2019
Sajid Qureshi - September 13, 2018
Alibaba Cloud Serverless - June 28, 2022
Alibaba Clouder - November 21, 2019
Alibaba Cloud Container Service for Kubernetes is a fully managed cloud container management service that supports native Kubernetes and integrates with other Alibaba Cloud products.
Learn MoreProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreA secure image hosting platform providing containerized image lifecycle management
Learn MoreMore Posts by Neel_Shah