Automating Laravel Deployments on Kubernetes with CI/CD Pipelines

Deploying Laravel applications on Kubernetes can be complex, but automating the process with CI/CD pipelines simplifies updates and ensures consistency. This article explores how to set up automated deployments for Laravel on Kubernetes using popular CI/CD tools.

Understanding the Deployment Workflow

The deployment pipeline involves building, testing, and deploying Laravel applications automatically. Kubernetes provides a scalable environment, while CI/CD tools like GitHub Actions, GitLab CI, or Jenkins automate the process from code commit to deployment.

Prerequisites

  • Laravel application configured with Docker
  • Kubernetes cluster (e.g., Minikube, GKE, EKS)
  • Container registry (Docker Hub, GitHub Container Registry)
  • CI/CD platform (GitHub Actions, GitLab CI, Jenkins)
  • Kubeconfig access for deployment

Containerizing the Laravel Application

Create a Dockerfile in your Laravel project:

FROM php:8.1-fpm

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libzip-dev \
    unzip \
    curl

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd pdo pdo_mysql zip mbstring exif pcntl

# Set working directory
WORKDIR /var/www

# Copy existing application directory contents
COPY . /var/www

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install dependencies
RUN composer install --no-dev --optimize-autoloader

# Set permissions
RUN chown -R www-data:www-data /var/www

EXPOSE 9000
CMD ["php-fpm"]

Creating Kubernetes Deployment Files

Define deployment and service manifests for Laravel:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: laravel-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: laravel
  template:
    metadata:
      labels:
        app: laravel
    spec:
      containers:
      - name: laravel
        image: yourdockerhub/laravel:latest
        ports:
        - containerPort: 9000
        env:
        - name: DB_HOST
          value: "mysql"
        - name: DB_DATABASE
          value: "laravel_db"
        - name: DB_USERNAME
          value: "user"
        - name: DB_PASSWORD
          value: "password"
---
apiVersion: v1
kind: Service
metadata:
  name: laravel-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 9000
  selector:
    app: laravel

Configuring CI/CD Pipelines

Set up your CI/CD pipeline to automate build, test, and deployment. Here is an example GitHub Actions workflow:

name: Laravel CI/CD

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Log in to Docker Hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and push Docker image
      uses: docker/build-push-action@v2
      with:
        context: .
        push: true
        tags: yourdockerhub/laravel:latest

    - name: Set up Kubeconfig
      uses: azure/setup-kubectl@v1
      with:
        version: 'v1.21.0'
        kubeconfig: ${{ secrets.KUBECONFIG }}

    - name: Deploy to Kubernetes
      run: |
        kubectl rollout restart deployment/laravel-deployment

Automating Deployment Updates

With the pipeline configured, every push to the main branch triggers a build and deployment. Kubernetes manages rolling updates, minimizing downtime.

Conclusion

Automating Laravel deployments on Kubernetes with CI/CD pipelines streamlines updates, improves reliability, and accelerates development cycles. By containerizing your app, defining deployment manifests, and integrating with CI/CD tools, you can achieve continuous deployment with ease.