How to scale PHP application across multiple NGINX Docker nodes

docker with multiple php apps logoGreetings to Docker specialists and regular visitors of our blog! What should you do if your project has grown and single server can’t handle the load? What should be considered during scaling process? How to correctly scale your high volume website? And which docker commands you should be familiar with to perform this?

I’ll try to disclose this topic in details and show you the best ways to solve such problems.

So, let’s imagine that your project is developed on PHP and it was dockerized before. You’re using single node for your whole stack and remote MySQL server as a database server. You’ve launched a marketing campaign today and huge wave of visitors has flown to your website, but something is wrong. Surprisingly everything began slowing down and users are experiencing bad experience. Or it can be even worse: your website started to throw errors and just stopped opening at all. Am I talking about something familiar? So let’s go!

Ideally you should use applications in Docker when they’re serverless, in other words when they’re not dependent on permanent files or data writing to the disk, so we’re able to easily launch them on any available server. But in most cases the situation is different, so let’s review the example of usage of WordPress inside of Docker (not the best option), but it will work to provide you with clear example of how to correctly use scaling for PHP. Also, we will show you in live how to use corresponding docker commands to resolve such situation.

Let’s begin from the review of the scaling schema of PHP Docker high available cluster:

scale php application across multiple nginx docker nodes diagram

NFS storage – we’ll use it as a shared storage for our WordPress. We’ll carry out wp-content directory there, where plugins and static content (i.e images) is stored. Of course the best way would be to store images in some kind of CDN, but that’s totally another story.

HAproxy Load Balancer – will be used for redirecting users to the correspondent container and distribute the load accordingly.

Code container – we’ll keep WordPress core here and run Nginx with PHP-FPM.

Cron container – this is a container with your code, but it will be used only for cron tasks execution. I’d like to draw your attention that WordPress has several options of cron executions. First one is usage of standard CRON function, when task is executed when client appeals to the website, and second option is usage of system cron. If we’re talking about high load usage of system crons is better option, as it will reduce the load on the servers.

High Available Cluster Info

Here’s what was used in order to get started:

Two servers with Ubuntu 16.04 in Digital Ocean thedockerexperts-wp-01 & thedockerexperts-wp-02

Test domain wp.thedockerexperts.com

Installed and configured Rancher rancher.example.com

Private registry

Remote MySQL Server

Docker-ce Installation

Before the start we need to check the compatibility of Docker versions and your Rancher. When the article was written I used Rancher 1.6 and versions were checked via https://rancher.com/docs/rancher/v1.6/en/hosts/

I installed docker-ce 17.03.x-ce on all hosts:

NFS server setup

I’ve chosen thedockerexperts-wp-01 to be the NFS server:

Now we’re creating directory www-data with permissions that will be shared between hosts:

To limit the accesses from trusted hosts let’s edit file /etc/exports and add this string:

You need to change the data according to your IP addresses and dirs.

IMPORTANT! I suggest to use internal network with private IPs for NFS and also cover all of that with firewall for security reasons.

Now we’re setting up NFS server on all servers:

To check let’s mount the dir /srv/nfs-server/wp.thedockerexperts.com/shared with previously added content of wp-content:

Docker build Build and publish Docker images

To ease the process our company has prepare ready-to-go Docker image with NginX and PHP-FPM, which is available on Docker Hub. We’ll use it for building images of cronjob container and container with your code.

Directories structure:

Disabling of standard wp-cron:

To disable standard wp-cron and use system one we need to add the next string to wp-config.php file:

NginX Docker Image

We put code of your app to NginX Docker container with PHP-FPM & excluding wp-content and called it CODEBASE:

Content of lemp7/Dockerfile:

To configure NginX we use simple config lemp7/conf/nginx/wp.thedockerexperts.com:

Don’t forget that you need to change paths in Dockerfile as well as NginX configuration file according to your project’s specs.

Docker commands for image building ( you can also read our overview about top docker commands which every expert should know )

Docker commands for image publishing

CRON image

CRON container will use files of CODEBASE image, so all PHP modules dependencies are followed and your code is in place.

Content of cron/Dockerfile:

Content of cron/conf/supervisor/supervisord.conf:

Content of cron/conf/cron/crontab:

Docker commands for image building

Docker commands for image publishing

Rancher

Create Base Environment

Click on the button Add environment in envs. management, which can be found by following the link in your Rancher: https://rancher.example.com/settings/env

rancher screenshotLet’s enter the new environment and start adding hosts.

rancher env menu screenshotIn your environment we’ll see a notification that before container launch we need to add hosts with compatible Docker versions. You’re able to check them by following the link.

Add Hosts

Infrastructure 〉Add Host

We copy the content of the 5th point and add it to the console of our servers. When installation process of rancher-agent is over, you’ll have to see similar data, as it’s shown on the below screenshot:

Connect Private Registry

Infrastructure 〉Registry 〉Add registry

Stacks and services

Now when everything is ready, we can start adding services to Rancher.

NFS Rancher

Go to environment 〉Catalog – let’s use the search to find NFS.

Click on View Details and fill in the fields:

NFS Server: <nfs-server-IP>

Export Base Directory: /

On Remove: Retain

We’ll see the next picture after the launch:

Now we’re adding volume. Let’s proceed in Infrastructure 〉Storage 〉Add Volume:

Name: wp-content

MyScalableWP stack

We need to add stack, where we’ll launch our services. Keep in mind that wp-content needs to be connected from NFS.

Service configuration

Volumes configuration

MySQL External Service

Let’s add MySQL as external service for convenience:

Load balancing

We need to schedule Load Balancer to 2 hosts, where we add balancer=true labels. This menu is available while editing hosts.

Load Balancer service

DNS records

CRON service

To deploy cron container we need to add new service:

Volumes

Testing

Let’s try to install W3 Total Cache WP plugin:

We see that plugin was installed in shared dir:

Let’s try to add one more host.

Don’t forget that we need to update /etc/exports file on the NFS server and add new host.

Conclusions

In this blog post I tried to show you how you can use Docker for scaling of your PHP project in high available cluster. WordPress was used just for example’s clear visibility. However something is not considered in this article and you should always remember that, when you’re ready to scale your project.

1. NFS is a bottleneck in this case. And for HA infrastructure it doesn’t fit really well, because if NFS server goes down your whole project will be down. Also the speed of NFS doesn’t always fit for production mode – consider CDN to serve static content. Alternative for NFS is GLusterFS, it’s more appropriate for HA but the data transfer speed won’t be really high if we’re comparing it with regular local disks.

2. Take into consideration where your sessions are stored. In Worpdress case it’s not actual, but with other PHP projects you’ll need to put them to shared storage. In my opinion the best option here would be Redis or database.

3. Don’t forget about the size of your docker images. Always try to cut your image, so the time spent for its build and deploy is reduced.

I’ll be really glad to see any kind of comments, questions and suggestions!

ITsyndicate contact form

Contact us if you have any questions!