基於Linux的集羣系統(五) Linux集羣系統的實現

1.設計目標

設計一個基於Linux的集羣系統,它能夠提供負載平衡的功能。系統能夠不斷監視集羣中各臺實際服務器的負載狀況,並且將來自外部網的多種請求轉發到內部網中的某一臺實際服務器上執行。

具體來說,它必須擁有以下的功能:

(1)服務轉發。能接受來自外部網中的多種基於TCP/IP的服務請求如FTP 、TELNET、 HTTP等,並且將它們轉發到當前負載最輕的機器上執行。
(2)動態負載平衡。平衡器能夠監視內部網中的實際服務器的負載狀況並且找到負載最輕的機器。
(3)連接持續性。來自外部網的同一客戶的所有請求必須轉發到內部網中的同一臺實際服務器上進行處理。

2.環境設置

如圖 1-1所示,該集羣系統由router、server 1、server 2、server 3以及 server n等機器組成。其中router作爲內部網和外部網的接口,能夠接收外部網的用戶請求,並將此用戶請求發送到server1到servern中的某臺機器上(如server1),當server1處理完用戶的請求以後,就將處理完的結果發送給router,然後再由router將請求迴應返回給外部網的用戶。文章中也將router這臺機器稱爲負載平衡器,因爲它承擔了均衡負載的作用;將實際響應用戶請求的server1等機器稱爲實際服務器。試驗環境中外部網中的客戶機器的IP地址爲192.9.200.53,負載平衡器(router)有兩個IP地址,一個是192.9.200.56,一個是10.1.1.1,負載平衡器上運行的操作系統內核爲Linux 2.2.x。內部網中有n臺實際服務器,它們的IP地址分別爲10.1.1.2、10.1.1.3……..10.1.1.n,他們上面運行的操作系統可以是linux 操作系統,也可以是Windows系列的操作系統,如Windows 98、Windows NT等等。它們都將10.1.1.1設爲網關,並且都增加了通往192.9.200.0網絡的路由。負載平衡的目的就是將客戶發向平衡器的請求如:telnet、 ftp 、www等按照內部網機器當前負載的情況分發到各個實際服務器上。

圖1-1 負載平衡系統環境設置圖
 



3.構建過程

在構建過程這一小節中,首先介紹IP僞裝技術的原理,然後介紹集羣系統的建立過程。本集羣系統的建立首先要建立IP僞裝機制,然後再增加IP Portforwarding(IP端口轉發)機制,然後設計一個應用程序,它能夠根據集羣中的機器的狀態實現實時動態負載平衡。本集羣系統中的負載平衡器上的Linux內核版本爲2.2.x。

3.1 IP僞裝技術

本集羣系統主要採用了IP Masquerade(IP僞裝)機制。該負載平衡系統採用了NAT(network address translation)機制。NAT機制主要用於內部私有網與外部網之間進行通訊。IP地址中的那些私有地址如10.0.0.0/255.0.0.0, 172.16.0.0/255.240.0.0 以及192.168.0.0/255.255.0.0等是無法直接與Internet上的機器通訊的,如果它們想與Internet上的機器通訊,需要採用網絡地址翻譯(Network Address Translation,NAT)機制。

NAT意味着將IP地址從一組映射到另外一組,如果這種映射關係是N-N的,則稱之爲靜態網絡地址翻譯;如果映射是M-N(M>N)的,則叫做動態網絡地址翻譯;IP僞裝機制實際上就是一種M-1的動態網絡地址翻譯,它能夠將多個內部網中的IP地址映射到一個與Internet相連接的外部網IP地址上,這樣這些無法直接與Internet上的機器通訊的具有內部網IP地址的機器就可以通過這臺映射機器與外界進行通訊了。而網絡地址端口翻譯是對網絡地址翻譯的一種擴展,它將許多網絡地址以及它們的TCP/UDP端口翻譯爲一個IP地址和TCP/UDP 端口。本集羣系統採用的就是網絡地址端口翻譯機制。

3.2 IP僞裝機制的建立過程

(1) 編譯核心使之能夠支持IP僞裝。

編譯核心時要注意以下選項的選擇。

 

