<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ipvs on Digi Hunch</title><link>https://static.digihunch.com/tag/ipvs/</link><description>Recent content in ipvs on Digi Hunch</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Sat, 20 Jul 2024 16:48:10 -0400</lastBuildDate><atom:link href="https://static.digihunch.com/tag/ipvs/index.xml" rel="self" type="application/rss+xml"/><item><title>IPVS, iptables and kube-proxy</title><link>https://static.digihunch.com/2020/11/ipvs-iptables-and-kube-proxy/</link><pubDate>Tue, 24 Nov 2020 13:17:00 -0400</pubDate><guid>https://static.digihunch.com/2020/11/ipvs-iptables-and-kube-proxy/</guid><description>&lt;p class="wp-block-paragraph"&gt;This is an overview of the underlying technologies that drives load balancing. It covers LVS, Netfilter, iptables, IPVS and eventually kube-proxy.&lt;/p&gt;&#10;&lt;h3 class="wp-block-heading" id="h-lvs-linux-virtual-server"&gt;LVS (Linux Virtual Server)&lt;/h3&gt;&#10;&lt;p class="wp-block-paragraph"&gt;One of the ways to implement software load balancing is via LVS (Linux Virtual Server), as &lt;a href="https://static.digihunch.com/2020/01/several-ways-to-ensure-high-availability/" class="rank-math-link"&gt;previously discussed&lt;/a&gt;. The diagram below shows the LVS &lt;a href="http://www.linuxvirtualserver.org/about.html" class="rank-math-link"&gt;framework&lt;/a&gt;, with IPVS as the fundamental technology:&lt;/p&gt;&#10;&lt;div class="wp-block-image"&gt;&#10;&lt;figure class="aligncenter size-large"&gt;&lt;img loading="lazy" decoding="async" width="304" height="340" src="https://static.digihunch.com/wp-content/uploads/2021/05/lvs.jpeg" alt="" class="wp-image-2262"/&gt;&lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The major work of the LVS project is to develop advanced IP load balancing software (IPVS), application-level load balancing software (KTCPVS), cluster management components. &lt;a href="http://www.linuxvirtualserver.org/software/ktcpvs/ktcpvs.html" class="rank-math-link"&gt;KTCPVS &lt;/a&gt;implements application-level load balancing inside the Linux kernel (still under development). &lt;a href="http://www.linuxvirtualserver.org/software/ipvs.html" class="rank-math-link"&gt;IPVS &lt;/a&gt;is an advanced IP load balancing software implemented inside the Linux kernel. The IPVS code was already included into the standard Linux kernel 2.4 and 2.6.&lt;/p&gt;&#10;&lt;h3 class="wp-block-heading" id="h-netfilter"&gt;Netfilter&lt;/h3&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Both IPVS and iptables (the technology behind Linux firewall, discussed &lt;a href="https://static.digihunch.com/2018/10/redhat-firewall-configuration-firewalld-vs-iptables/" class="rank-math-link"&gt;here&lt;/a&gt;) are based on &lt;strong&gt;netfilter&lt;/strong&gt;, a &lt;span style="text-decoration: underline;"&gt;packet-filtering framework&lt;/span&gt; provided by the Linux kernel. In this section, we will discuss them all together, starting with Netfilter and then discuss how iptables and IPVS uses netfilter. &lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;Netfilter allows various networking-related operations to be implemented in the form of customized handlers, by offers various functions and operations for &lt;span style="text-decoration: underline;"&gt;packet filtering&lt;/span&gt;, &lt;span style="text-decoration: underline;"&gt;network address translation&lt;/span&gt;, and &lt;span style="text-decoration: underline;"&gt;port translation&lt;/span&gt;, which provide the functionality required for directing packets through a network and prohibiting packets from reaching sensitive locations within a network. Netfilter represents a set of &lt;strong&gt;hooks&lt;/strong&gt; inside the Linux kernel, allowing specific kernel modules to register &lt;strong&gt;callback&lt;/strong&gt; functions with the kernel&amp;#8217;s networking stack. Those functions, usually applied to the traffic in the form of filtering and modification rules, are called for every packet that traverses the respective hook within the networking stack.&lt;/p&gt;&#10;&lt;h3 class="wp-block-heading" id="h-iptables"&gt;Iptables&lt;/h3&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The kernel modules named &lt;strong&gt;ip_tables&lt;/strong&gt;, &lt;strong&gt;ip6_tables&lt;/strong&gt;, &lt;strong&gt;arp_tables &lt;/strong&gt;(the underscore is part of the name), and &lt;strong&gt;ebtables &lt;/strong&gt;comprise the &lt;span style="text-decoration: underline;"&gt;legacy packet filtering portion of the Netfilter hook system&lt;/span&gt;. They provide a table-based system for defining firewall rules that can filter or transform packets. The tables can be administered through the &lt;span style="text-decoration: underline;"&gt;user-space tools&lt;/span&gt; &lt;strong&gt;iptables&lt;/strong&gt;, &lt;strong&gt;ip6tables&lt;/strong&gt;, &lt;strong&gt;arptables&lt;/strong&gt;, and &lt;strong&gt;ebtables&lt;/strong&gt;. &lt;strong&gt;Notice&lt;/strong&gt; that although both the &lt;span style="text-decoration: underline;"&gt;kernel modules&lt;/span&gt; and &lt;span style="text-decoration: underline;"&gt;userspace utilities&lt;/span&gt; have similar names, each of them is a different entity with different functionality.&lt;/p&gt;&#10;&lt;figure class="wp-block-image size-full"&gt;&lt;img loading="lazy" decoding="async" width="1024" height="306" src="https://static.digihunch.com/wp-content/uploads/2023/01/iptables.jpeg" alt="" class="wp-image-7749" srcset="https://static.digihunch.com/wp-content/uploads/2023/01/iptables.jpeg 1024w, https://static.digihunch.com/wp-content/uploads/2023/01/iptables-300x90.jpeg 300w, https://static.digihunch.com/wp-content/uploads/2023/01/iptables-768x230.jpeg 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;When a network packet is received on a network device, it first passes through the &lt;strong&gt;&lt;em&gt;Prerouting &lt;/em&gt;&lt;/strong&gt;hook. This is where the routing decision takes place. The kernel decides whether the packet is destined for a local process (e.g., a listening socket on a server in this system) or whether to forward it (system operates as a router). In the first case, the packet passes the &lt;strong&gt;&lt;em&gt;Input &lt;/em&gt;&lt;/strong&gt;hook and is then handed over to the local process.  If the packet is destined to be forwarded, it traverses the &lt;strong&gt;&lt;em&gt;Forward &lt;/em&gt;&lt;/strong&gt;hook and then a final &lt;strong&gt;&lt;em&gt;Postrouting &lt;/em&gt;&lt;/strong&gt;hook before being sent out on a network device. For packets that are generated locally (e.g., by a client or server process that likes sending things out), they must first pass the &lt;strong&gt;&lt;em&gt;Output &lt;/em&gt;&lt;/strong&gt;hook and then the  &lt;strong&gt;&lt;em&gt;Postrouting &lt;/em&gt;&lt;/strong&gt;hook before being sent out on a network device.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The aforementioned hooks &amp;nbsp;exist independently for the IPv4 and IPv6 protocols. Thus, IPv4 and IPv6 packets each traverse their own hooks. There are also other hooks for ARP packets and for Bridging. And all the &amp;nbsp;hooks exist independently within each network namespace. Additionally, there is an&amp;nbsp;&lt;strong&gt;&lt;em&gt;ingress&amp;nbsp;&lt;/em&gt;&lt;/strong&gt;hook for each network device. The list goes on… More explanations are from &lt;a href="https://www.teldat.com/blog/en/nftables-and-netfilter-hooks-via-linux-kernel/" class="rank-math-link"&gt;here&lt;/a&gt; and &lt;a href="https://www.digitalocean.com/community/tutorials/a-deep-dive-into-iptables-and-netfilter-architecture#iptables-rules" class="rank-math-link"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 class="wp-block-heading" id="h-ipvs"&gt;IPVS&lt;/h3&gt;&#10;&lt;p class="wp-block-paragraph"&gt;In LVS, IPVS is also based on netfilter framework, but works only on INPUT chain, by registering ip_vs_in hook function, to process request. IPVS (aka layer-4 switching) runs on a host at the front of a cluster of real servers. It directs requests for TCP/UDP based servers to the real server, while ensuring the resonse from (one or several) real server appears to the client as if they were all from a virtual service on a sigle IP address. It is based on in-kernel hash tables. The userspace utility is ipvsadm.&lt;/p&gt;&#10;&lt;figure class="wp-block-image"&gt;&lt;img decoding="async" src="https://i.imgur.com/i60QKw4.png" alt=""/&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;When the client request reaches the kernel space of load balancer, it arrives at PREROUTING chain. Route will determine whether the request packet is for the local host or not, based on the destination address of the packet. The packet is sent to INPUT chain if it is. The ip_vs_in function is hooked to LOCAL_IN and will examine the packet. If it finds a matching IPVS rule, it will (bypass INPUT chain) directly trigger POSTROUTING chain, &lt;strong&gt;skipping &lt;/strong&gt;iptables rules.vThis is discussed in detail &lt;a href="http://www.austintek.com/LVS/LVS-HOWTO/HOWTO/LVS-HOWTO.filter_rules.html" class="rank-math-link"&gt;here&lt;/a&gt;. &lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;IPVS supports 8 load balancing algorithms (round robin, weighted round robin, least-connection, weighted least connection, locality-based least-connection, locality-based least-connection with replication, destination-hashing, and source-hashing) and 3 packet-forwarding methods (NAT, tunneling and direct routing).&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The main difference between iptables and IPVS, is &lt;a href="https://www.thegeekstuff.com/2011/01/iptables-fundamentals/"&gt;iptables&lt;/a&gt; includes a number of tables, each with a number of chains, each further involves a number of rules. The total number of rules is large. The packet is assessed against many of such rules. For the same reason, the order of the rule matters. IPVS on the other hand, leverages hash table, with a complexity of O(1), or O(n) in the worst case scenarios. They vary significantly in the efficiency of packet filtering and forwarding, especially when the rules gets complicated. Iptable also presents more latency when adding or removing rules as more rules are involved. This &lt;a href="https://www.slideshare.net/LCChina/scale-kubernetes-to-support-50000-services" class="rank-math-link"&gt;presentation &lt;/a&gt;includes some quantitative comparison.&lt;/p&gt;&#10;&lt;h3 class="wp-block-heading" id="h-kubeproxy"&gt;KubeProxy&lt;/h3&gt;&#10;&lt;p class="wp-block-paragraph"&gt;In Kubernetes architecture, &lt;a class="rank-math-link" href="https://kubernetes.io/docs/reference/command-line-tools-reference/kube-proxy/"&gt;KubeProxy &lt;/a&gt;takes care of load balancing. Kube-proxy can run in three modes: userspace, iptables and IPVS. &lt;/p&gt;&#10;&lt;figure class="wp-block-image is-resized"&gt;&lt;img loading="lazy" decoding="async" src="https://d33wubrfki0l68.cloudfront.net/e351b830334b8622a700a8da6568cb081c464a9b/13020/images/docs/services-userspace-overview.svg" alt="Services overview diagram for userspace proxy" width="826" height="464"/&gt;&lt;figcaption class="wp-element-caption"&gt;userspace proxy mode&lt;/figcaption&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The userspace mode is old and inefficient. The packet is compared against iptables rule and then forwarded to a pod named kube-Proxy, which operates as an application to forward packet to backend pods.&lt;/p&gt;&#10;&lt;figure class="wp-block-image is-resized"&gt;&lt;img loading="lazy" decoding="async" src="https://d33wubrfki0l68.cloudfront.net/27b2978647a8d7bdc2a96b213f0c0d3242ef9ce0/e8c9b/images/docs/services-iptables-overview.svg" alt="Services overview diagram for iptables proxy" width="810" height="601"/&gt;&lt;figcaption class="wp-element-caption"&gt;iptables proxy mode&lt;/figcaption&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;The iptables mode is better since it uses the kernel feature of iptables, which is fairly mature. kube-proxy manages iptables rule based on the service yaml of Kubernetes.&lt;/p&gt;&#10;&lt;figure class="wp-block-image is-resized"&gt;&lt;img loading="lazy" decoding="async" src="https://d33wubrfki0l68.cloudfront.net/2d3d2b521cf7f9ff83238218dac1c019c270b1ed/9ac5c/images/docs/services-ipvs-overview.svg" alt="Services overview diagram for IPVS proxy" width="810" height="601"/&gt;&lt;figcaption class="wp-element-caption"&gt;IPVS proxy mode&lt;/figcaption&gt;&lt;/figure&gt;&#10;&lt;p class="wp-block-paragraph"&gt;With the comparison between iptables and IPVS earlier, we can expect that iptables operations slow down dramatically in large scale cluster. Therefore IPVS based kubeproxy was &lt;a class="rank-math-link" href="https://github.com/kubernetes/kubernetes/issues/17470"&gt;brought up&lt;/a&gt;. This &lt;a class="rank-math-link" href="https://speakerdeck.com/sufuf3/ipvs-based-kube-proxy-for-scaled-kubernetes-load-balancing"&gt;presentation &lt;/a&gt;illustrated the differences.&lt;/p&gt;&#10;&lt;p class="wp-block-paragraph"&gt;In this post we discussed load balancing technologies from ipvs to iptables and then to kube-proxy, which is used in Kubernetes nodes.&lt;/p&gt;&#10;&lt;nav class="wp-post-navigation" aria-label="Post navigation"&gt;&#10;&lt;a rel="prev" href="https://static.digihunch.com/2020/11/how-imaging-devices-talk-to-each-other-tip-in-dicom/"&gt;&lt;span class="wp-post-navigation-label"&gt;Previous Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;How imaging devices talk to each other (in DICOM)&lt;/strong&gt;&lt;/a&gt;&#10;&lt;a rel="next" href="https://static.digihunch.com/2020/12/instance-initialization-with-aws-cdk-in-python/"&gt;&lt;span class="wp-post-navigation-label"&gt;Next Post&lt;/span&gt;&lt;strong class="wp-post-navigation-title"&gt;AWS CDK example in Python – provision Kubernetes Nodes&lt;/strong&gt;&lt;/a&gt;&#10;&lt;/nav&gt;&#10;</description></item></channel></rss>