ÔõÑùÔÚLinuxÉÏÉèÖÃÈí¼þ¸ºÔØƽºâ£¨ÈçHAProxy£©
ÔõÑùÔÚlinuxÉÏÉèÖÃÈí¼þ¸ºÔØƽºâ£¨Èçhaproxy£©
µ¼ÑÔ£º
ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬¸ß¿ÉÓÃÐԺ͸ßÐÔÄÜÊÇÖÁ¹ØÖ÷ÒªµÄ¡£ÎªÁËʵÏÖ¿ÉÀ©Õ¹ÐÔºÍÈÝ´íÐÔ£¬¾³£ÐèҪʹÓøºÔØƽºâÆ÷À´·Ö·¢ÍøÂçÁ÷Á¿µ½¶à¸öЧÀÍÆ÷¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxÉÏÉèÖÃÈí¼þ¸ºÔØƽºâ£¬ÒÔHAProxyΪÀý£¬Í¬Ê±Ìṩ´úÂëʾÀý¡£
Ò»¡¢×°ÖúÍÉèÖÃHAProxy
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃHAProxyÈí¼þ¡£ÔÚUbuntuÉÏ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install haproxy
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê±Ïºó£¬ÎÒÃÇÐèÒª¶ÔHAProxy¾ÙÐÐÉèÖ᣷¿ªÉèÖÃÎļþ/etc/haproxy/haproxy.cfg£¬Ê¹ÓÃÎı¾±à¼Æ÷¾ÙÐÐÐ޸ġ£
sudo vi /etc/haproxy/haproxy.cfg
µÇ¼ºó¸´ÖÆ
ÔÚÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒªÉèÖüàÌýÆ÷ºÍºó¶ËЧÀÍÆ÷¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖÃÎļþµÄÄÚÈÝ£º
global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin stats timeout 30s maxconn 4096 user haproxy group haproxy daemon defaults log global mode http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 frontend http_front bind *:80 default_backend http_back backend http_back balance roundrobin server web1 192.168.0.101:80 check server web2 192.168.0.102:80 check
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏÉèÖÃÖУ¬ÎÒÃÇÉèÖÃÁËÒ»¸ö¼àÌý¶Ë¿ÚΪ80µÄhttpÇ°¶Ë£¬½«Á÷Á¿·Ö·¢µ½ÃûΪhttp_backµÄºó¶ËЧÀÍÆ÷¡£Í¨¹ýbalanceÖ¸ÁÎÒÃÇ¿ÉÒÔÑ¡Ôñ¸ºÔØƽºâËã·¨£¬Èçroundrobin¡¢leastconnµÈ¡£ÔÚʾÀýÖУ¬ÎÒÃÇʹÓÃroundrobinËã·¨¾ÙÐÐÂÖѯ·Ö·¢¡£Í¬Ê±ÎÒÃǽç˵ÁËÁ½¸öºó¶ËЧÀÍÆ÷£¬»®·ÖΪ192.168.0.101:80ºÍ192.168.0.102:80¡£checkÖ¸ÁîÌåÏÖ¼ì²éºó¶ËЧÀÍÆ÷µÄ¿µ½¡×´Ì¬¡£
Íê³ÉÉèÖúó£¬ÉúÑIJ¢Í˳ö¡£
¶þ¡¢Æô¶¯ºÍ¼à¿ØHAProxy
ÔÚÍê³ÉÉèÖúó£¬ÎÒÃÇÐèÒªÆô¶¯²¢¼à¿ØHAProxyЧÀÍ¡£Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯HAProxy£º
sudo service haproxy start
µÇ¼ºó¸´ÖÆ
ЧÀÍÆô¶¯ºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¼ì²éЧÀÍ״̬£º
sudo service haproxy status
µÇ¼ºó¸´ÖÆ
ʹÓÃHAProxyµÄͳ¼Æ±¨¸æ¹¦Ð§£¬¿ÉÒÔʵʱµØ¼à¿ØÁ÷Á¿·Ö·¢ÇéÐΡ£ÔÚÉèÖÃÎļþµÄglobal¶ÎÖУ¬ÎÒÃÇÉèÖÃÁËstats socketºÍstats timeout£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî»á¼ûͳ¼Æ±¨¸æ£º
sudo socat stdio /run/haproxy/admin.sock
µÇ¼ºó¸´ÖÆ
±ðµÄ£¬»¹¿ÉÒÔͨ¹ýÔÚä¯ÀÀÆ÷Öлá¼ûhttp://localhost:1936À´Éó²éͼÐλ¯µÄHAProxyͳ¼Æ±¨¸æ¡£
Èý¡¢Ê¹ÓÃHAProxy¾ÙÐиºÔØƽºâ
ÔÚÉèÖÃÍê³É²¢Æô¶¯HAProxyЧÀͺó£¬ÎÒÃÇ¿ÉÒÔʹÓøºÔØƽºâÆ÷À´·Ö·¢Á÷Á¿µ½¶à¸öºó¶ËЧÀÍÆ÷¡£ÀýÈ磬ÔÚÍâµØÔËÐеÄÓ¦ÓóÌÐò¼àÌý¶Ë¿ÚΪ8080£¬Ï£Íûͨ¹ýHAProxy¾ÙÐиºÔØƽºâ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚä¯ÀÀÆ÷Öлá¼ûhttp://localhost:80À´»á¼ûÓ¦ÓóÌÐò¡£
ÔÚÕâÖÖÉèÖÃÏ£¬HAProxy½«Æ¾Ö¤ËùÑ¡µÄ¸ºÔØƽºâËã·¨£¬½«Á÷Á¿ÇëÇó´ò°üת·¢µ½ºó¶ËЧÀÍÆ÷ÉϵÄÓ¦ÓóÌÐò¡£
½áÂÛ£º
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLinuxÉÏÉèÖÃÈí¼þ¸ºÔØƽºâ£¬ÒÔHAProxyΪÀý¡£Í¨¹ý×°ÖúÍÉèÖÃHAProxy£¬ÎÒÃÇ¿ÉÒÔʵÏָ߿ÉÓÃÐԺ͸ßÐÔÄܵÄÍøÂçÁ÷Á¿·Ö·¢¡£±¾ÎÄÌṩÁË´úÂëʾÀýºÍ¼à¿ØÖ¸ÄÏ£¬×ÊÖú¶ÁÕß¿ìËÙÉÏÊÖʹÓÃHAProxy¡£¸ºÔØƽºâÆ÷µÄʹÓÿÉÒÔÏÔÖøÌáÉýÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬ÊÇÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖв»¿É»òȱµÄÒ»»·¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖÃÈí¼þ¸ºÔØƽºâ£¨ÈçHAProxy£©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