Running Laravel on K8S

Hello! I decided to try running cloudnativemauritius.com on Kubernetes. Since that's our whole thing, right? I expected this to be an easy 30 min job at most, and, as usual, it turned into much more. The website itself is a mix of Laravel and AlpineJS for the most part.

The installation method is on your Github repo so you can follow along if you want. Knowing how to get the website running, I started with the Dockerfile.

FROM registry.suse.com/bci/php:8

WORKDIR /app

COPY . .

RUN zypper -n in php8-intl php8-tokenizer php8-fileinfo php8-dom php8-xmlreader php8-xmlwriter php8-pdo php8-sqlite
RUN zypper -n in nodejs npm

RUN composer install

RUN npm install

COPY .env.example .env

RUN php artisan key:generate
RUN php artisan storage:link

RUN sed -i 's/;extension=pdo_sqlite/extension=pdo_sqlite/' /etc/php8/cli/php.ini
RUN sed -i 's/;extension=sqlite3/extension=sqlite3/' /etc/php8/cli/php.ini
RUN touch ./database/database.sqlite

RUN cat /etc/php8/cli/php.ini | grep sqlite

RUN php artisan migrate --seed

EXPOSE 5173 8000

CMD ./start_script.sh

The start script was ideal as the website had to be run in two parts, first through php artisan serve and then npm run dev. All it did was run the two commands with the "&" symbol at the end to signify not to wait for the process to finish.

My first issue was Laravel not responding to localhost:8000 on my host machine when running the plain Docker container. This is because usually PHP will serve the website to 'localhost' inside the container, and thus connections from outside failed. After specifying the host as '0.0.0.0', the Laravel side of things worked.

I'll skip the boring details of how I was running `npm run dev` in the Docker container, causing CORS errors of the PHP and JS sides being on different ports and not being able to access each other. This is the part which took me the most time as I was insisting on finding a way. It seems fairly obvious not to put 'npm run dev' in the Docker image in hindsight. Oh well...

To top things off for Kubernetes, I created a manifest file for the deployment and one for the service which exposes port 8000 through my MetalLB LoadBalancer.