Honestly, I don’t want to make a clone of Kubernetes Documentation on my blog 😂, here I just wanted to share my fun experiences when I was playing with it. So please don’t forget to check the official documentations if you have some doubts.

Alright, let’s talk about our project:

We have a terribly simple project, a PHP file which output PHP’s info:

<?php
phpinfo();

We’d like to deploy it into our servers: we have 2 servers running whatever-linux-distro-let’s-say-Ubuntu-18-LTS.

Our imagination-dev-team is using PHP: One uses PHP 5 (don’t worry, he is our key person for 100 years), someone uses PHP 7.1/7.2/7.3/7.4,.. Ah, they are also using different OS… your best dream team ever 🤣

Marketing team planned a big launch event, expecting 1 million people will visit our website (well, to see our PHP’s info). So we’d like to have a load balancer, but really, nobody knows how many minions we should put – wait, we don’t want to bring up 100 minions running in the midnight to serve nobody.

Sometime, for some funny reason, phpinfo() just crashed php-fpm, and your testers were seeing some not-so-nice 5xx error from nginx.

What we’re gonna do? Just put the file, config nginx, php-fpm, spin up a load balancer, and pray?

Let’s start everything with development. Everyone, please install Docker – Your new friend to make it easier to create, run, and deploy applications by using container!

I don’t care about your OS, your PHP installation, we’re gonna work with the same environment as we have on production: PHP-FPM 7.4

While our devs are busy downloading & installing their new friend Docker, let’s init the repository:

  Project             
   |                 
   |-- Dockerfile                 
   |-- src                    
        |-- index.php

Dockerfile is a definition for your container, where you create the environment for your application.

FROM php:7.4-fpm
RUN mkdir /app
WORKDIR /app
COPY src .

Pretty simple huh?

  • My environment is based on php:7.4-fpm – a PHP official Docker image version 7.4 with PHP-FPM. Means you’re gonna create an environment which has PHP-FPM 7.4, perfect.
  • I create a new directory called /app – where I’m gonna put my application.
  • I want to set the current working directory to /app. So everything I’m gonna do from now is for the directory /app.
  • Finally I want to put our codebase (which is located under directory src inside the repo) into the working directory.
  • Done

Just that, with 4 lines, you defined the environment for your devteam and also for our production server.

Let’s wait for the devteam finishes and pushes their codebase into the repo:

<?php
phpinfo():

Ok, we’re ready to run. First of all, we need to package our application codebase along with our environment:

$ docker build -t funny-project .

You just built your first Docker image, which contains the codebase, and PHP-FPM 7.4.

Let’s verify it

$ docker images -a

REPOSITORY      TAG         SIZE
funny-project   latest      405MB

A 405MB image is registered on your Docker. Let’s try to run it:

$ docker run -p 9000:9000 funny-project  

It will spin up a new Container, based on funny-project image, expose port 9000 from its environment to outside.

You can test it by a fcgi tool

$ SCRIPT_FILENAME=index.php REQUEST_METHOD=GET cgi-fcgi -bind -connect localhost:9000 | grep ">PHP Version <"

It works! You’re having a working container, which is ready to be deployed.

…To be continued

Next chapter https://www.martinpham.com/2019/12/08/having-fun-with-kubernetes-3/