How to have a autoscalable Laravel API in kubernetes

11/18/2018

So I've been playing with kubernetes for the last few days and diving into all the tech. But I've reached a point that I cannot surpass, hopefully, someone will be able to help me.

My objective: Have a Laravel app running on Nginx + PHP-fpm in kubernetes (each service in its own pod in order to be able to scale independently)

My current problem: When I hit my endpoint I get a 200 result regardless of the code being runned on the background. At this point, I'm not sure anymore if I am miss understanding how nginx+php-fpm+laravel work.

My understanding is that any request would do something along this: Browser->nginx->php-fpm->nginx->browser.

With Nginx purely passing the request along and not actually doing anything with the request itself.

I leave the relevant files in case someone can assist me.

Any help would be more than welcome

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-fpm
  namespace: dash
  labels:
    app: php-fpm
spec:
  selector:
    matchLabels:
      app: php-fpm
  replicas: 1
  template:
    metadata:
      labels:
        app: php-fpm
    spec:
      containers:
      - name: php-fpm
        image: azurecr.io/php
        ports:
        - containerPort: 9000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dash
labels:
  app: nginx
  spec:
    selector:
      matchLabels:
        app: nginx
    replicas: 1
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: azurecr.io/nginx
          ports:
          - containerPort: 80

services.yaml

kind: Service
apiVersion: v1
metadata:
  name: php-service
  namespace: dash
spec:
  selector:
    app: php-fpm
  ports:
  - name: php-fpm-port
    protocol: TCP
    port: 9000
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
  namespace: dash
spec:
  selector:
    app: nginx
  ports:
  - name: "80-nginx"
    port: 80
    targetPort: 80
  - name: "443-nginx"
    port: 443
    targetPort: 443

nginx.conf

server {
    # Set the port to listen on and the server name
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name api.dashboard;

   location / {
       fastcgi_pass php-service.dash:9000;
       fastcgi_buffers 16 16k;
       fastcgi_buffer_size 32k;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       #fixes timeouts
       fastcgi_read_timeout 600;
       include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
   }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/laravel_error.log;
    access_log /var/log/nginx/laravel_access.log;
}

php-fpm.conf

user = www-data
group = www-data
listen = 0.0.0.0:9000
-- Umbert P.
docker
kubernetes
laravel
nginx
php

1 Answer

11/19/2018

After some more testing and investigation my problem was comming from a missunderstanding. I did assumed that nginx would not need to have the code since it would not be processing it but it is indeed needed for the Laravel app to actually run as it should.

The way to fix the problem is copying the code files in the nginx pods aswell.

-- Umbert P.
Source: StackOverflow