Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running XtraDB Cluster Nodes With Namespaces on the Same Host

DZone's Guide to

Running XtraDB Cluster Nodes With Namespaces on the Same Host

This post is a continuance of my Docker series and examines Running Percona XtraDB Cluster nodes with Linux Network Namespaces on the same host.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

In this blog, I want to look into a lower-level building block: Linux Network Namespace.

The same as with cgroups, Docker uses Linux Network Namespace for resource isolation. I was looking into cgroup a year ago, and now I want to understand more about Network Namespace.

The goal is to both understand a bit more about Docker internals and to see how we can provide network isolation for different processes within the same host. You might need to isolate process when running several MySQL or MongoDB instances on the same server (which might come in handy during testing). In this case, I needed to test ProxySQL without Docker.

We can always use different ports for different MySQL instances (such as 3306, 3307, 3308), but it quickly gets complicated.

We could also use IP address aliases for an existing network interface, and use bind=<IP.ADD.RE.SS> for each instance. But because Percona XtraDB Cluster can use three different IP ports and network channels for communications, this also quickly gets complicated.

Linux Network Namespace provides greater network isolation for resources so that it can be a better fit for Percona XtraDB Cluster nodes. Now, setting up Network Namespace in and of itself can be confusing; my recommendation is if you can use Docker, use Docker instead. It provides isolation on process ID and mount points and takes care of all the script plumbing to create and destroy networks. As you will see in our scripts, we need to talk about directory location for datadirs.

Let's create a network for Percona XtraDB Cluster with Network Namespaces.

I will try to do the following:

  • Start four nodes of Percona XtraDB Cluster.
  • For each node, create separate network namespace so the nodes will be able to allocate network ports 3306, 4567, 4568 without conflicts.
  • Assign the nodes IP addresses: 10.200.10.2-10.200.10.5.
  • Create a "bridge interface" for the nodes to communicate, using IP address 10.200.10.1.

For reference, I took ideas from this post: Linux Switching – Interconnecting Namespaces

First, we must create the bridge interface on the host:


Next, we create four namespaces (one per Percona XtraDB Cluster node) using the following logic:


We see the following interfaces on the host:


We also see the following network namespaces:


After that, we can check the namespace IP address:


To enable communication from inside the network namespace to the external world, we should add some iptables rules, e.g.:


enp2s0f0 is an interface that has an external IP address (for some reason, modern Linux distros decided to use names like enp2s0f0 for network interfaces, instead old good "eth0").

To start a node (or MySQLd instance) inside a network namespace, we should use ip netns exec prefix for commands.

For example, to start Percona XtraDB Cluster first node in the namespace pxc_ns1 with IP address 10.200.10.2, we use:


To start following nodes:


As the result of this procedure, we have four Percona XtraDB Cluster nodes running in an individual network namespace, not worrying about IP address and ports conflicts. We also allocated a dedicated IP range for our cluster.

This procedure isn't trivial, but it is easy to script. I also think provides a good understanding what Docker, LXC, or other containerization technologies do behind the scenes with networks.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
nodes ,linux ,docker ,xtradb ,percona ,percona xtradb cluster ,namespaces

Published at DZone with permission of Vadim Tkachenko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}