Tuesday, April 2, 2024

How to Setting Laravel Behind Kubernetes Proxy


 If you're working with a Laravel application in a Kubernetes environment and need to configure it with K8s Nginx Ingress, here's a detailed guide covering the setup for various components including Laravel Nova, Fortify, Jetstream, and Livewire.

1. Configuring Entry Point for PHP Scripts

Before defining the Ingress, create a ConfigMap to specify the location of the main script file for Nginx Ingress.

kind: ConfigMap

apiVersion: v1

metadata:

  name: gbl-ingress-cm

  namespace: apps-ess-gg-dev

data:

  SCRIPT_FILENAME: /app/public/index.php


2. Configuring Ingress Entry Point

Define the Ingress to serve the Laravel app from a specific URL path.

kind: Ingress

apiVersion: networking.k8s.io/v1

metadata:

  name: gbl-auto-deploy

  namespace: apps-ess-gg-dev

  annotations:

    kubernetes.io/ingress.class: nginx-ess-gg-dev

    nginx.ingress.kubernetes.io/backend-protocol: FCGI

    nginx.ingress.kubernetes.io/fastcgi-index: index.php

    nginx.ingress.kubernetes.io/fastcgi-params-configmap: gbl-ingress-cm

    nginx.ingress.kubernetes.io/use-regex: "true"

spec:

  ingressClassName: nginx-ess-gg-dev

  rules:

    - host: dev.e-sportstats.com

      http:

        paths:

          - path: ^/(?:esports-betting|nova-api|nova-vendor)

            pathType: ImplementationSpecific

            backend:

              service:

                name: gbl-auto-deploy

                port:

                  number: 9000

3. Configuring Laravel Routes

Modify the Laravel routes to reflect the new base path.

Route::prefix(config('app.base_path'))->group(function () {

    // All your routes should be mounted in this block.


    Route::get('/', function () {

        return view('welcome');

    });

}

4. Configuring Laravel Nova


Adjust the Ingress configuration to include Laravel Nova routes.

- path: ^/(?:esports-betting|nova-api|nova-vendor)

  pathType: ImplementationSpecific


5. Configuring PHP-FPM

Modify PHP-FPM configuration to allocate more memory and disable script change checks.

kind: ConfigMap

apiVersion: v1

metadata:

  name: gbl-configs

  namespace: apps-ess-gg-dev

data:

  php-fpm.conf: |

    php_admin_value[memory_limit] = 256M

    php_admin_value[opcache.validate_timestamps] = false


6. Configuring Laravel Application

Update the Laravel configuration files to reflect the new base path and adjust proxy settings.

// config/app.php

return [

  'base_path' => env('APP_BASE_PATH'),

  // ... other settings

];


// bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__))

    ->withMiddleware(function (Middleware $middleware) {

        $middleware->trustProxies('*');

    });


// config/nova.php

return [

  'path' => config('app.base_path').'/nova',

  // ... other settings

];


// config/fortify.php

return [

  'prefix' => config('app.base_path'),

  // ... other settings

];


// config/jetstream.php

return [

  'prefix' => config('app.base_path'),

  // ... other settings

];


7. Configuring Livewire

Adjust Livewire requests to match the new base path.

// app/Providers/AppServiceProvider.php

app(HandleRequests::class)->setUpdateRoute(function ($handle) {

    return Route::prefix(config('app.base_path'))->post('/livewire/update', $handle)->middleware('web');

});

With these configurations in place, your Laravel application should be seamlessly integrated with K8s Nginx Ingress, allowing you to serve it from the desired URL path within your Kubernetes environment.

0 comments:

Post a Comment