8-deploying-a-laravel-application-with-docker-and-kubernetes-on-azure.html

Deploying a Laravel Application with Docker and Kubernetes on Azure

In today’s fast-paced development landscape, deploying applications efficiently is crucial. One powerful combination for modern application deployment is Laravel, Docker, and Kubernetes, especially when hosted on Microsoft Azure. This article will guide you through the step-by-step process of deploying a Laravel application using these technologies, ensuring you have a solid understanding of each component, its use cases, and actionable insights.

What is Laravel?

Laravel is a popular PHP framework known for its elegant syntax and robust features, making it perfect for web application development. It provides developers with the tools necessary to build applications quickly while maintaining code quality. Its built-in features, such as routing, authentication, and ORM (Eloquent), streamline the development process.

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications within lightweight containers. Containers encapsulate an application and its dependencies, ensuring consistency across different environments. This means your application can run the same way on a developer’s laptop, a test server, or in production.

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an orchestration platform for managing containerized applications across a cluster of machines. It automates deployment, scaling, and operations of application containers, providing tools for managing complex applications seamlessly.

Why Use Azure for Deployment?

Microsoft Azure is a cloud computing service that offers a variety of tools and services for building, deploying, and managing applications in the cloud. It provides scalability, reliability, and a wide array of services that integrate well with Docker and Kubernetes, making it an excellent choice for deploying Laravel applications.

Step-by-Step Guide to Deploying a Laravel Application with Docker and Kubernetes on Azure

Prerequisites

Before diving into the deployment process, ensure you have the following:

  • Azure Account: Sign up for an Azure account if you don’t have one.
  • Docker Installed: Ensure Docker is installed on your local machine.
  • Kubernetes: Install kubectl, the command-line tool for Kubernetes.
  • Azure CLI: Install the Azure Command-Line Interface for managing Azure resources.

Step 1: Set Up Your Laravel Application

First, create a new Laravel project:

composer create-project --prefer-dist laravel/laravel my-laravel-app

Navigate into your project directory:

cd my-laravel-app

Step 2: Create a Dockerfile

In the root of your Laravel project, create a file named Dockerfile with the following content:

# Use the official PHP image
FROM php:8.1-fpm

# Set working directory
WORKDIR /var/www

# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev && \
    docker-php-ext-configure gd --with-freetype --with-jpeg && \
    docker-php-ext-install gd

# Copy application code
COPY . .

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

# Install PHP dependencies
RUN composer install

# Expose port
EXPOSE 9000
CMD ["php-fpm"]

Step 3: Create a Docker Compose File

Create a docker-compose.yml file in the root of your project to define services:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

Step 4: Build and Test Locally

To build your Docker image and run the application locally, execute:

docker-compose up --build

Visit http://localhost:9000 in your browser to see your Laravel application running.

Step 5: Push Your Docker Image to Azure Container Registry

Next, you’ll need to push your Docker image to Azure. First, log in to Azure:

az login

Create an Azure Container Registry (ACR):

az acr create --resource-group YOUR_RESOURCE_GROUP --name YOUR_ACR_NAME --sku Basic

Log in to your ACR:

az acr login --name YOUR_ACR_NAME

Now, tag and push your Docker image:

docker tag my-laravel-app YOUR_ACR_NAME.azurecr.io/my-laravel-app
docker push YOUR_ACR_NAME.azurecr.io/my-laravel-app

Step 6: Set Up Kubernetes on Azure

Create an Azure Kubernetes Service (AKS) cluster:

az aks create --resource-group YOUR_RESOURCE_GROUP --name YOUR_AKS_NAME --node-count 1 --enable-addons monitoring --generate-ssh-keys

Connect to your AKS cluster:

az aks get-credentials --resource-group YOUR_RESOURCE_GROUP --name YOUR_AKS_NAME

Step 7: Deploy Your Laravel Application to Kubernetes

Create a deployment.yaml file in your project:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: laravel-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: laravel
  template:
    metadata:
      labels:
        app: laravel
    spec:
      containers:
      - name: laravel
        image: YOUR_ACR_NAME.azurecr.io/my-laravel-app
        ports:
        - containerPort: 9000

Deploy the application:

kubectl apply -f deployment.yaml

Step 8: Expose Your Application

To expose your Laravel application, create a service.yaml file:

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

Apply the service configuration:

kubectl apply -f service.yaml

Conclusion

Congratulations! You have successfully deployed a Laravel application using Docker and Kubernetes on Azure. This setup allows for scalable and maintainable applications, leveraging the power of containers and orchestration. Remember to monitor your application and optimize performance by adjusting the number of replicas and resources as necessary.

Troubleshooting Tips

  • Check Logs: Use kubectl logs <pod-name> to check logs for any errors.
  • Pod Status: Use kubectl get pods to check the status of your pods.
  • Service Access: Ensure your firewall rules allow access to the LoadBalancer service.

By following this guide, you’re now equipped with the knowledge to deploy Laravel applications effectively using modern tools. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.