<?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 Faysal Mehedi on Medium]]></title>
        <description><![CDATA[Stories by Faysal Mehedi on Medium]]></description>
        <link>https://medium.com/@faysalmehedi?source=rss-6fe23c38e321------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JnolTBsPG8xCN0izc7ZEiA.jpeg</url>
            <title>Stories by Faysal Mehedi on Medium</title>
            <link>https://medium.com/@faysalmehedi?source=rss-6fe23c38e321------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:32:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@faysalmehedi/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[[Hands-on] Understanding multi-container host networking using vxlan overlay network feature.]]></title>
            <link>https://faysalmehedi.medium.com/hands-on-understanding-multi-container-host-networking-using-vxlan-overlay-network-feature-fc1604bc5a6c?source=rss-6fe23c38e321------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc1604bc5a6c</guid>
            <category><![CDATA[kubernetes-networking]]></category>
            <category><![CDATA[vxlan]]></category>
            <category><![CDATA[devops-practice]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[overlay]]></category>
            <dc:creator><![CDATA[Faysal Mehedi]]></dc:creator>
            <pubDate>Tue, 01 Mar 2022 12:41:39 GMT</pubDate>
            <atom:updated>2022-03-01T12:43:51.174Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>This hands-on demo will provide an overview of container communication between multi-node or multi container daemon under the hood. Find the github repo <a href="https://github.com/faysalmehedi/vxlan-docker-hands-on/">here</a>.</blockquote><h4>What is VxLAN?</h4><p>VxLAN — or Virtual Extensible LAN addresses the requirements of the Layer 2 and Layer 3 data center network infrastructure in the presence of VMs in a multi-tenant environment. It runs over the existing networking infrastructure and provides a means to &quot;stretch&quot; a Layer 2 network. In short, VXLAN is a Layer 2 overlay scheme on a Layer 3 network. Each overlay is termed a VXLAN segment. Only VMs within the same VXLAN segment can communicate with each other. Each VXLAN segment is identified through a 24-bit segment ID, termed the &quot;VNI&quot;. This allows up to 16 M VXLAN segments to coexist within the same administrative domain.</p><h4>What is VNI?</h4><p>Unlike VLAN, VxLAN does not have ID limitation. It uses a 24-bit header, which gives us about 16 million VNI’s to use. A VNI VXLAN Network Identifier (VNI) is the identifier for the LAN segment, similar to a VLAN ID. With an address space this large, an ID can be assigned to a customer, and it can remain unique across the entire network.</p><h4>What is VTEP?</h4><p>VxLAN traffic is encapsulated before it is sent over the network. This creates stateless tunnels across the network, from the source switch to the destination switch. The encapsulation and decapsulation are handled by a component called a VTEP (VxLAN Tunnel End Point). A VTEP has an IP address in the underlay network. It also has one or more VNI’s associated with it. When frames from one of these VNI’s arrives at the Ingress VTEP, the VTEP encapsulates it with UDP and IP headers. The encapsulated packet is sent over the IP network to the Egress VTEP. When it arrives, the VTEP removes the IP and UDP headers, and delivers the frame as normal.</p><h3>What are we going to cover in this hands-on demo?</h3><ul><li>We will use two VM for this, will install docker for running container.</li><li>We have to create separate subnet and assign static IP address for simplicity.</li><li>Then we will create vxlan bridge using linux “ip link vxlan” feature.</li><li>Then bind the vxlan to the docker bridge to crate the tunnel</li><li>Hopefully then we see the response from another host conatiner.</li></ul><h3>Get an overview of the hands-on from the diagram below</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*1yB1NNI05EyIzlSxp7u2YQ.png" /><figcaption>Following hands-on demo will be based on this diagram.</figcaption></figure><h3>Let’s start…</h3><p><strong><em>Step 0:</em></strong> For this demo, anyone can deploy two VM on any hypervisor or virtualization technology. Make sure they are on the same network thus hosts can communicate each other. I launched two ec2 instance(ubuntu) which is on same VPC from AWS to simulate this hands-on. In case of AWS, please allow all traffic in security group to avoid connectivity issues.</p><p><strong><em>Step 1:</em></strong> Install docker client and create separate subnet using docker network utility</p><p>For Host-01</p><pre># update the repository and install docker<br><strong>sudo apt update<br>sudo apt install -y docker.io</strong><br><br># create a separate docker bridge network <br><strong>sudo docker network create --subnet 172.18.0.0/16 vxlan-net</strong><br><br>c43287381077769a873105e49d34d45f5426e42adf52ef7a92394fe0192715b1<br><br># list all networks in docker<br><strong>sudo docker network ls</strong><br><br>NETWORK ID     NAME        DRIVER    SCOPE<br>982c9e8e9e40   bridge      bridge    local<br>2c2b3714bca9   host        host      local<br>223b98ebd6ef   none        null      local<br>c43287381077   vxlan-net   bridge    local<br><br># Check interfaces<br><strong>ip a</strong><br><br>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>    inet 127.0.0.1/8 scope host lo<br>       valid_lft forever preferred_lft forever<br>    inet6 ::1/128 scope host <br>       valid_lft forever preferred_lft forever<br>2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 9001 qdisc fq_codel state UP group default qlen 1000<br>    link/ether 0a:f5:a4:e1:3a:c2 brd ff:ff:ff:ff:ff:ff<br>    inet 10.0.1.4/24 brd 10.0.1.255 scope global dynamic eth0<br>       valid_lft 2529sec preferred_lft 2529sec<br>    inet6 fe80::8f5:a4ff:fee1:3ac2/64 scope link <br>       valid_lft forever preferred_lft forever<br>3: docker0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default <br>    link/ether 02:42:9f:18:ff:df brd ff:ff:ff:ff:ff:ff<br>    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0<br>       valid_lft forever preferred_lft forever<br>4: br-c43287381077: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default <br>    link/ether 02:42:f2:77:0d:ab brd ff:ff:ff:ff:ff:ff<br>    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-c43287381077<br>       valid_lft forever preferred_lft forever</pre><p>For Host-02</p><pre><strong>sudo apt update<br>sudo apt install -y docker.io<br><br>sudo docker network create --subnet 172.18.0.0/16 vxlan-net</strong><br><br>c485be328b349a64ab32f1743658e09d16d39b3fef83946bbfad662831a92a33<br><br><strong>sudo docker network ls</strong><br><br>NETWORK ID     NAME        DRIVER    SCOPE<br>6e1b56c794c3   bridge      bridge    local<br>d1f9b80443c4   host        host      local<br>922f1ff12422   none        null      local<br>c485be328b34   vxlan-net   bridge    local<br><br><strong>ip a</strong><br><br>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>    inet 127.0.0.1/8 scope host lo<br>       valid_lft forever preferred_lft forever<br>    inet6 ::1/128 scope host <br>       valid_lft forever preferred_lft forever<br>2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 9001 qdisc fq_codel state UP group default qlen 1000<br>    link/ether 0a:05:59:dc:14:18 brd ff:ff:ff:ff:ff:ff<br>    inet 10.0.1.41/24 brd 10.0.1.255 scope global dynamic eth0<br>       valid_lft 1984sec preferred_lft 1984sec<br>    inet6 fe80::805:59ff:fedc:1418/64 scope link <br>       valid_lft forever preferred_lft forever<br>3: docker0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default <br>    link/ether 02:42:57:11:86:a0 brd ff:ff:ff:ff:ff:ff<br>    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0<br>       valid_lft forever preferred_lft forever<br>4: br-c485be328b34: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default <br>    link/ether 02:42:29:42:6b:94 brd ff:ff:ff:ff:ff:ff<br>    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-c485be328b34<br>       valid_lft forever preferred_lft forever</pre><p><strong><em>Step 2:</em></strong> Run docker container on top of newly created docker bridge network and try to ping docker bridge</p><p>For Host-01</p><pre># running ubuntu container with &quot;sleep 3000&quot; and a static ip<br><strong>sudo docker run -d --net vxlan-net --ip 172.18.0.11 ubuntu sleep 3000</strong><br><br>a90cbd31d1a20b5e8369f211e65e6fdabf611d01353cb4dc3ff646166ce18e26<br><br># check the container running or not<br><strong>sudo docker ps</strong><br><br>CONTAINER ID   IMAGE     COMMAND        CREATED         STATUS         PORTS     NAMES<br>a90cbd31d1a2   ubuntu    &quot;sleep 3000&quot;   7 seconds ago   Up 6 seconds             jolly_khayyam<br><br># check the IPAddress to make sure that the ip assigned properly<br><strong>sudo docker inspect a9 | grep IPAddress</strong></pre><pre>            &quot;SecondaryIPAddresses&quot;: null,<br>            &quot;IPAddress&quot;: &quot;&quot;,<br>                    &quot;IPAddress&quot;: &quot;172.18.0.11&quot;,<br><br># ping the docker bridge ip to see whether the traffic can pass<br><strong>ping 172.18.0.1 -c 2</strong></pre><pre>PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.<br>64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.047 ms<br>64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.044 ms<br><br>--- 172.18.0.1 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1010ms<br>rtt min/avg/max/mdev = 0.044/0.045/0.047/0.001 ms</pre><p>For Host-02</p><pre><strong>sudo docker run -d --net vxlan-net --ip 172.18.0.12 ubuntu sleep 3000</strong><br><br>7755c18d3550d1fd80d0d00d9dce85dfd8370b2d26cbc088c97bd1b91ac95be7<br><strong><br>sudo docker ps</strong><br><br>CONTAINER ID   IMAGE     COMMAND        CREATED         STATUS         PORTS     NAMES<br>7755c18d3550   ubuntu    &quot;sleep 3000&quot;   9 seconds ago   Up 8 seconds             affectionate_black<br><strong><br>sudo docker inspect 77 | grep IPAddress</strong><br><br>            &quot;SecondaryIPAddresses&quot;: null,<br>            &quot;IPAddress&quot;: &quot;&quot;,<br>                    &quot;IPAddress&quot;: &quot;172.18.0.12&quot;,<br><strong><br>ping 172.18.0.1 -c 2</strong><br><br>PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.<br>64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.047 ms<br>64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.044 ms<br><br>--- 172.18.0.1 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1010ms<br>rtt min/avg/max/mdev = 0.044/0.045/0.047/0.001 ms</pre><p><strong><em>Step 3:</em></strong> Now access to the running container and try to ping another hosts running container via IP Address. Though hosts can communicate each other, conatiner communication should fail because there is no tunnel or anything to carry the traffic.</p><p>For Host-01</p><pre># enter the running container using exec <br><strong>sudo docker exec -it a9 bash</strong></pre><pre># Now we are inside running container<br># update the package and install net-tools and ping tools<br><strong>apt-get update<br>apt-get install net-tools<br>apt-get install iputils-ping</strong><br><br># Now ping the another container<br><strong>ping 172.18.0.12 -c 2</strong><br><br>PING 172.18.0.12 (172.18.0.12) 56(84) bytes of data.<br>--- 172.18.0.12 ping statistics ---<br>2 packets transmitted, 0 received, 100% packet loss, time 2028ms</pre><p>For Host-02</p><pre><strong>sudo docker exec -it 77 bash</strong></pre><pre># inside container<br><strong>apt-get update<br>apt-get install net-tools<br>apt-get install iputils-ping</strong></pre><pre># Now ping the another container<br><strong>ping 172.18.0.11 -c 2</strong></pre><pre>PING 172.18.0.11 (172.18.0.11) 56(84) bytes of data.<br>--- 172.18.0.11 ping statistics ---<br>2 packets transmitted, 0 received, 100% packet loss, time 2028ms</pre><p><strong><em>Step 4:</em></strong> It’s time to create a VxLAN tunnel to establish communication between two hosts running containers. Then attch the vxlan to the docker bridge. Make sure the VNI ID is the same for both hosts.</p><p>For Host-01</p><pre># check the bridges list on the hosts<br><strong>brctl show</strong></pre><pre>bridge name	bridge id		STP enabled	interfaces<br>br-c43287381077		8000.0242f2770dab	no		veth725a704<br>docker0		8000.02429f18ffdf	no</pre><pre># create a vxlan<br># &#39;vxlan-demo&#39; is the name of the interface, type should be vxlan<br># VNI ID is 100<br># dstport should be 4789 which a udp standard port for vxlan communication<br># 10.0.1.41 is the ip of another host<br><strong>sudo ip link add vxlan-demo type vxlan id 100 remote 10.0.1.41 dstport 4789 dev eth0	</strong>	</pre><pre># check interface list if the vxlan interface created<br><strong>ip a | grep vxlan</strong></pre><pre>9: vxlan-demo: &lt;BROADCAST,MULTICAST&gt; mtu 8951 qdisc noop state DOWN group default qlen 1000</pre><pre># make the interface up<br><strong>sudo ip link set vxlan-demo up</strong></pre><pre># now attach the newly created vxlan interface to <br># the docker bridge we created<br><strong>sudo brctl addif br-c43287381077 vxlan-demo</strong></pre><pre># check the route to ensure everything is okay. here &#39;172.18.0.0&#39; part is our concern part.</pre><pre><strong>route -n</strong></pre><pre>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>0.0.0.0         10.0.1.1        0.0.0.0         UG    100    0        0 eth0<br>10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0<br>10.0.1.1        0.0.0.0         255.255.255.255 UH    100    0        0 eth0<br>172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0<br>172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-c43287381077</pre><p>For Host-02</p><pre><strong>brctl show</strong></pre><pre>bridge name	bridge id		STP enabled	interfaces<br>br-c485be328b34		8000.024229426b94	no		veth478bcd1<br>docker0		8000.0242571186a0	no	</pre><pre># 10.0.1.4 is the ip of another host<br># make sure VNI ID is the same on both hosts, this is important</pre><pre><strong>sudo ip link add vxlan-demo type vxlan id 100 remote 10.0.1.4 dstport 4789 dev eth0</strong></pre><pre><strong>ip a | grep vxlan</strong></pre><pre>9: vxlan-demo: &lt;BROADCAST,MULTICAST&gt; mtu 8951 qdisc noop state DOWN group default qlen 1000</pre><pre><strong>sudo ip link set vxlan-demo up<br>sudo brctl addif br-c485be328b34 vxlan-demo</strong></pre><pre><strong>route -n</strong><br>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>0.0.0.0         10.0.1.1        0.0.0.0         UG    100    0        0 eth0<br>10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0<br>10.0.1.1        0.0.0.0         255.255.255.255 UH    100    0        0 eth0<br>172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0<br>172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-c485be328b34</pre><p><strong><em>Step 5:</em></strong> Now test the connectivity. It should work now. A Vxlan Overlay Network Tunnel has been created.</p><p>For Host-01</p><pre><strong>sudo docker exec -it a9 bash</strong></pre><pre># ping the other container IP<br><strong>ping 172.18.0.12 -c 2</strong></pre><pre>PING 172.18.0.12 (172.18.0.12) 56(84) bytes of data.<br>64 bytes from 172.18.0.12: icmp_seq=1 ttl=64 time=0.601 ms<br>64 bytes from 172.18.0.12: icmp_seq=2 ttl=64 time=0.601 ms</pre><pre>--- 172.18.0.12 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1018ms<br>rtt min/avg/max/mdev = 0.601/0.601/0.601/0.000 ms</pre><p>For Host-02</p><pre><strong>sudo docker exec -it 77 bash</strong></pre><pre><strong>ping 172.18.0.11 -c 2</strong></pre><pre>PING 172.18.0.11 (172.18.0.11) 56(84) bytes of data.<br>64 bytes from 172.18.0.11: icmp_seq=1 ttl=64 time=0.601 ms<br>64 bytes from 172.18.0.11: icmp_seq=2 ttl=64 time=0.601 ms</pre><pre>--- 172.18.0.11 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1018ms<br>rtt min/avg/max/mdev = 0.601/0.601/0.601/0.000 ms</pre><h4>Now the hands on completed;</h4><h4>Resources</h4><ul><li><a href="https://datatracker.ietf.org/doc/html/rfc7348">https://datatracker.ietf.org/doc/html/rfc7348</a></li><li><a href="http://ce.sc.edu/cyberinfra/workshops/Material/SDN/Lab%205%20-Configuring%20VXLAN%20to%20Provide%20Network%20Traffic%20Isolation.pdf">http://ce.sc.edu/cyberinfra/workshops/Material/SDN/Lab%205%20-Configuring%20VXLAN%20to%20Provide%20Network%20Traffic%20Isolation.pdf</a></li><li><a href="https://vincent.bernat.ch/en/blog/2017-vxlan-linux">https://vincent.bernat.ch/en/blog/2017-vxlan-linux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc1604bc5a6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding container networking using Linux network namespaces and a virtual switch to isolate…]]></title>
            <link>https://faysalmehedi.medium.com/understanding-container-networking-using-linux-network-namespaces-and-a-virtual-switch-to-isolate-d5dd288e8dcf?source=rss-6fe23c38e321------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5dd288e8dcf</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[namespaces]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Faysal Mehedi]]></dc:creator>
            <pubDate>Tue, 14 Dec 2021 20:31:22 GMT</pubDate>
            <atom:updated>2021-12-14T20:31:22.221Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding container networking using Linux network namespaces and a virtual switch to isolate servers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*G1tLFAB0_m7RJlos2B1Thw.png" /><figcaption>Container networking under the hood. (Picture taken from vmware tanzu blog)</figcaption></figure><h4><strong><em>What is Linux Namespaces?</em></strong></h4><p><strong><em>Linux namespace </em></strong>is an abstraction over resources in the operating system. We can think of a namespace as a box. Inside this box are these system resources, which ones exactly depend on the box’s (namespace’s) type. There are currently 7 types of namespaces <strong><em>Cgroup, IPC, Network, Mount, PID, User, UTS</em></strong>.</p><h4><strong><em>What is Network Namespaces?</em></strong></h4><p>Network namespaces, according to man 7 network_namespaces:<br> network namespaces provide isolation of the system resources associated with networking: network devices, IPv4 and IPv6 protocol stacks, IP routing tables, firewall rules, the /proc/net directory, the /sys/class/net directory, various files under /proc/sys/net, port numbers (sockets), and so on.</p><h4><strong><em>Virtual Interfaces and Bridges:</em></strong></h4><p>Virtual interfaces provide us with virtualized representations of physical network interfaces; and the bridge gives us the virtual equivalent of a switch.</p><h4><strong><em>What are we going to cover?</em></strong></h4><ul><li>We are going to create two network namespace(like two isolated servers), two veth pair(like two physical ethernet cable) and a bridge (for routing traffic between namespaces).</li><li>Then we will configure the bridge as the two namespaces can communicate with each other.</li><li>Then we will connect the bridge to the host and the internet</li><li>At last we will cofigure for incoming traffic(outside) to the namespace.</li></ul><h3>Let’s start…</h3><h4>Step 0:</h4><p>Check basic network status on host machine/root namespace. Just need to track the current status for better understanding. [I launch an ec2 instance(ubuntu) from AWS to simulate this hands-on. VM or even Normal linux machine are also okay.]</p><pre># list all the interfaces<br>sudo ip link</pre><pre>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 9001 qdisc fq_codel state UP mode DEFAULT group default qlen 1000<br>    link/ether 0a:1b:b1:bc:70:d0 brd ff:ff:ff:ff:ff:ff</pre><pre># find the routing table<br>sudo route -n</pre><pre>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>0.0.0.0         172.31.0.1      0.0.0.0         UG    100    0        0 eth0<br>172.31.0.0      0.0.0.0         255.255.240.0   U     0      0        0 eth0<br>172.31.0.1      0.0.0.0         255.255.255.255 UH    100    0        0 eth0</pre><h4>Step 1.1:</h4><p>Create two network namespace</p><pre># add two two network namespaces using &quot;ip netns&quot; command<br>sudo ip netns add ns1<br>sudo ip netns add ns2</pre><pre># list the created network namespaces<br>sudo ip netns list</pre><pre>ns1<br>ns2</pre><pre># By convention, network namespace handles created by<br># iproute2 live under `/var/run/netns`<br>sudo ls /var/run/netns/</pre><pre>ns1 ns2</pre><h4>Step 1.2:</h4><p>By default, network interfaces of created netns are down, even loop interfaces. make them up.</p><pre>sudo ip netns exec ns1 ip link set lo up<br>sudo ip netns exec ns1 ip link</pre><pre>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</pre><pre>sudo ip netns exec ns2 ip link set lo up<br>sudo ip netns exec ns2 ip link</pre><pre>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</pre><h4>Step 2.1:</h4><p>Create a bridge network on the host</p><pre>sudo ip link add br0 type bridge<br># up the created bridge and check whether it is created and in UP/UNKNOWN state<br>sudo ip link set br0 up<br>sudo ip link</pre><pre>3: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/ether 12:38:75:40:c0:17 brd ff:ff:ff:ff:ff:ff</pre><h4>Step 2.2:</h4><p>Configure IP to the bridge network</p><pre>sudo ip addr add 192.168.1.1/24 dev br0<br># check whether the ip is configured and also ping to ensure<br>sudo ip addr</pre><pre>3: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000<br>    link/ether 12:38:75:40:c0:17 brd ff:ff:ff:ff:ff:ff<br>    inet 192.168.1.1/24 scope global br0<br>       valid_lft forever preferred_lft forever<br>    inet6 fe80::1038:75ff:fe40:c017/64 scope link <br>       valid_lft forever preferred_lft forever</pre><pre>ping -c 2 192.168.1.1</pre><pre>--- 192.168.1.1 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1026ms<br>rtt min/avg/max/mdev = 0.020/0.029/0.039/0.009 ms</pre><h4>Step 3.1:</h4><p>Create two veth interface for two network netns, then attach to the bridge and netns</p><pre># For ns1</pre><pre># creating a veth pair which have two ends identical veth0 and ceth0<br>sudo ip link add veth0 type veth peer name ceth0<br># connect veth0 end to the bridge br0<br>sudo ip link set veth0 master br0<br># up the veth0 <br>sudo ip link set veth0 up <br># connect ceth0 end to the netns ns1<br>sudo ip link set ceth0 netns ns1<br># up the ceth0 using &#39;exec&#39; to run command inside netns<br>sudo ip netns exec ns1 ip link set ceth0 up<br># check the link status <br>sudo ip link</pre><pre>3: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000<br>    link/ether 9a:af:0d:89:8b:81 brd ff:ff:ff:ff:ff:ff<br>5: veth0@if4: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br0 state UP mode DEFAULT group default qlen 1000<br>    link/ether 9a:af:0d:89:8b:81 brd ff:ff:ff:ff:ff:ff link-netns ns1</pre><pre># check the link status inside ns1<br>sudo ip netns exec ns1 ip link</pre><pre>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>4: ceth0@if5: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000<br>    link/ether 3e:66:e5:b6:07:9a brd ff:ff:ff:ff:ff:ff link-netnsid 0<br></pre><pre># For ns2; do the same as ns1</pre><pre>sudo ip link add veth1 type veth peer name ceth1<br>sudo ip link set veth1 master br0<br>sudo ip link set veth1 up<br>sudo ip link set ceth1 netns ns2<br>sudo ip netns exec ns2 ip link set ceth1 up</pre><pre>sudo ip link </pre><pre>3: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000<br>    link/ether 1a:f5:b2:8e:ca:a5 brd ff:ff:ff:ff:ff:ff<br>5: veth0@if4: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br0 state UP mode DEFAULT group default qlen 1000<br>    link/ether 9a:af:0d:89:8b:81 brd ff:ff:ff:ff:ff:ff link-netns ns1<br>7: veth1@if6: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br0 state UP mode DEFAULT group default qlen 1000<br>    link/ether 1a:f5:b2:8e:ca:a5 brd ff:ff:ff:ff:ff:ff link-netns ns2</pre><pre>sudo ip netns exec ns2 ip link</pre><pre>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000<br>    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br>6: ceth1@if7: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000<br>    link/ether 3e:1e:48:de:47:07 brd ff:ff:ff:ff:ff:ff link-netnsid 0</pre><h4>Step 3.2:</h4><p>Now we will we add ip address to the netns veth interfaces and update route table to establish communication with bridge network and it will also allow communication between two netns via bridge;</p><pre># For ns1<br>sudo ip netns exec ns1 ip addr add 192.168.1.10/24 dev ceth0<br>sudo ip netns exec ns1 ping -c 2 192.168.1.10<br>sudo ip netns exec ns1 ip route</pre><pre>192.168.1.0/24 dev ceth0 proto kernel scope link src 192.168.1.10<br># check if you can reach bridge interface<br>sudo ip netns exec ns1 ping -c 2 192.168.1.1</pre><pre>--- 192.168.1.1 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1020ms<br>rtt min/avg/max/mdev = 0.046/0.050/0.054/0.004 ms</pre><pre># For ns2<br>sudo ip netns exec ns2 ip addr add 192.168.1.11/24 dev ceth1<br>sudo ip netns exec ns2 ping -c 2 192.168.1.11<br>sudo ip netns exec ns2 ip route </pre><pre>192.168.1.0/24 dev ceth1 proto kernel scope link src 192.168.1.11<br># check if you can reach bridge interface<br>sudo ip netns exec ns2 ping -c 2 192.168.1.1</pre><pre>-------- 192.168.1.1 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1020ms<br>rtt min/avg/max/mdev = 0.046/0.050/0.054/0.004 ms</pre><h4>Step 4:</h4><p>Verify connectivity between two netns and it should work!</p><pre># For ns1: <br># we can log in to netns environment using below; <br># it will be totally isolated from any other network<br>sudo nsenter --net=/var/run/netns/ns1<br># ping to the ns2 netns to verify the connectivity<br>ping -c 2 192.168.1.11</pre><pre>-------- 192.168.1.11 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1019ms<br>rtt min/avg/max/mdev = 0.033/0.042/0.051/0.009 ms<br># exit from the ns1<br>exit<br># For ns2<br>sudo nsenter --net=/var/run/netns/ns2<br># ping to the ns1 netns to verify the connectivity<br>ping -c 2 192.168.1.10</pre><pre>-------- 192.168.1.10 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1022ms<br>rtt min/avg/max/mdev = 0.041/0.044/0.048/0.003 ms<br># exit from the ns2<br>exit</pre><h3>Connectivity between two network namespaces via bridge is completed.</h3><h4>Step 5.1:</h4><p>Now it’s time to connect to the internet. As we saw routing table from ns1 doesn’t have a default gateway, it can’t reach any other machine from outside the 192.168.1.0/24 range.</p><pre>sudo ip netns exec ns1 ping -c 2 8.8.8.8<br>ping: connect: Network is unreachable<br># check the route inside ns1<br>sudo ip netns exec ns1 route -n<br>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ceth0<br># As we can see, no route is defined to carry other traffic than 192.168.1.0/24<br># we can fix this by using adding default route <br>sudo ip netns exec ns1 ip route add default via 192.168.1.1<br>sudo ip netns exec ns1 route -n<br>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 ceth0<br>192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ceth0</pre><pre># Do the same for ns2<br>sudo ip netns exec ns2 ip route add default via 192.168.1.1<br>sudo ip netns exec ns2 route -n</pre><pre>Kernel IP routing table<br>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface<br>0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 ceth1<br>192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ceth1</pre><pre># now first ping the host machine eth0<br>ip addr | grep eth0</pre><pre>2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 9001 qdisc fq_codel state UP group default qlen 1000<br>    inet 172.31.13.55/20 brd 172.31.15.255 scope global dynamic eth0<br># ping from ns1 to host ip<br>sudo ip netns exec ns1 ping 172.31.13.55<br>64 bytes from 172.31.13.55: icmp_seq=1 ttl=64 time=0.037 ms<br>64 bytes from 172.31.13.55: icmp_seq=2 ttl=64 time=0.036 ms<br># we get the response from host machine eth0</pre><h4>Step 5.2:</h4><p>Now let’s see if ns1 can communicate to the internet, we can analysis traffic using tcpdump to see how a packet will travel. Open another terminal for catching traffic using tcpdump.</p><pre># terminal-1<br># now trying to ping 8.8.8.8 again<br>sudo ip netns exec ns1 ping 8.8.8.8</pre><pre># still unreachable<br># terminal 2<br># open tcpdump in eth0 to see the packet<br>sudo tcpdump -i eth0 icmp<br>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br>listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes</pre><pre># no packet captured, let&#39;s capture traffic for br0<br>sudo tcpdump -i br0 icmp</pre><pre>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br>listening on br0, link-type EN10MB (Ethernet), capture size 262144 bytes<br>02:17:30.807072 IP ip-192-168-1-10.ap-south-1.compute.internal &gt; dns.google: ICMP echo request, id 17506, seq 1, length 64<br>02:17:31.829317 IP ip-192-168-1-10.ap-south-1.compute.internal &gt; dns.google: ICMP echo request, id 17506, seq 2, length 64</pre><pre># we can see the traffic at br0 but we don&#39;t get response from eth0.<br># it&#39;s because of IP forwarding issue<br>sudo cat /proc/sys/net/ipv4/ip_forward<br>0</pre><pre># enabling ip forwarding by change value 0 to 1<br>sudo sysctl -w net.ipv4.ip_forward=1<br>sudo cat /proc/sys/net/ipv4/ip_forward<br>1</pre><pre># terminal-2<br>sudo tcpdump -i eth0 icmp<br>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br>listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes<br>02:30:12.603895 IP ip-192-168-1-10.ap-south-1.compute.internal &gt; dns.google: ICMP echo request, id 18103, seq 1, length 64<br>02:30:13.621367 IP ip-192-168-1-10.ap-south-1.compute.internal &gt; dns.google: ICMP echo request, id 18103, seq 2, length 64<br># as we can see now we are getting response eth0<br># but ping 8.8.8.8 still not working<br># Although the network is now reachable, there’s no way that <br># we can have responses back - cause packets from external networks <br># can’t be sent directly to our `192.168.1.0/24` network.</pre><h4>Step 5.3:</h4><p>To get around that, we can make use of NAT (network address translation) by placing an iptables rule in the POSTROUTING chain of the nat table.</p><pre>sudo iptables \<br>        -t nat \<br>        -A POSTROUTING \<br>        -s 192.168.1.0/24 ! -o br0 \<br>        -j MASQUERADE<br># -t specifies the table to which the commands<br># should be directed to. By default it&#39;s `filter`.<br># -A specifies that we&#39;re appending a rule to the<br># chain then we tell the name after it;<br># -s specifies a source address (with a mask in this case).<br># -j specifies the target to jump to (what action to take).</pre><pre># now we&#39;re getting response from google dns<br>sudo ip netns exec ns1 ping -c 2 8.8.8.8</pre><pre>-------- 8.8.8.8 ping statistics ---<br>2 packets transmitted, 2 received, 0% packet loss, time 1002ms<br>rtt min/avg/max/mdev = 1.625/1.662/1.700/0.037 ms</pre><h4>Step: 6</h4><p>Now let’s open a service in one of the namespaces and try to get response from outside</p><pre>sudo nsenter --net=/var/run/netns/netns1<br>python3 -m http.server --bind 192.168.1.10 3000</pre><p>As I have a ec2 instance from AWS, it have an attached public IP. I will try to reach that IP with specific port from outside.</p><pre>telnet 65.2.35.192 5000<br>Trying 65.2.35.192...<br>telnet: Unable to connect to remote host: Connection refused</pre><p>As we can see we can’t reach the destination. Because we didn’i tell the Host machine where to put the incoming traffic. We have to NAT again, this time we will define the destination.</p><pre>sudo iptables \<br>        -t nat \<br>        -A PREROUTING \<br>        -d 172.31.13.55 \<br>        -p tcp -m tcp --dport 5000 \<br>        -j DNAT --to-destination 192.168.1.10:5000<br># -p specifies a port type and --dport specifies the destination port<br># -j specifies the target DNAT to jump to destination IP with port.<br></pre><pre># from my laptop<br># now I can connect the destination with port.<br># We successfully recieved traffic from internet inside container network</pre><pre>telnet 65.2.35.192 5000<br>Trying 65.2.35.192...<br>Connected to 65.2.35.192.<br>Escape character is &#39;^]&#39;.</pre><h4>Now the hands on completed; What we achieve:</h4><ul><li>the host can send traffic to any application inside network namespaces</li><li>the application inside network namespaces can comunicate with host applications and another network namespaces applications</li><li>an application inside the network namespaces can connect to the internet</li><li>an application inside the network namespaces can listen for request from the outside internet</li><li>Finally we understand how Docker or some other container tool done networking under the hood. They automate the whole process for us when we give command like “Docker run -p 5000:5000 frontend-app”</li></ul><h4><em>Only commands will be found </em><a href="https://github.com/faayam/linux-network-namespaces-hands-on/blob/main/ns-project.sh"><em>here</em></a><em>. Find the github repo </em><a href="https://github.com/faayam/linux-network-namespaces-hands-on"><em>here</em></a></h4><h4>Resources</h4><ul><li><a href="https://man7.org/linux/man-pages/man7/network_namespaces.7.html">https://man7.org/linux/man-pages/man7/network_namespaces.7.html</a></li><li><a href="https://man7.org/linux/man-pages/man8/iptables.8.html">https://man7.org/linux/man-pages/man8/iptables.8.html</a></li><li><a href="https://ops.tips/blog/using-network-namespaces-and-bridge-to-isolate-servers/">https://ops.tips/blog/using-network-namespaces-and-bridge-to-isolate-servers/</a></li><li><a href="https://github.com/dipanjal/DevOps/tree/main/NetNS_Ingress_Egress_Traffic">https://github.com/dipanjal/DevOps/tree/main/NetNS_Ingress_Egress_Traffic</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5dd288e8dcf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>