<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matt Hanley on Medium]]></title>
        <description><![CDATA[Stories by Matt Hanley on Medium]]></description>
        <link>https://medium.com/@_matthanley?source=rss-5b890be75cf3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*g-OQE43KAsWPJQnomwr6rQ.jpeg</url>
            <title>Stories by Matt Hanley on Medium</title>
            <link>https://medium.com/@_matthanley?source=rss-5b890be75cf3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 01:35:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@_matthanley/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tunnelling inter-host networking through a Docker Swarm Overlay network]]></title>
            <link>https://medium.com/@_matthanley/tunnelling-inter-host-networking-through-a-docker-swarm-overlay-network-18f022cd1282?source=rss-5b890be75cf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/18f022cd1282</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[network-namespace]]></category>
            <category><![CDATA[vxlan]]></category>
            <category><![CDATA[overlay]]></category>
            <category><![CDATA[swarm]]></category>
            <dc:creator><![CDATA[Matt Hanley]]></dc:creator>
            <pubDate>Fri, 16 Aug 2019 09:05:54 GMT</pubDate>
            <atom:updated>2019-09-20T09:26:54.051Z</atom:updated>
            <content:encoded><![CDATA[<p>Extending on Laurent Bernaille’s excellent <a href="https://blog.d2si.io/2017/04/25/deep-dive-into-docker-overlay-networks-part-1/">3-part deep dive series on Docker’s overlay networks</a> I wanted to experiment with how we could utilise Docker’s overlay network to tunnel traffic between the nodes in a Swarm outside of the container environment, essentially treating the Overlay network as a mesh VPN between host nodes.</p><h3>Prepare the host</h3><p>The first step is to symlink Docker’s <em>netns</em> mount points to a location where we can use the <em>iproute2</em> utilities to interact with them:</p><pre>ln -s /var/run/docker/netns /var/run/netns</pre><p>We can now view Docker’s network namespaces:</p><pre>ip netns</pre><p>Comparing output with Docker’s networks, we can identify the relevant <em>netns</em> as they’re named according to Docker’s network ID:</p><pre>~# docker network ls<br>NETWORK ID          NAME                DRIVER              SCOPE<br>ed31264a1f4f        bridge              bridge              local<br>5ef35596d5b1        docker_gwbridge     bridge              local<br><strong>or1wj1px3q8b        testoverlay         overlay             swarm</strong><br>bf9f478ebd5d        host                host                local<br>scybkysot08x        ingress             overlay             swarm<br>a9871bc3532d        none                null                local<br>~# ip netns<br>fe5b42ad2e7e (id: 3)<br><strong>1-or1wj1px3q (id: 2)</strong><br>lb_or1wj1px3<br>1-scybkysot0 (id: 0)<br>ingress_sbox (id: 1)</pre><p>Now that we’ve identified the correct <em>netns</em> for the overlay network, we need to add a virtual interface to the host to give us access into the overlay network’s namespace.</p><h3>Accessing the Overlay network from the Docker host</h3><h4>Create a new veth pair</h4><p><em>veth</em> interfaces are created in pairs and allow us to pass traffic between namespaces. First, we’ll create a new <em>veth</em> pair and move one end into the relevant namespace for the Docker overlay network we want to connect into:</p><pre>ip link add dev veth1 type veth peer name veth2<br>ip link set dev veth2 netns 1-or1wj1px3q</pre><h4>Assign an IP address and set the MAC address on the host interface</h4><p>First we allocate our <strong>host interface</strong> (<em>veth1</em>) an IP address in the Overlay network’s subnet</p><pre>ip a a 10.0.0.100/24 dev veth1</pre><p>For consistency, we use the same MAC address scheme as Docker: the last 4 bytes of the MAC address match the IP address of the interface and the second one is the VXLAN ID:</p><pre>ip link set dev veth1 address 02:42:0a:00:00:64</pre><h4>Add veth2 to Docker’s bridge device</h4><p>Bridge devices act like small switches, so to connect us into the Overlay network we need to plug one end of our <em>veth</em> pair (<em>veth2</em> — the one we migrated into Docker’s <em>netns</em>) into Docker’s bridge device.</p><pre>ip netns exec 1-or1wj1px3q ip link set master br0 veth2</pre><p>This is the same method used by Docker to connect containers into the local Overlay network on each node.</p><h4>Fix MTU</h4><p>To allow for the size of the VXLAN headers, we need to drop the MTU on our interfaces:</p><pre>ip netns exec 1-or1wj1px3q ip link set mtu 1450 veth2<br>ip link set mtu 1450 dev veth1</pre><h4>Bring the interfaces up</h4><p>Now everything’s configured, the last step for connecting our local host into the overlay network is to bring up the virtual interfaces:</p><pre>ip netns exec 1-or1wj1px3q ip link set up dev veth2<br>ip link set up dev veth1</pre><p>If everything is configured correctly, we’re now able to access containers running on our local host and attached to the overlay network directly. You can test this by firing up a container attached to the network (assuming the network was created with the <em>attachable</em> flag) and pinging the assigned container IP directly from the host.</p><p>One of the key features of Docker’s Overlay networks is the ability for containers to communicate across different host nodes, so the next step is to enable forwarding over the VXLAN tunnel for our new interfaces.</p><h3>Configure forwarding over VXLAN overlay</h3><p>Once all of the nodes in the Swarm have been configured to access the overlay network locally (as above), we need to tell the kernel how traffic should be passed between hosts. Usually Swarm manages this for us, but since we’re working outside of the Swarm context we need to configure this manually.</p><h4>Create permanent ARP entries</h4><p>As we don’t have a Layer 2 network spanning the network namespaces on our nodes, we need to manually create ARP table entries to tell the kernel through which device to send traffic destined for the other side of our VXLAN tunnel. Here we add the IP and MAC address of the <em>veth1</em> (host side) interface on the <strong>remote</strong> side of the tunnel, and tell the kernel to forward traffic via the <em>vxlan0</em> device. This device is created by Docker as part of the Swarm overlay network.</p><pre>ip netns exec 1-or1wj1px3q ip n a 10.0.0.101 lladdr 02:42:0a:00:00:65 nud permanent dev vxlan0</pre><p>This needs to be completed on each of the nodes in the Swarm, with an entry added for every other node. Note that since the overlay network is part of the distributed Swarm config they share the same network ID and hence the network namespaces share a common name across host nodes.</p><h4>Configure forwarding</h4><p>With the kernel now forwarding traffic to the <em>vxlan0</em> device, we just need to configure our bridge’s forwarding database to pass the traffic to the correct remote host over the VXLAN tunnel.</p><p>Again for each node, we need to configure the correct remote endpoint to forward our traffic to. Here we tell the forwarding database that MAC address <strong>02:42:0a:00:00:65</strong> (the <em>veth1</em> interface on the remote host with overlay IP address 10.0.0.101) resides on the remote host with IP <em>x.x.x.x </em>(where <em>x.x.x.x</em> is the IP address used as Swarm’s <em>advertise-addr</em>)</p><pre>ip netns exec 1-or1wj1px3q bridge fdb add 02:42:0a:00:00:65 dev vxlan0 dst x.x.x.x self permanent</pre><p>We can now test connectivity by pinging the remote host’s overlay IP from our local host:</p><pre>~# ping 10.0.0.100<br>PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.<br>64 bytes from 10.0.0.100: icmp_seq=1 ttl=64 time=10.4 ms<br>64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=10.0 ms</pre><h3>Summary</h3><p>We’ve demonstrated how Docker’s Overlay networks use VXLANs and network namespaces to provide container network isolation and inter-host communication, as well as how we can manipulate these to tunnel traffic between host nodes across one of those overlay networks.</p><p>Note that bridging a host interface into an overlay network is <strong>definitely not the recommended way</strong> to expose containers to the Docker host, or to the Docker host’s network. There are also other factors to consider such as IP addresses conflicting with Docker’s IPAM and persistence — Docker is clever enough to only configure an overlay network on a host when it’s required by a running container, so it’s not necessarily safe to rely on existence of the overlay network on any given node.</p><blockquote><a href="https://medium.com/@_matthanley">Matt Hanley</a> is a Software and Infrastructure Engineer committed to increasing the adoption of cloud-native development and deployment strategies. Connect with me on <a href="https://www.linkedin.com/in/matthew-hanley-b032601a/">LinkedIn</a> or say hi on <a href="https://twitter.com/_matthanley">Twitter</a>, mentioning this story when you write.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18f022cd1282" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MariaDB Galera in Docker: Persistent Storage and Automating the Bootstrapping Process]]></title>
            <link>https://medium.com/@_matthanley/mariadb-galera-in-docker-persistent-storage-and-automating-the-bootstrapping-process-b5600db35e21?source=rss-5b890be75cf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5600db35e21</guid>
            <category><![CDATA[mariadb]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[galera]]></category>
            <dc:creator><![CDATA[Matt Hanley]]></dc:creator>
            <pubDate>Wed, 13 Jun 2018 06:02:08 GMT</pubDate>
            <atom:updated>2020-02-18T12:13:40.597Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Important Note<br></strong>I don’t recommend running this setup in a production environment — it’s an interesting prototype, but gives no control over the bootstrap process (it essentially relies on brute-forcing a race condition on container startup) which can lead to unreliable and unpredictable behaviour.</blockquote><p>Several months ago I was looking at deploying a MariaDB cluster using Galera into our Docker Swarm platform. The end-goal was to implement a zero-touch solution for deploying and scaling MariaDB in Docker which could be fully automated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ww5G7x_KfoqKRfQC" /><figcaption>Photo by <a href="https://unsplash.com/@guilhermestecanella?utm_source=medium&amp;utm_medium=referral">Guilherme Stecanella</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The Challenges</h3><p>Deploying MariaDB in Docker is straight-forward using the <a href="https://hub.docker.com/_/mariadb/">official image</a>. Performance tweaks are similar to those required when running in a traditional VM or bare metal environment, and the config for these tweaks can be baked into a new image or managed through environment variables.</p><p>There are however two main challenges faced when deploying an application like MariaDB in Docker:</p><h4>Persistent Storage</h4><p>Usually, I’ll avoid persistent storage in Docker applications as it can cause issues with scaling across multiple nodes**, preferring instead to utilise external storage services such as our in-house S3 platform. This solution is perfect for object storage, but doesn’t lend itself towards the constant disk access of database workloads.</p><p>Instead, we rolled out three SSD-equipped Docker hosts in different data centres and mounted those SSD’s on Docker’s Volume directory. For each new database instance, we create a new Docker Volume (on the SSD) for database storage.</p><p>If we try and run MariaDB with this set up and then scale the service to more than one instance on the same node things will start to break due to accessing the same underlying storage from multiple instances of MariaDB, so it’s important that we limit the service to one container per database instance per node. We achieve this by running the service in Global mode across the three Docker hosts, where Global mode runs a single container on every host in the swarm (or a subset of hosts when used with Docker’s service constraints).</p><p>With most applications in Docker, we’d need to manage replication of data between all of the hosts in the swarm to ensure the application is in a consistent state wherever the containers are started. Fortunately the key feature of MariaDB with Galera is application-level clustering which takes care of state (data) synchronisation for us.</p><h4>Bootstrapping the Cluster</h4><p>The second challenge is to find a way to achieve our <em>zero-touch</em> goal. Getting to the point of deploying a MariaDB service and being able to scale it is relatively easy, but requires that we already have an operational Galera cluster in place for the new nodes to join.</p><p>By default, MariaDB with Galera clustering enabled will only join an existing cluster and won’t start a new cluster without manual intervention. This is a very sensible precaution on Galera’s behalf as it prevents a split-brain scenario (where two independent instances of the cluster are running with diverging data) due to network partitioning or if all of the nodes get restarted at the same time. This bootstrapping operation only needs to be performed on a single node; you’re essentially telling the node to become the master node and take control of the cluster.</p><p>The biggest challenge in our Docker environment is knowing when to bootstrap a new cluster, which requires a bit more intelligence. Automating the process of boostrapping a single node with homogeneous containers is tricky because we need to infer the state of the cluster to identify a single instance from which to boostrap the cluster.</p><p><strong>Selecting a Container to Bootstrap the Cluster</strong></p><p>When a new container starts we don’t have any shared state to help us identify the state of the cluster, so we need to rely on some external (to the application) factor to tell the container to bootstrap the cluster. To achieve this we can exploit Docker Swarm’s service networking model. Specifically, we can infer details about the state of our parent service and the other containers (tasks) running as part of that service.</p><p>Docker exposes DNS endpoints for the tasks running under each Service, and a call to getent hosts tasks.$SERVICE_NAME will return a list of the containers running as part of that service, with their IP addresses. In our entrypoint script, we attempt to bootstrap if we’re the “first” container (i.e, the container with the lowest IP address).</p><p><strong>Avoiding Split Brain</strong></p><p>After we’ve decided we should attempt to bootstrap we then loop through the other containers running as part of the service, attempting to connect to each one in turn. If we can connect successfully we check to see if that instance is part of a running cluster, and if so we configure MariaDB to join that cluster.</p><p>If none of our sibling containers are part of an existing cluster, we proceed with bootstrapping a new one by configuring MariaDB appropriately. As the other containers restart (or as the service is scaled and new containers are added), they’ll configure themselves automatically to join our newly created cluster using the same DNS-based service discovery.</p><h4>Building the Image</h4><p>We’ve successfully implemented a fully automated solution for deploying and scaling MariaDB clusters using Galera that handles bootstrapping and persistent storage with no manual intervention required. You can see the resulting image here: <a href="https://github.com/matthanley/docker-galera">https://github.com/matthanley/docker-galera</a></p><blockquote><a href="https://medium.com/@_matthanley">Matt Hanley</a> is a Software and Infrastructure Engineer committed to increasing the adoption of cloud-native development and deployment strategies. Connect with me on <a href="https://www.linkedin.com/in/matthew-hanley-b032601a/">LinkedIn</a> or say hi on <a href="https://twitter.com/_matthanley">Twitter</a>, mentioning this story when you write.</blockquote><p><em>** This isn’t strictly true, but it’s a good generalisation for many applications.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5600db35e21" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>