Kube-Proxy: Switching from iptables to IPVS mode

User Story: As we know service to pod
traffic load balancing is random as iptables is used by kube-proxy as default
mode of operation, is there any way so that we ensure to round robin
By default, Kubernetes uses iptables for handling network traffic.
To check if iptables is being used as the network proxy in your Kubernetes cluster.
1. Check Kube-Proxy Mode
Run the following command to see the current configuration of the kube-proxy:
kubectl get cm kube-proxy -n kube-system -o yaml |
In the output, check the mode field under kubeProxyConfiguration:
If mode is set to iptables or is empty (""), kube-proxy is using iptables.
If mode is set to ipvs, then it’s using IPVS.
2. We can check another way to check network proxy
glab@k8smaster:~$ kubectl get pods
-n kube-system -l k8s-app=kube-proxy
|
glab@k8smaster:~$ kubectl logs kube-proxy-424vz -n kube-system |
To change from iptables to IPVS mode in a Kubernetes cluster for kube-proxy, follow these steps:
3. Verify IPVS Support
Make sure that your nodes support IPVS by checking the installed kernel modules:
lsmod | grep ip_vs
|
If you ran lsmod | grep ip_vs and got an empty result, it means the required IPVS kernel modules are not loaded on your system. To enable IPVS mode in Kubernetes, you need to install the IPVS-related kernel modules.
Here’s how to install and load the IPVS modules:
4. Install Required Packages
For Ubuntu/Debian:
sudo apt-get update
|
5. Load IPVS Kernel Modules
After installing ipvsadm, load the necessary IPVS kernel modules.
Run the following commands to load the modules:
sudo modprobe ip_vs
|
These modules enable IPVS with different load-balancing algorithm:
ip_vs: Base IPVS module
ip_vs_rr: Round-robin algorithm
ip_vs_wrr: Weighted round-robin
ip_vs_sh: Source hash scheduling
nf_conntrack: Required for connection tracking
6. Make IPVS Modules Load at Boot
To ensure that the IPVS modules load automatically on system reboot, create a configuration file /etc/modules-load.d/ipvs.conf:
sudo tee /etc/modules-load.d/ipvs.conf <<EOF
|
This will ensure that the modules are loaded every
time the system starts.
7. Verify IPVS Modules Are Loaded
Run the following command again to confirm that the IPVS modules are now loaded:
lsmod | grep ip_vs |
You should see output similar to this:
ip_vs_sh 16384
0
|
8. Modify Kube-Proxy Configuration
Now, modify the kube-proxy configuration to use IPVS mode instead of iptables.
kubectl edit cm kube-proxy -n kube-system |
Look for the mode field under
kubeProxyConfiguration. Change the mode from iptables to ipvs like this:
apiVersion: kubeproxy.config.k8s.io/v1alpha1
|
9. Restart the Kube-Proxy Pods
After editing the configuration, restart the kube-proxy pods to apply the change
Kubernetes will recreate the kube-proxy pods automatically with the new IPVS mode.
kubectl delete pod -n kube-system -l k8s-app=kube-proxy
|
10. Verify the Change
glab@k8smaster:~$ kubectl get pods -n kube-system -l k8s-app=kube-proxy |
To verify that IPVS is now being used, check the
logs of a kube-proxy pod:
glab@k8smaster:~$ kubectl logs kube-proxy-c9j6p -n
kube-system
|
11. To convert from IPVS back to iptables
mode in your Kubernetes cluster
Edit the Kube-Proxy ConfigMap
First, you'll need to modify the kube-proxy configuration to switch the mode from IPVS to iptables.
Run the following command to edit the kube-proxy ConfigMap:
kubectl edit cm kube-proxy -n kube-system |
In the editor, look for the mode field under
kubeProxyConfiguration. If it is currently set to ipvs, change it to iptables,
or simply leave it empty ("") to default to iptables.
mode: iptables
|
Or:
mode: ""
|
12. Delete Existing Kube-Proxy Pods
kubectl delete pod -n kube-system -l k8s-app=kube-proxy
|
13. Verify the Change
Check the logs of the newly created kube-proxy pods:
kubectl logs -n kube-system -l k8s-app=kube-proxy |
Look for the following message to confirm iptables mode:
Using iptables Proxier. |
Summary
To convert from IPVS back to iptables:
● Edit the kube-proxy ConfigMap and set the mode to iptables (or leave it empty).
● Delete the kube-proxy pods to apply the changes.
● Verify that kube-proxy is now using iptables by checking the logs and iptables rules.
This process ensures a smooth transition from IPVS back to iptables mode for service networking in your Kubernetes cluster.
Author
Md. Abu Salman
Software Engineer
Tirzok Private Limited
LinkedIn