Scaling Percona XtraDB Cluster with ProxySQL in Docker Swarm

Percona XtraDB Cluster with ProxySQLIn this post, we’ll look at scaling Percona XtraDB Cluster with ProxySQL in Docker Swarm.

In my previous post, I showed how to employ Percona XtraDB Cluster on multiple nodes in a Docker network.

The intention is to be able to start/stop nodes and increase/decrease the cluster size dynamically. This means that we should track running nodes, but also to have an easy way to connect to the cluster.

So there are two components we need: service discovery to register nodes and ProxySQL to handle incoming traffic.

The work with service discovery is already bundled with Percona XtraDB Cluster Docker images, and I have experimental images for ProxySQL

For multi-node management, we also need some orchestration tool, and a good start is Docker Swarm. Docker Swarm is simple and only provides basic functionality, but it works for a good start. (For more complicated setups, consider Kubernetes.)

I assume you have Docker Swarm running, but if not here is some good material on how to get it rolling. You also need to have service discovery running (see and my previous post).

To start a cluster with ProxySQL, we need a docker-compose definition file docker-compose.yml.:

For convenience, both proxy and percona-xtradb-cluster share the same environment files (.env):

You can also get both files from

To start both the cluster node and proxy:

We can start as many Percona XtraDB Cluster nodes as we want:

The command above will make sure that five nodes are running.

We can check it with docker ps:

We can see that Docker schedules containers on two different nodes, the Proxy SQL container is smblade04/swarm_proxy_1, and the connection point is

To register Percona XtraDB Cluster in ProxySQL we can just execute the following:

The script will connect to the service discovery DISCOVERY_SERVICE (defined in .env file) and register nodes in ProxySQL.

To check that they are all running:

We can connect to a cluster using a ProxySQL endpoint:

We can see that we connect to a different node every time.

Now if we want to get crazy and make sure we have ten Percona XtraDB Cluster nodes running, we can execute the following:

And Docker Swarm will make sure ten nodes are running.

I hope this demonstrates that you can easily start playing with multi-nodes using Percona XtraDB Cluster. In the next post, I will show how to use Percona XtraDB Cluster with Kubernetes.

Share this post

Comments (7)

  • Peter Zaitsev Reply


    It is not totally clear to me how do we handle initialization of the new cluster vs stopping all the nodes and when existing cluster back ?

    In PXC we have –wsrep-new-cluster to set up the new cluster vs adding nodes to existing clusters. I’m not sure how it works in this case

    June 14, 2016 at 2:14 pm
    • Vadim Tkachenko Reply


      So there is when the service discovery comes into play.
      When a node starts, it checks if there any records in the service discovery for CLUSTER_NAME. If there are records – the node will try to join existing nodes by their IP addresses. If there is no records for CLUSTER_NAME – the node will initialize new cluster. The all logic is done in the image entry script –

      June 14, 2016 at 2:18 pm
  • BlindPenguin Reply

    What about two hosts? Would it be possible to configure this in a way, that it will use two (real) machines and deploy only a limited amount of containers on each of them?

    Server 1
    – Percona 1
    – Percona 2

    Server 2
    – Percona 3
    – Percona 4

    Also: How are the database files handled? In the “normal” percona image, there are configuration options where i can load an external directory for them. Also i can do some other stuff, like adding a my.cnf file. Do those options work there too?

    June 23, 2016 at 5:11 am
  • Dm Reply

    Is there any better documentation for this setup? This does not work correctly on latest docker 1.13. Every starts, but none of the auto discovery works and such.

    January 30, 2017 at 3:17 am
  • Martinhop Reply


    Thanks for this article, it was a great help for me.
    But I still don’t get the point of proxySQL, could we just let docker swarm handles load balancing?

    June 21, 2017 at 6:00 am
  • Henrique Abreu Reply

    Thanks for this article, it was a great help for me.

    November 6, 2017 at 5:25 pm
  • brandon Reply

    +1 for BlindPenguin’s request to have database scaling between two hosts with replicated data

    December 12, 2017 at 11:24 am

Leave a Reply