Redian新闻
>
iptables实战-DNAT、SNAT和负载均衡

iptables实战-DNAT、SNAT和负载均衡

公众号新闻

iptabelsSNATDNAT

SNAT


debianens38,ip172.16.1.2/24172.16.1.1(routereth2)

router2eth1eth2eth1192.168.124.247192.168.124.1;eth2172.16.1.1debian

使debianping172.16.1.1pingdebianpingroutereth1ip192.168.124.247linuxipdebian ping 192.168.124.247ip172.16.1.1,dst ip192.168.124.247dst maceth2macroutermacmac(eth2mac)ip(eth1ip)

debianpingrouterrouterip_forwordrouter

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

debianrouter172.16.1.2routersnatdebiansnatroutereth1iprouterroutersnatdebian

iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 192.168.124.247

debian

DNAT

router(192.168.124.247)debian(172.16.1.2)debian,sshrouterdebianrouter32200debian22

router32200PREROUTINGPREROUTINGdnat使ssh 192.168.124.24732200172.16.1.222

 iptables -t nat -A PREROUTING -d 192.168.124.247 -p tcp --destination-port 32200 -i eth0 -j DNAT --to-destination 172.16.1.2:22

nat


debian4ip4ip访

便nginxnginxip

      location / {
      return 200 <span data-raw-text="" "="" data-textnode-index-1671119076312="26" data-index-1671119076312="1621" data-textnode-notemoji-index-1671119076312="1621" class="character">"$remote_addr\n<span data-raw-text="" "="" data-textnode-index-1671119076312="28" data-index-1671119076312="1636" data-textnode-notemoji-index-1671119076312="1636" class="character">";
      }

nginxnginxip

debian

auto ens38
iface ens38 inet static
address 172.16.1.2
netmask 255.255.255.0
gateway 172.16.1.1
auto ens38:1
iface ens38:1 inet static
address 172.16.1.3
netmask 255.255.255.0
auto ens38:2
iface ens38:2 inet static
address 172.16.1.4
netmask 255.255.255.0
auto ens38:3
iface ens38:3 inet static
address 172.16.1.5
netmask 255.255.255.0

使ipPOSTROUTINGipiptablesstatistic

statistic

statistic
This module matches packets based on some statistic condition. It supports two distinct modes settable with the --mode option.
Supported options:
 --mode  

--mode mode
Set the matching mode of the matching rule, supported modes are random and nth.
randomnth  
[!] --probability p
Set the probability for a packet to be randomly matched. It only works with the random mode. p must be within 0.0 and 1.0. The supported granularity is in 1/2147483648th increments.
 P0.01.0 1/2147483648  
[!] --every n
Match one packet every nth packet. It works only with the nth mode (see also the --packet option).
n nth(--packet)  
--packet p
Set the initial counter value (0 <= p <= n-1, default 0) for the nth mode.
=p0 <= p <= n-1p=n-100nth

https://ipset.netfilter.org/iptables-extensions.man.html

iptables -A POSTROUTING  -t nat -d 192.168.127.247 -m statistic --mode nth --every 4 --packet 0 -j SNAT --to-source 172.16.1.2
iptables -A POSTROUTING  -t nat -d 192.168.127.247 -m statistic --mode nth --every 3 --packet 0 -j SNAT --to-source 172.16.1.3
iptables -A POSTROUTING  -t nat -d 192.168.127.247 -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-source 172.16.1.4
iptables -A POSTROUTING  -t nat -d 192.168.127.247 -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-source 172.16.1.5

tcp0 ,+1snat172.16.1.2
tcp10,+1 snat172.16.1.3
tcp210+1 snat172.16.1.4
tcp3210+1every0 snat172.16.1.2

nat

root@debian:~# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  0.0.0.0/0            192.168.124.247      statistic mode nth every 4 to:172.16.1.2
SNAT       all  --  0.0.0.0/0            192.168.124.247      statistic mode nth every 3 to:172.16.1.3
SNAT       all  --  0.0.0.0/0            192.168.124.247      statistic mode nth every 2 to:172.16.1.4
SNAT       all  --  0.0.0.0/0            192.168.124.247      statistic mode nth every 1 to:172.16.1.5

root@debian:~# curl 192.168.124.247
172.16.1.2
root@debian:~# curl 192.168.124.247
172.16.1.3
root@debian:~# curl 192.168.124.247
172.16.1.4
root@debian:~# curl 192.168.124.247
172.16.1.5
root@debian:~# curl 192.168.124.247
172.16.1.2
root@debian:~# curl 192.168.124.247
172.16.1.3
root@debian:~# curl 192.168.124.247
172.16.1.4
root@debian:~# curl 192.168.124.247
172.16.1.5

https://blog.csdn.net/syaziou/article/details/123443578


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
Shanghai Codifies Accessible Infrastructure for the Old, Disable精选SDE岗位 | Comcast、Seagate、Veritas等公司发布海量岗位!霸权是美国的历史使命Chinese Cities Allow Spring Festival Fireworks Amid COVID Blues微软又给谷歌双重暴击,ChatGPT或将加入Word、PPT和Excel!求职干货 | RBC 2024 暑期实习已开!海外求职:投行 (IBD、S&t、行研等)精选SDE岗位 | Jane Street、Tesla、Splunk发布新岗位!精选SDE岗位 | T-Mobile、State Street、RayMond James发布新岗位!精选SDE岗位 | Kaseya、SAP、Square发布最新岗位!TOLET和TOILET的关系?英国地铁到底叫什么?你不了解的英国还有这些! Part 2MIT、斯坦福、UCB霸榜!2023年USNews全美理工科排名发布秋行南意 行程 感受无需担心CPT、OPT,助你在求职季脱颖而出的零基础CS实用集训班火爆来袭!精选岗位 | Roblox、Snap发布最新岗位!不可错过!普林斯顿陈丹琦最新《大语言模型理解》2022课程!全面讲述BERT、GPT、T5等大模型,附Slides从Oracle迁移到openGauss实战分享笑死!在英国Tim Hortons点double double,一整个尴尬!Dell PowerFlex 为云原生工作负载提供弹性和灵活的基础架构Unsupervised NMT、PBSMT、coarse-to-fine...你都掌握了吗?一文总结机器翻译必备经典模型(二)2023USNews全美理工科排名:MIT、斯坦福、UCB霸榜!精选Data岗位 | Roblox、SelectQuote、Axios发布最新岗位!微软又给谷歌「双重暴击」,ChatGPT或将加入Word、PPT和Excel!专访中科翎碳丨集成「电催化」和「生物合成」系统,聚焦零碳和负碳技术,未来2~3年实现万吨级以上的量产运维必学:一文看懂全局负载均衡与 CDN 内容分发文革前的上海交大篮球(ZT)精选SDE岗位 | PayPal、Salesforce、SAP热招中!非游记(三)解读 ChatGPT 背后的技术重点:RLHF、IFT、CoT、红蓝对抗精选DATA岗位 | Nutrien、Target、Slack发布新岗位!习近平会改行党主席制吗?常委会为何不会扩充为9人?负载均衡 LVS vs Nginx 对比!还傻傻分不清?EeSen、FSMN、CLDNN、BERT、Transformer-XL…你都掌握了吗?一文总结语音识别必备经典模型(二)GPT、BERT、XLM、GPT-2、BART…你都掌握了吗?一文总结文本生成必备经典模型(二)解密负载均衡技术和负载均衡算法Cyber Monday炸鸡甜甜圈超低价抄底, Popeyes实现买一送一自由
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。