Linux bind IP that doesn’t exist with net.ipv4.ip_nonlocal_bind

栏目: IT技术 · 发布时间: 5年前

内容简介:, which allows processes to bind() to non-local IP addresses, which can be quite useful for application such as load balancer such as Nginx, HAProxy, keepalived and others. This page explains how to bind IP address that doesn’t exist with net.ipv4.ip_nonlo

H ow do I allow Linux processes to bind to IP address that doesn’t exist yet on my Linux systems or server?

You need to set up net.ipv4.ip_nonlocal_bind

, which allows processes to bind() to non-local IP addresses, which can be quite useful for application such as load balancer such as Nginx, HAProxy, keepalived and others. This page explains how to bind IP address that doesn’t exist with net.ipv4.ip_nonlocal_bind Linux kernel option.

Why use net.ipv4.ip_nonlocal_bind under Linux operating systems?

HAProxy acts as a load balancer (LB) and a proxy server for TCP and HTTP-based applications. Similarly, Keepalived software provides High-Availability (HA) and Load Balancing features for Linux using VRRP protocol. It acts as an IP failover (Virtual IP) software to route traffic to the correct backend. We can combine HAProxy (or Nginx) along with Keepalived to build a two-node high availability cluster for our applications.

However, LB in HAPorxy, Nginx, and Keepalived need the ability to bind to a non-local IP address. The problem is we can have an IP (Virtual IP) address assigned to one node at a time. So other nodes running Nginx/HAProxy will refuse to start. You will often see an error that as follows:

Nginx: cannot bind socket.

We want to allows a running LB instance to bind to an IP that is not local for failover.

Linux bind IP that doesn’t exist with net.ipv4.ip_nonlocal_bind

Use the sysctl command to find the current value of net.ipv4.ip_nonlocal_bind:

# sysctl net.ipv4.ip_nonlocal_bind
 # sysctl net.ipv6.ip_nonlocal_bind

We can use thecat command as follows too:

cat /proc/sys/net/ipv4/ip_nonlocal_bind

To bind IP that doesn’t exist yet under Linux, run:

sudo sysctl -w net.ipv4.ip_nonlocal_bind=1

Linux bind IP net.ipv4.ip_nonlocal_bind

We can use the following syntax too:

echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind

For IPv6:

sudo sysctl -w net.ipv6.ip_nonlocal_bind=1

Binding to Non-local IP addresses in Linux permanently

Edit the /etc/sysctl.conf or /etc/sysctl.d/99-custom.conf

sudo vi /etc/sysctl.d/99-custom.conf

Append the following line:

## allow Nginx to start and bind to non local IP ##
 net.ipv4.ip_nonlocal_bind=1
 

Save and close the file. To load changes, run:

sudo sysctl -f /etc/sysctl.d/99-custom.conf
Linux bind IP that doesn’t exist with net.ipv4.ip_nonlocal_bind

Understanding sysctl command options

The -w option enable writing a value to Linux kernel variable. The -a option show all variables. For more info type the following man command:

man sysctl

OR

sysctl --help

Sample outputs:

Options:
  -a, --all            display all variables
  -A                   alias of -a
  -X                   alias of -a
      --deprecated     include deprecated parameters to listing
  -b, --binary         print value without new line
  -e, --ignore         ignore unknown variables errors
  -N, --names          print variable names without values
  -n, --values         print only values of a variables
  -p, --load[=<file>]  read values from file
  -f                   alias of -p
      --system         read values from all system directories
  -r, --pattern <expression>
                       select setting that match expression
  -q, --quiet          do not echo variable set
  -w, --write          enable writing a value to variable
  -o                   does nothing
  -x                   does nothing
  -d                   alias of -h
 
 -h, --help     display this help and exit
 -V, --version  output version information and exit

Conclusion

You learned how to set net.ipv4.ip_nonlocal_bind to 1 to configure a high available load-balancer (LB) under Linux. See “ Handling nginx Failover With KeepAlived ” and Linux kernel docs here for more info.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

SEO深度解析

SEO深度解析

痞子瑞 / 电子工业出版社 / 2014-3-1 / CNY 99.00

《SEO深度解析》以SEO从业人员普遍存在的疑问、经常讨论的问题、容易被忽视的细节以及常见的错误理论为基础,对SEO行业所包含的各方面内容进行了深入的讨论,使读者更加清晰地了解SEO及操作思路。内容分为两类:一类为作者根据自己真实、丰富的SEO经验对SEO所涉及的各种问题进行详细的讨论,主要包括SEO 基础原理剖析、SEO实操思路方法、常用工具数据剖析、竞争对手分析案例实操、网站数据分析思路指导、......一起来看看 《SEO深度解析》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具