來源地址: http://linux.chinaunix.net/bbs/thread-1146810-1-1.html
Ippen Yang
(ippen@21cn.com)
1.前言
本文檔主要描述如何利用Linux構(gòu)建一臺能力強大的NAT服務(wù)器,并發(fā)連接數(shù)高達(dá)50萬或更高(最高曾經(jīng)有70萬),流量1GB,系統(tǒng)穩(wěn)定。這個方案適合動手能力強的朋友。
2.許可協(xié)議
本文的許可協(xié)議遵循GNU Free Document License。協(xié)議的具體內(nèi)容請參見http://www.gnu.org/copyleft/fdl.html。在遵循GNU Free Document License的基礎(chǔ)上,可以自由地轉(zhuǎn)載或發(fā)行本文,但請保留本文的完整性。
3.準(zhǔn)備工作
CentOS 4.6光盤一套(4張CD)
kernel-2.6.28.9內(nèi)核源碼
PC服務(wù)器一臺,4核 CPU,速度越快越好,2G或以上的硬盤(可以用CF電子盤),兩個高性能PCI-E千兆網(wǎng)卡(一定要Intel PCI-E的網(wǎng)卡),
參考硬件配置:Intel S3200,512M內(nèi)存*2,Q6600 CPU,80G硬盤,Intel千兆網(wǎng)卡
4.安裝
首先安裝CentOS,安裝時記住要安裝開發(fā)工具,因為要編譯內(nèi)核;安裝時不要建立交換區(qū),切記??!
安裝完成后,運行setup,在system service中將不需要用的程序全部停止,基本上只需要保留ssh,iptables,network,snmpd,syslog幾個,設(shè)置完后重新啟動服務(wù)器。
將kernel文件解壓到/usr/src中,然后運行 make menuconfig,配置內(nèi)核編譯參數(shù),基本上不用改什么參數(shù),注意要選擇多CPU,選擇netfilter中的參數(shù),指定你需要編譯的模塊,然后執(zhí)行make編譯內(nèi)核,make modules_install 安裝內(nèi)核模塊,make install 安裝內(nèi)核
編輯/etc/grub.conf
#boot=/dev/sda1
default=0
timeout=1
#splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.28.9)
root (hd0,2)
kernel /boot/vmlinuz-2.6.28.9 ro root=LABEL=/
initrd /boot/initrd-2.6.28.9.img
title CentOS-4 i386 (2.6.9-34.ELsmp)
root (hd0,0)
kernel /boot/vmlinuz-2.6.9-34.ELsmp ro root=LABEL=/
initrd /boot/initrd-2.6.9-34.ELsmp.img
title CentOS-4 i386-up (2.6.9-34.EL)
root (hd0,0)
kernel /boot/vmlinuz-2.6.9-34.EL ro root=LABEL=/
initrd /boot/initrd-2.6.9-34.EL.img
將服務(wù)器調(diào)整使用2.6.28.9內(nèi)核啟動
編輯/etc/modprobe.conf
確認(rèn)PCI-E網(wǎng)卡的模塊正常,CentOS 4.6有時候?qū)CI-E的網(wǎng)卡當(dāng)pci網(wǎng)卡認(rèn),PCI-E的網(wǎng)卡驅(qū)動是e1000e
編輯/etc/sysctl.conf,查找行“net.ipv4.ip_forward = 0”,然后將這行按下面的內(nèi)容進行更改,原文件中沒有的內(nèi)容請自己添加
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
net.ipv4.netfilter.ip_conntrack_max = 1048576
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 300
編輯/etc/rc.local如下:
#!/bin/sh
touch /var/lock/subsys/local
/root/nat
編輯/root/nat如下:
# eth0 is internet interface
# eth1 is intranet interface
INCOMING=eth0
OUTGOING=eth1
ip addr add 222.222.222.8 dev $OUTGOING
ip addr add 222.222.222.9 dev $OUTGOING
ip addr add 222.222.222.10 dev $OUTGOING
ip addr add 222.222.222.11 dev $OUTGOING
IPPOOL=222.222.222.8-222.222.222.11
iptables -t nat -F
iptables -t nat -A POSTROUTING -s 內(nèi)部IP地址/子網(wǎng) -o $OUTGOING -j SNAT --to $IPPOOL
iptables-save -c >/etc/sysconfig/iptables
注意,這里假定互聯(lián)網(wǎng)IP是222.222.222.8-222.222.222.11(上面黑色字體),請用真正使用的IP范圍將黑色字體替換。內(nèi)網(wǎng)地址也請根據(jù)實際環(huán)境變更。這個腳本只是NAT用,如果需要增加安全策略,請參考其它的iptables的腳本。
完成后,重啟服務(wù)器
重啟后,登錄到系統(tǒng)中
察看中斷分配情況
cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 27 0 0 0 IO-APIC-edge timer
1: 0 1 1 0 IO-APIC-edge i8042
6: 0 1 0 1 IO-APIC-edge floppy
9: 0 0 0 0 IO-APIC-fasteoi acpi
12: 1 0 1 2 IO-APIC-edge i8042
14: 0 0 0 0 IO-APIC-edge ide0
15: 0 0 0 0 IO-APIC-edge ide1
17: 0 0 0 0 IO-APIC-fasteoi uhci_hcd:usb3, ehci_hcd:usb7
18: 0 0 0 0 IO-APIC-fasteoi uhci_hcd:usb1, uhci_hcd:usb6
19: 0 0 0 0 IO-APIC-fasteoi uhci_hcd:usb5
21: 0 0 0 0 IO-APIC-fasteoi uhci_hcd:usb2
23: 0 0 0 0 IO-APIC-fasteoi uhci_hcd:usb4, ehci_hcd:usb8
379: 180564744 180564148 6 4 PCI-MSI-edge eth1
380: 5 3 181325020 181321112 PCI-MSI-edge eth0
381: 528 539 188 188 PCI-MSI-edge ahci
NMI: 0 0 0 0 Non-maskable interrupts
LOC: 2098128 2098112 2098094 2098076 Local timer interrupts
RES: 1235 1318 1707 2126 Rescheduling interrupts
CAL: 1418 1424 1423 10 Function call interrupts
TLB: 260 245 639 597 TLB shootdowns
TRM: 0 0 0 0 Thermal event interrupts
SPU: 0 0 0 0 Spurious interrupts
ERR: 0
MIS: 0
找當(dāng)中與網(wǎng)卡有關(guān)的中斷,上面的表中,中斷379對應(yīng)eth1,中斷380對應(yīng)eth0
進入對應(yīng)的中斷控制目錄,指定cpu0 和cpu1負(fù)責(zé)eth1的中斷處理
cd /proc/irq/379
echo 3 >smp_affinity
指定cpu2和cpu3負(fù)責(zé)eth0的中斷處理
cd /proc/irq/380
echo c >smp_affinity
這樣,指定cpu響應(yīng)不同網(wǎng)卡的請求,提高系統(tǒng)處理能力(上面的表中能明顯看出網(wǎng)卡的中斷是分配在不同的cpu上的)
補充:
修改tcp_timeout_established的參數(shù),在文件 net/netfilter/nf_conntrack_proto_tcp.c 中
修改buckets和ip_conntrack_max的參數(shù),在文件 net/netfilter/nf_conntrack_core.c 里面
需要修改參數(shù)請在編譯前修改
不明白請參考我原來寫的文章 http://linux.chinaunix.net/bbs/viewthread.php?tid=1015371&extra=page%3D2...
有問題請上CU的論壇(http://bbs.chinaunix.net)發(fā)帖子。