ÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõĶ˿Úת·¢
ÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõĶ˿Úת·¢
Ò»¡¢Åä¾°¸ÅÊö
ÔÚÂþÑÜʽϵͳµÄ¼Ü¹¹ÖУ¬¶Ë¿Úת·¢ÊǷǾ³£¼ûµÄÒ»ÖÖÍøÂçÊÖÒÕ¡£Í¨¹ý¶Ë¿Úת·¢£¬¿ÉÒÔδÀ´×ÔÍⲿÍøÂçµÄÅþÁ¬ÇëÇóת·¢µ½ÄÚ²¿½ÚµãÉÏ£¬´Ó¶øʵÏÖ¶ÔÍâЧÀ͵ĸ߿ÉÓÃÐԺ͸ºÔØƽºâ¡£ÔÚLinux²Ù×÷ϵͳÉÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩ¹¤¾ßºÍÉèÖÃÀ´ÊµÏָ߿ÉÓõĶ˿Úת·¢¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃLinux²Ù×÷ϵͳÉèÖúÍÖÎÀí¸ß¿ÉÓõĶ˿Úת·¢¡£
¶þ¡¢Ê¹ÓÃiptablesʵÏֶ˿Úת·¢
iptablesÊÇLinuxϵͳÉϳ£ÓõķÀ»ðǽÈí¼þ£¬Ò²¿ÉÒÔÓÃÀ´ÊµÏֶ˿Úת·¢¹¦Ð§¡£ÒÔÏÂÊÇÒ»¸öʹÓÃiptablesʵÏֶ˿Úת·¢µÄʾÀý£º
Ê×ÏÈ£¬ÐèҪȷÈÏiptablesÈí¼þÒѾװÖÃÔÚϵͳÉÏ¡£ÔÚÖÕ¶ËÉÏÖ´ÐÐÒÔÏÂÏÂÁ
sudo apt-get install iptables
µÇ¼ºó¸´ÖÆ
ÉèÖö˿Úת·¢¹æÔò¡£¼ÙÉèÎÒÃÇÐèҪδÀ´×Ô¹«ÍøIPµÄ»á¼ûÇëÇóת·¢µ½ÄÚÍøµÄij¸ö½ÚµãÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐж˿Úת·¢ÉèÖãº
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j DNAT --to 192.168.0.10:80 sudo iptables -A FORWARD -p tcp -d 192.168.0.10 --dport 80 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬eth0ΪÍⲿÍøÂç½Ó¿Ú£¬192.168.0.10ΪÄÚÍø½ÚµãµÄIPµØµã£¬80ΪÐèҪת·¢µÄ¶Ë¿ÚºÅ¡£
ÉúÑÄÉèÖᣵ±ÒÔÉÏÉèÖÃÍê³Éºó£¬ÎÒÃÇÐèÒª½«ÉèÖÃÉúÑĵ½iptablesÉèÖÃÎļþÖУ¬ÒÔ±ãϵͳÖØÆôºó×Ô¶¯¼ÓÔØÉèÖá£Ö´ÐÐÒÔÏÂÏÂÁîÉúÑÄÉèÖãº
sudo sh -c "iptables-save > /etc/iptables.rules"
µÇ¼ºó¸´ÖÆ
Ìí¼Ó×ÔÆô¶¯¾ç±¾¡£ÎªÁËʹÉèÖÃÔÚϵͳÆô¶¯Ê±×Ô¶¯¼ÓÔØ£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö×ÔÆô¶¯¾ç±¾£¬²¢Ìí¼Óµ½ÏµÍ³µÄÆô¶¯Ð§ÀÍÖС£½¨ÉèÒ»¸öÃûΪiptables.shµÄ¾ç±¾£¬ÄÚÈÝÈçÏ£º
#!/bin/sh /sbin/iptables-restore < /etc/iptables.rules
µÇ¼ºó¸´ÖÆ
½«¾ç±¾ÉúÑĵ½/etc/init.dĿ¼Ï£¬²¢Îª¾ç±¾Ìí¼ÓÖ´ÐÐȨÏÞ£º
sudo chmod +x /etc/init.d/iptables.sh
µÇ¼ºó¸´ÖÆ
È»ºó£¬½«¾ç±¾Ìí¼Óµ½Æô¶¯Ð§ÀÍÖУº
sudo update-rc.d iptables.sh defaults 99
µÇ¼ºó¸´ÖÆ
ÖÁ´Ë£¬ÎÒÃÇÍê³ÉÁËʹÓÃiptablesʵÏֶ˿Úת·¢µÄÉèÖá£
Èý¡¢Ê¹ÓÃHAProxyʵÏָ߿ÉÓõĶ˿Úת·¢
³ýÁËʹÓÃiptablesÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃHAProxyÀ´ÊµÏָ߿ÉÓõĶ˿Úת·¢¡£HAProxyÊÇÒ»¿î¿É¿¿µÄ¡¢¸ßÐÔÄܵĸºÔØƽºâÈí¼þ£¬ÊÊÓÃÓÚÂþÑÜʽϵͳÖеĶ˿Úת·¢ÐèÇó¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHAProxyÉèÖø߿ÉÓõĶ˿Úת·¢µÄʾÀý£º
Ê×ÏÈ£¬ÐèҪȷÈÏHAProxyÈí¼þÒѾװÖÃÔÚϵͳÉÏ¡£ÔÚÖÕ¶ËÉÏÖ´ÐÐÒÔÏÂÏÂÁ
sudo apt-get install haproxy
µÇ¼ºó¸´ÖÆ
±à¼HAProxyµÄÉèÖÃÎļþ¡£·¿ªÉèÖÃÎļþ/etc/haproxy/haproxy.cfg£¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º
frontend http-in bind *:80 default_backend backend_servers backend backend_servers balance roundrobin server server1 192.168.0.10:80 check server server2 192.168.0.11:80 check
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬192.168.0.10ºÍ192.168.0.11ΪÄÚ²¿½ÚµãµÄIPµØµã£¬80ΪÐèҪת·¢µÄ¶Ë¿ÚºÅ¡£
ÖØÆôHAProxyЧÀÍ¡£Ö´ÐÐÒÔÏÂÏÂÁîÖØÆôHAProxyЧÀÍ£º
sudo service haproxy restart
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬HAProxy½«Æ¾Ö¤ÉèÖÃÎļþÖеĹæÔò£¬½«»á¼ûÇëÇóת·¢µ½ÄÚ²¿½ÚµãÉÏ£¬ÊµÏָ߿ÉÓõĶ˿Úת·¢¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚLinux²Ù×÷ϵͳÉÏÉèÖø߿ÉÓõĶ˿Úת·¢µÄÒªÁ죬»®·Öͨ¹ýiptablesºÍHAProxyÁ½ÖÖ·½·¨¾ÙÐÐʾÀý¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇóÑ¡ÔñºÏÊʵķ½·¨¾ÙÐÐÉèÖúͰ²ÅÅ¡£Í¨¹ýºÏÀíʹÓö˿Úת·¢£¬¿ÉÒÔÌá¸ßϵͳµÄ¿É¿¿ÐԺ͸ºÔØÄÜÁ¦£¬Ìṩ¸üÎȹ̺͸ßЧµÄЧÀÍ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõĶ˿Úת·¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