* Prompt for development and/or incomplete code/drivers
(CONFIG_EXPERIMENTAL) [Y/n/?]
    - YES: though not required for IP MASQ, this option allows
    the kernel to create the MASQ modules and enable the option
    for port forwarding
  -- Non-MASQ options skipped --
  * Enable loadable module support (CONFIG_MODULES) [Y/n/?]
    - YES: allows you to load kernel IP MASQ modules
  -- Non-MASQ options skipped --
  * Networking support (CONFIG_NET) [Y/n/?]
    - YES: Enables the network subsystem
  -- Non-MASQ options skipped --
  * Sysctl support (CONFIG_SYSCTL) [Y/n/?]
    - YES:  Enables the ability to enable disable options such as forwarding,
      dynamic IPs, LooseUDP, etc.
  -- Non-MASQ options skipped --
  * Packet socket (CONFIG_PACKET) [Y/m/n/?]
    - YES: Though this is OPTIONAL, this recommended feature will allow you
    to use TCPDUMP to debug any problems with IP MASQ
  * Kernel/User netlink socket (CONFIG_NETLINK) [Y/n/?]
    - YES: Though this is OPTIONAL, this feature will allow the logging of
    advanced firewall issues such as routing messages, etc
  * Routing messages (CONFIG_RTNETLINK) [Y/n/?]
    - NO:  This option does not have anything to do with packet firewall logging
  -- Non-MASQ options skipped --
  * Network firewalls (CONFIG_FIREWALL) [Y/n/?]
    - YES: Enables the kernel to be comfigured by the IPCHAINS firewall tool
  * Socket Filtering (CONFIG_FILTER) [Y/n/?]
    - OPTIONAL:  Though this doesn't have anything do with IPMASQ, if you plan
      on implimenting a DHCP server on the internal network, you WILL need this
      option.
  * Unix domain sockets (CONFIG_UNIX) [Y/m/n/?]
    - YES:  This enables the UNIX TCP/IP sockets mechanisms
  * TCP/IP networking (CONFIG_INET) [Y/n/?]
    - YES: Enables the TCP/IP protocol
  -- Non-MASQ options skipped --
  * IP: advanced router (CONFIG_IP_ADVANCED_ROUTER) [Y/n/?]
    - YES:  This will allow you to configure advanced MASQ options farther down
  * IP: policy routing (CONFIG_IP_MULTIPLE_TABLES) [N/y/?]
    - NO: Not needed by MASQ though users who need advanced features
    such as TCP/IP source address-based or TOS-enabled routing will
    need to enable this option.
  * IP: equal cost multipath (CONFIG_IP_ROUTE_MULTIPATH) [N/y/?]
    - NO: Not needed for normal MASQ functionality
  * IP: use TOS value as routing key (CONFIG_IP_ROUTE_TOS) [N/y/?]
    - NO:  Not needed for normal MASQ functionality
  * IP: verbose route monitoring (CONFIG_IP_ROUTE_VERBOSE) [Y/n/?]
    - YES: This is useful if you use the routing code to drop IP
    spoofed packets (highly recommended) and you want to log them.
  * IP: large routing tables (CONFIG_IP_ROUTE_LARGE_TABLES) [N/y/?]
    - NO:  Not needed for normal MASQ functionality
  * IP: kernel level autoconfiguration (CONFIG_IP_PNP) [N/y/?] ?
    - NO:  Not needed for normal MASQ functionality
  * IP: firewalling (CONFIG_IP_FIREWALL) [Y/n/?]
    - YES: Enable the firewalling feature
  * IP: firewall packet netlink device
  (CONFIG_IP_FIREWALL_NETLINK) [Y/n/?]
    - OPTIONAL: Though this is OPTIONAL, this feature will allow
    IPCHAINS to copy some packets to UserSpace tools for additional
    checks
  * IP: transparent proxy support (CONFIG_IP_TRANSPARENT_PROXY) [N/y/?]
    - NO:  Not needed for normal MASQ functionality
  * IP: masquerading (CONFIG_IP_MASQUERADE) [Y/n/?]
    - YES: Enable IP Masquerade to re-address specific internal to
    external TCP/IP packets
  * IP: ICMP masquerading (CONFIG_IP_MASQUERADE_ICMP) [Y/n/?]
    - YES: Enable support for masquerading ICMP ping packets
    (ICMP error codes will be MASQed regardless).  This is an
    important feature for troubleshooting connections.
  * IP: masquerading special modules support
  (CONFIG_IP_MASQUERADE_MOD) [Y/n/?]
    - YES: Though OPTIONAL, this enables the OPTION to later enable
    the TCP/IP Port forwarding system to allow external computers to
    directly connect to specified internal MASQed machines.
  * IP: ipautofw masq support (EXPERIMENTAL)
  (CONFIG_IP_MASQUERADE_IPAUTOFW) [N/y/m/?]
    - NO:  IPautofw is a legacy method of port forwarding.  It is
    mainly old code and has been found to have some issues.  NOT
    recommended.
  * IP: ipportfw masq support (EXPERIMENTAL)
  (CONFIG_IP_MASQUERADE_IPPORTFW) [Y/m/n/?]
    - YES: Enables IPPORTFW which allows external computers on
    the Internet to directly communicate to specified internal
    MASQed machines.  This feature is typically used to access
    internal SMTP, TELNET, and WWW servers.  FTP port forwarding
