1.前言html
本文檔主要描述如何利用Linux構建一臺能力強大的NAT服務器,併發鏈接數高達50萬或更高(最高曾經有70萬),流量1GB,系統穩定。這個方案適合動手能力強的朋友。
2.許可協議
本文的許可協議遵循GNU Free Document License。協議的具體內容請參見http://www.gnu.org/copyleft/fdl.html。在遵循GNU Free Document License的基礎上,能夠自由地轉載或發行本文,但請保留本文的完整性。
3.準備工做
CentOS 4.6光盤一套(4張CD)
kernel-2.6.28.9內核源碼
PC服務器一臺,4核 CPU,速度越快越好,2G或以上的硬盤(能夠用CF電子盤),兩個高性能PCI-E千兆網卡(必定要Intel PCI-E的網卡),
參考硬件配置:Intel S3200,512M內存*2,Q6600 CPU,80G硬盤,Intel千兆網卡
4.安裝
首先安裝CentOS,安裝時記住要安裝開發工具,由於要編譯內核;安裝時不要創建交換區,切記!!
安裝完成後,運行setup,在system service中將不須要用的程序所有中止,基本上只須要保留ssh,iptables,network,snmpd,syslog幾個,設置完後從新啓動服務器。
將kernel文件解壓到/usr/src中,而後運行 make menuconfig,配置內核編譯參數,基本上不用改什麼參數,注意要選擇多CPU,選擇netfilter中的參數,指定你須要編譯的模塊,而後執行 make編譯內核,make modules_install 安裝內核模塊,make install 安裝內核
編輯/etc/grub.conf
QUOTE:
#boot=/dev/sda1
default=0
timeout=1
#splashp_w_picpath=(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
將服務器調整使用2.6.28.9內核啓動
編輯/etc/modprobe.conf
確認PCI-E網卡的模塊正常,CentOS 4.6有時候將PCI-E的網卡當pci網卡認,PCI-E的網卡驅動是e1000e
編輯/etc/sysctl.conf,查找行「net.ipv4.ip_forward = 0」,而後將這行按下面的內容進行更改,原文件中沒有的內容請本身添加
QUOTE:
# 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以下:
QUOTE:
#!/bin/sh
touch /var/lock/subsys/local
/root/nat
編輯/root/nat以下:
QUOTE:
# 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 內部IP地址/子網 -o $OUTGOING -j SNAT --to $IPPOOL
iptables-save -c >/etc/sysconfig/iptables
注意,這裏假定互聯網IP是222.222.222.8-222.222.222.11(上面黑色字體),請用真正使用的IP範圍將黑色字體替換。內網地址也請根據實際環境變動。這個腳本只是NAT用,若是須要增長安全策略,請參考其它的iptables的腳本。
完成後,重啓服務器
重啓後,登陸到系統中
察看中斷分配狀況
cat /proc/interrupts
QUOTE:
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
找當中與網卡有關的中斷,上面的表中,中斷379對應eth1,中斷380對應eth0
進入對應的中斷控制目錄,指定cpu0 和cpu1負責eth1的中斷處理
cd /proc/irq/379
echo 3 >smp_affinity
指定cpu2和cpu3負責eth0的中斷處理
cd /proc/irq/380
echo c >smp_affinity
這樣,指定cpu響應不一樣網卡的請求,提升系統處理能力(上面的表中能明顯看出網卡的中斷是分配在不一樣的cpu上的)
補充:
修改tcp_timeout_established的參數,在文件 net/netfilter/nf_conntrack_proto_tcp.c 中
修改buckets和ip_conntrack_max的參數,在文件 net/netfilter/nf_conntrack_core.c 裏面
須要修改參數請在編譯前修改