Having fun with Kubernetes - Chapter 2

martinpham

Martin Pham

Posted on December 23, 2019

Having fun with Kubernetes - Chapter 2

(Hey... Chapter 1 is here)

Honestly, I don’t want to clone all Kubernetes Documentation into 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();
Enter fullscreen mode Exit fullscreen mode

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

Our dream-dev-team is using PHP like this: 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? Well, Hell NO

Let’s start everything with development. Everyone, please download and 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
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

Pretty simple huh?

  • Line 1) 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.
  • Line 2) I create a new directory called /app – where I’m gonna put my application.
  • Line 3) I want to set the current working directory to /app. So everything I’m gonna do from now is for the directory /app.
  • Line 4) 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():
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

You just built your first ever 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
Enter fullscreen mode Exit fullscreen mode

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

$ docker run -p 9000:9000 funny-project  
Enter fullscreen mode Exit fullscreen mode

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 (You'd need to install cgi-fcgi)

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

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

To be continued here!

💖 💪 🙅 🚩
martinpham
Martin Pham

Posted on December 23, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Having fun with Kubernetes - Chapter 2
kubernetes Having fun with Kubernetes - Chapter 2

December 23, 2019