will need an additional patch as described in the FAQ section of
the MASQ HOWTO.  Additional information on port forwarding is
available in the Forwards section of this HOWTO.
  * IP: ip fwmark masq-forwarding support (EXPERIMENTAL)
  (CONFIG_IP_MASQUERADE_MFW) [Y/m/n/?]
    - OPTIONAL:  This is a new method of doing PORTFW.  With this option,
    IPCHAINS can mark packets that should have additional work on.
    Using a UserSpace tool, much like IPMASQADM or IPPORFW, IPCHAINS
    would then automaticaly re-address the packets. Currently, this
    code is less tested than PORTFW but it looks promising.  For now,
    the recommended method is to use IPMASQADM and IPPORTFW.  If you
    have thoughts on MFW, please email me.
  * IP: optimize as router not host (CONFIG_IP_ROUTER) [Y/n/?]
    - YES:  This optimizes the kernel for the network subsystem though
    it isn't known if it makes a siginificant performance difference.
  * IP: tunneling (CONFIG_NET_IPIP) [N/y/m/?]
    - NO: This OPTIONAL section is for IPIP tunnels through IP Masq.
    If you need tunneling/*** functionality, it is recommended to
    use either GRE or IPSEC tunnels.
  * IP: GRE tunnels over IP (CONFIG_NET_IPGRE) [N/y/m/?]
    - NO:   This OPTIONAL selection is to enable PPTP and
    GRE tunnels through the IP MASQ box
    -- Non-MASQ options skipped --
  * IP: TCP syncookie support (not enabled per default)
  (CONFIG_SYN_COOKIES) [Y/n/?]
    - YES: HIGHLY recommended for basic TCP/IP network security
    -- Non-MASQ options skipped --
  * IP: Allow large windows (not recommended if <16Mb of memory) *
  (CONFIG_SKB_LARGE) [Y/n/?]
    - YES:  This is recommended to optimize Linux's TCP window
    -- Non-MASQ options skipped --
  * Network device support (CONFIG_NETDEVICES) [Y/n/?]
    - YES: Enables the Linux Network device sublayer
    -- Non-MASQ options skipped --
  * Dummy net driver support (CONFIG_DUMMY) [M/n/y/?]
    - YES:  Though OPTIONAL, this option can help when debugging problems
  == Don't forget to compile in support for your network card !! ==
    -- Non-MASQ options skipped --
  == Don't forget to compile in support for PPP/SLIP if you use a modem or
     use a PPPoE DSL modem ==
    -- Non-MASQ options skipped --
  * /proc filesystem support (CONFIG_PROC_FS) [Y/n/?]
    - YES:  Required to enable the Linux network forwarding system



(2)重新編譯了核心以後,應該通過以下命令重新編譯並安裝IP僞裝模塊:

make modules; make modules_install

3.3 IP端口轉發機制的建立過程

現在需要增加適當的轉發機制,從而將數據報文轉發到合適的機器上去。首先要注意的是IPFWADM已經不再是2.1.x 和2.2.x核心中控制IP僞裝規則的工具,這些核心現在使用的工具是IPCHAINS。

(1)首先根據以下的規則創建/etc/rc.d/rc.firewall文件。

 

/sbin/depmod -a
/sbin/modprobe ip_masq_ftp
/sbin/modprobe ip_masq_portfw.o
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_always_defrag
/sbin/ipchains -M -S 7200 10 160
/sbin/ipchains -P forward DENY
/sbin/ipchains -A forward -s 10.1.1.0/24 -j MASQ
/usr/sbin/ipmasqadm portfw -f
#port forwarding strategy
#port forward the packet of interface 192.9.200.56 to 10.1.1.2 (server2)
#telnet service:port 23
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 23 -R 10.1.1.2 23 -p 1
#ftp service:port 21
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 21 -R 10.1.1.2 21 -p 1
#www service:port 80
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 80 -R 10.1.1.2 80 -p 1
#port forward the packet of interface 192.9.200.56 to 10.1.1.3 (server3)
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 23 -R 10.1.1.3 23 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 21 -R 10.1.1.3 21 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 80 -R 10.1.1.3 80 -p 1
#port forward the packet of interface 192.9.200.56 to 10.1.1.4 (server4)
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 23 -R 10.1.1.4 23 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 21 -R 10.1.1.4 21 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 80 -R 10.1.1.4 80 -p 1



(2)在編輯完/etc/rc.d/rc.firewall文件後,運行 chmod 700 /etc/rc.d/rc.firewall命令使該文件變爲可執行的。

(3)在 /etc/rc.d/rc.local文件中增加一行來在每次重啓以後激活IP僞裝模塊。

 

#rc.firewall script - Start IPMASQ and the firewall
/etc/rc.d/rc.firewall.




在依次完成了3.2和3.3小節的操作以後,實際上已經建立起了一個基於Round-Robin調度算法的集羣系統,如果用戶想根據計算機性能的不同爲之賦相應的權重,只需修改rc.firewall中的規則即可。例如,如果要把server2(10.1.1.2)的權重改爲2,只須將原來的規則:
 

/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 23 -R 10.1.1.2 23 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 21 -R 10.1.1.2 21 -p 1
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 80 -R 10.1.1.2 80 -p 1


改爲:
 

/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 23 -R 10.1.1.2 23 -p 2
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 21 -R 10.1.1.2 21 -p 2
/usr/sbin/ipmasqadm portfw -a -P tcp -L 192.9.200.56 80 -R 10.1.1.2 80 -p 2



這樣就可以了。

3.4 建立實現動態負載平衡的應用程序

該應用程序監視集羣中的各個實際服務器的負載情況,並將用戶的請求轉發到負載最輕的實際服務器上。具體的實現請參考文章後半部分有關調度模塊的實現方法

4.設計原理

本集羣系統實現了IP級的負載平衡。當客戶向平衡器發送一個請求報文時,在平衡器的IP層對此請求報文的目標地址進行了替換工作,將目標地址替換爲內部網中的實際服務器中的負載最輕的機器的IP地址。然後將此報文再次轉發出去。當內部網中的實際服務器將請求處理完了以後,它將請求迴應發向平衡器,平衡器再次在IP層將目標地址替換爲發出請求的外部網中的客戶的IP地址,然後將此報文再次轉發到客戶。

對目標地址進行替換的工作是在操作系統的核心中實現的,而選取負載最輕的機器的IP地址是在應用層實現的。之所以這樣做是因爲在應用層取負載數據可以提高系統的可擴展性,當需要向內部網中增加一臺新的實際服務器時,只需要在應用程序的數組變量中增加一項就可以了;而且在應用層可以靈活地決定調度策略,可以採用靜態的調度策略如Round Robin、Weighted Round Robin等,也可以採用動態的調度策略如Least Connection 、Weighted Least Connection等。對IP報文進行目標地址改寫的工作主要在覈心完成,這是因爲這樣速度很快,省掉了從用戶到核心的通訊過程。

當外部網中的客戶向負載平衡器發出一個服務請求(如www、ftp、telnet等) 時,從這個請求中可以獲得外部網機器的IP地址和端口號(laddr ,lport),以及平衡器的IP地址,根據這些信息查詢IP端口轉發雙向鏈表看是否有匹配(laddr, lport)的表項存在,如果存在的話,就取出該表項中的(raddr, rport)的值,即內部網中機器的IP地址和端口號,並且替換IP包的目標地址和端口號爲(raddr, rport),再將此IP包重新發送到內部網中的對應機器上去。如果沒有對應表項,則創建新的IP端口轉發表項,以及對應的IP僞裝表項。再進行目標地址替換和包重發的工作。

5.各模塊功能

本負載平衡系統主要分爲IP僞裝模塊、IP端口轉發模塊和調度模塊,其中IP僞裝模塊和IP端口轉發模塊都是在IP層實現的,在Linux源代碼所在目錄下都可以找到它們對應的程序。而調度模塊是在應用層實現的。

模塊名 標識符 說明
ip僞裝模塊 ip_masq 對ip報頭進行改寫,對ip報文進行轉發。
ip端口轉發模塊 ip_portfw 接收外界的請求,根據調度算法決定將ip報文轉發到哪一臺實際服務器上。
調度模塊 sched 根據負載信息收集模塊確定的負載最輕的機器的地址將用戶請求轉發到負載最輕的機器上。
模塊名 標識符 說明
ip僞裝模塊 ip_masq 對ip報頭進行改寫,對ip報文進行轉發。
ip端口轉發模塊 ip_portfw 接收外界的請求,根據調度算法決定將ip報文轉發到哪一臺實際服務器上。
調度模塊 sched 根據負載信息收集模塊確定的負載最輕的機器的地址將用戶請求轉發到負載最輕的機器上。
模塊名 標識符 說明
ip僞裝模塊 ip_masq 對ip報頭進行改寫,對ip報文進行轉發。
ip端口轉發模塊 ip_portfw 接收外界的請求,根據調度算法決定將ip報文轉發到哪一臺實際服務器上。
調度模塊 sched 根據負載信息收集模塊確定的負載最輕的機器的地址將用戶請求轉發到負載最輕的機器上。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章