Setting up a scalable and highly available Wordpress environment with Rancher and Docker


Kubernetes和Rancher培训
Join our free online training sessions to learn more about Kubernetes, containers, and Rancher.

Note: Rancher has come a long way since this was first published in June 2015. We’ve revised this post (as of August 2016) to reflect the updates in our enterprise container management service. Read on for the updated tutorial!

Rancher supports multiple orchestration engines for its managed environments, including Kubernetes, Mesos, Docker Swarm, and Cattle (the default Rancher managed environment). The Cattle environment is rich with features like stacks, services, and load balancing, and in this post, we’ll highlight common uses for these features. Overall, we’ll use Rancher and Docker to build a highly available and scalable WordPress environment, then we’ll cover how Rancher Catalog can be used to deploy complex stacks.

A scalable system is one that can handle an increasing number of requests without any impact on the response time and performance. On the other hand, High Availability describes a system that is continuously operational for a given period of time; the components of a high availability service often have failover components to continuously operate under any circumstances. HA Deployment of WordPress Wordpress is one of the most popular CMSs out there, and can be configured with high availability and scalability by using specific components including:

  • Shared storage for the application files: GlusterFS[.]
  • Database Cluster backend: Percona XtraDB Cluster[.]
  • Decoupled application Docker image.
  • Load Balancer to distribute requests to the application containers.

wp-scheme

Preparing a Rancher Environment

When creating the Rancher instance, you can choose any infrastructure, and any modern Linux distribution that supports Docker 1.10.3 (but RancherOS, Ubuntu, RHEL/CentOS 7 are more heavily tested). Note that the machine should have at least 1 GB of RAM. For this walkthrough, we are going to use a 2 GB RAM machine to deploy the latest Rancher server Docker container, using the following command:

# docker run -d -p 8080:8080 rancher/server:v1.1.1

Add
Host

Creating Docker Hosts

Rancher has the ability to create Docker machines at specific cloud service providers -- here, we’re going to create three hosts on AWS by clicking on ‘Add Hosts’, and selecting Amazon EC2. You’ll then need to specify multiple options:

  • Access and Secret keys
  • Region and Subnet
  • Security Group: Rancher will automatically create rancher-machine security groups for each machine, but you will need to edit this to allow HTTP traffic and ports for other services
  • Name, Type and AMI for each instance (in our case, it will be t1.small)
  • Quantity of servers

Create Docker
Hosts After a while, you’ll see your three hosts up and running: Three
Docker
Hosts

Deploying the storage cluster

This section will describe how to setup a GlusterFS storage cluster. Luckily, there’s already a Rancher Catalog template to help things along. Choose the GlusterFS template from the community catalog: GlusterFS
catalog Then set Volume Name to \“myvol\” and Gluster Networking Mode to \“container:glusterfs:server\“, which will make GlusterFS accessible through the Rancher-managed network: Add GlusterFS
stack A couple of minutes after clicking ‘Launch’, you should be able to click over to the infrastructure tab, and see the containers up and running: View GlusterFS
containers

Deploying the database cluster

Now it is time to create the database containers, which are running a Percona XtraDB Cluster (PXC). As PXC needs at least three nodes for quorum, you’ll be deploying three DB containers, each running on a different Docker host for better availability and performance. PXC is available as a catalog template as well - this template will ask for the root and SST passwords; there are optional questions about your MySQL database name, user, and password. Percona cluster
WP The Percona cluster should be ready after a few minutes. Now, the last step to bring up the application is to run the WordPress service.

Deploying the WordPress and LB services

WordPress will be considered a stateless container, enabling you to scale the service from 1 container to 1000 containers. All WordPress containers will read and write from the GlusterFS storage cluster we deployed earlier. The WordPress Docker image must be crafted to mount the GlusterFS storage and then edit the WordPress config file “wp-config.php” to point to the Percona XtraDB Cluster service. The detailed Dockerfile for this Docker image can be found here. To create the WordPress service, add a new stack and insert the following into docker-compose.yml and rancher-compose.yml: WordPress
Stateless

docker-compose.yml
wp:
  environment:
    DB_HOST: pxc
    DB_PASSWORD: password
    GLUSTER_HOST: glusterfs
    GLUSTER_VOL: ranchervol
    WP_DB_NAME: wordpress
  external_links:
  - glusterfs/glusterfs-server:glusterfs
  - pxc/pxc:pxc
  labels:
    io.rancher.container.pull_image: always
  tty: true
  privileged: true
  image: husseingalal/rancher-wp-ha:latest
  stdin_open: true

rancher-compose.yml
wp:
  scale: 3

The WordPress service has two external links to the PXC stack and GlusterFS stack, and starts the container as a privileged container so it is able to mount the GlusterFS volume. The environment variable, DB_HOST, is the name of the PXC service, and GLUSTER_HOST will be the name of the GlusterFS service that we created earlier. The rancher-compose.yml describes the properties of the containers defined in a Rancher environment. In this case, the WordPress service will be scaled to 3 containers. WP Scaled -
3 Now we can create the WordPress Load Balancers. Add a new stack and define a Load Balancer service as follows: Name: wordpress-lb Scale: Set to 3. These are deployed on all Docker hosts for the best availability and performance. Target Service: Link this service to the \“wp\” service Listeners: Enter 80/http for source port and map it to 80/http target port WordPress Load
Balancer After setting up the LB service group, start it by clicking the Start button. Congrats! At this point, you should have your HA WordPress environment up and running. WP Load Balancer -
Running WP
Online

Testing Scalability

If you need to scale up your environment to handle more traffic, you simply click the + button for the Scale option on your WordPress service group. Rancher will automatically handle container creation and add them to the Load Balancers for you. WP - Scaling
Up In this case we will scale up to 10 containers: WordPress Scaling -
10
Containers You can scale up and down as needed.

Testing HA Capabilities

Our WordPress environment will be reliable as long as the following minimum conditions are met:

  1. There is 1 WordPress container running.
  2. There are 2 PXC containers running.
  3. There is 1 Gluster container running, and
  4. There is 1 wordpress-lb agent running.

To test this, let’s simulate the worst-case scenario:

  • Scale down WordPress to 1 container.
  • Scale down GlusterFS to 1 container.
  • Scale down the Load Balancer to 1 container.
  • Scale down the PXC to 2 containers.

Browse to your blog and you will see that it is still online, and you can login on WordPress to enter your admin area.

Conclusion

Rancher has added a new service feature to make it easier to distribute traffic across different Docker containers. Now you can scale up/down your environments from Rancher UI in just one step, and Rancher will automatically create new containers and add them to the appropriate Load Balancers. This greatly simplifies the creation of highly available, scalable, and distributable environments ready for production use. In summary, we have deployed a WordPress installation with redundant services for storage, database, web application, and Load Balancers. We have also simulated common production failover/degradation scenarios and proved that our setup is fault tolerant, as users won’t experience any service disruption.

Kubernetes和Rancher培训
Join our free online training sessions to learn more about Kubernetes, containers, and Rancher.
快速开启您的Rancher之旅