ÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõķ´ÏòÊðÀí£¨ÈçNginx£©
ÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõķ´ÏòÊðÀí£¨Èçnginx£©
ͻ񻣼
·´ÏòÊðÀíÊÇÔÚÍøÂç¼Ü¹¹Öг£ÓõÄÒ»ÖÖÊÖÒÕ£¬¿ÉÒÔÌá¸ßЧÀ͵ĸºÔØƽºâÄÜÁ¦ºÍÈÝ´íÄÜÁ¦¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxϵͳÉÏÉèÖø߿ÉÓõķ´ÏòÊðÀí£¬ÒÔNginxΪÀý¡£
×°ÖÃNginx
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚLinuxϵͳÉÏ×°ÖÃNginx¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´Íê³É×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖ÷´ÏòÊðÀí
ÔÚNginxÖÐÉèÖ÷´ÏòÊðÀíºÜÊǼòÆÓ¡£±à¼NginxµÄÉèÖÃÎļþ/etc/nginx/nginx.conf£¬ÔÚhttp¿éÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪbackendµÄupstream¿é£¬ÆäÖÐÁгöÁËÐèÒªÊðÀíµÄºó¶ËЧÀÍÆ÷¡£server¿éÖ¸¶¨Á˼àÌýµÄ¶Ë¿ÚºÍÓòÃû£¬²¢½«ÇëÇóת·¢¸øbackend¡£
ÉèÖøºÔØƽºâÕ½ÂÔ
ÔÚÉÏÊöÉèÖÃÖУ¬Ä¬ÈÏʹÓõÄÊÇÂÖѯ£¨Round-Robin£©µÄ¸ºÔØƽºâÕ½ÂÔ¡£¼´Ã¿¸öÇëÇóÒÀ´Î°´Ë³Ðò·ÖÅɵ½ºó¶ËЧÀÍÆ÷¡£ÈôÊÇÐèÒª¸üÖØ´óµÄ¸ºÔØƽºâÕ½ÂÔ£¬¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐÉèÖá£
ÒÔÏÂÊÇһЩ³£¼ûµÄ¸ºÔØƽºâÕ½ÂÔʾÀý£º
a. IP Hash
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com; }
µÇ¼ºó¸´ÖÆ
IP HashÕ½ÂÔ»áƾ֤¿Í»§¶ËµÄIPµØµã½«ÆäÇëÇó·ÖÅɵ½Ìض¨µÄºó¶ËЧÀÍÆ÷£¬ÕâÑù¿ÉÒÔ¼á³Öͳһ¸ö¿Í»§¶ËµÄÇëÇ󶼷¢Ë͵½Í³Ò»Ì¨Ð§ÀÍÆ÷ÉÏ¡£
b. Least Connections
upstream backend { least_conn; server backend1.example.com; server backend2.example.com; server backend3.example.com; }
µÇ¼ºó¸´ÖÆ
Least ConnectionsÕ½ÂԻὫÇëÇó·¢Ë͵½Ä¿½ñÅþÁ¬Êý×îÉÙµÄЧÀÍÆ÷ÉÏ£¬ÒÔʵÏÖ¸ºÔØƽºâ¡£
ÖØÔØNginxÉèÖÃ
Íê³ÉÉèÖúó£¬ÐèÒªÖØÔØNginxÉèÖÃÎļþÒÔʹ¸ü¸ÄÉúЧ¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÍê³É²Ù×÷£º
sudo nginx -s reload
µÇ¼ºó¸´ÖÆ
¸ß¿ÉÓÃÐÔÉèÖÃ
ΪÁËʵÏָ߿ÉÓÃÐÔ£¬¿ÉÒÔ½«¶à¸öNginx½Úµã×é³ÉÒ»¸ö¼¯Èº£¬²¢Ê¹ÓÃKeepalivedµÈ¹¤¾ßÀ´ÊµÏÖ¹ÊÕÏתÒÆ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬¼ÙÉ輯ȺÖÐÓÐÁ½¸ö½Úµã£º
sudo apt-get install keepalived
µÇ¼ºó¸´ÖÆ
ÔÚÿ¸ö½ÚµãÉÏ£¬¶¼ÐèÒª±à¼KeepalivedµÄÉèÖÃÎļþ£¬Í¨³£Î»ÓÚ/etc/keepalived/keepalived.conf¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
global_defs { router_id LB_NODE1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 virtual_ipaddress { 192.168.1.100 } }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬stateÖ¸¶¨Á˽ڵãµÄ״̬£¨MASTER»òBACKUP£©£¬virtual_router_idÖ¸¶¨ÁËÐéÄâ·ÓÉÆ÷ID£¬virtual_ipaddressÖ¸¶¨ÁËÐéÄâIPµØµã¡£
×îºó£¬ÖØÆôKeepalivedЧÀÍ£¬Ê¹ÉèÖÃÉúЧ£º
sudo service keepalived restart
µÇ¼ºó¸´ÖÆ
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔÚLinuxϵͳÉÏÉèÖø߿ÉÓõķ´ÏòÊðÀíµÄ°ì·¨£¬ÒÔ¼°Ïà¹ØµÄ´úÂëʾÀý¡£Í¨¹ýÕâЩÉèÖ㬿ÉÒÔÌá¸ßЧÀ͵ĸºÔØƽºâÄÜÁ¦ºÍÈÝ´íÄÜÁ¦£¬´Ó¶øʵÏÖ¸ü¸ßµÄ¿ÉÓÃÐÔºÍÎȹÌÐÔ¡£Í¬Ê±£¬»¹ÏÈÈÝÁËÔõÑùʹÓÃKeepalivedµÈ¹¤¾ßʵÏÖ·´ÏòÊðÀíµÄ¹ÊÕÏתÒÆ¡£Ï£ÍûÕâЩÄÚÈݹØÓڴ¸ß¿ÉÓ÷´ÏòÊðÀíϵͳµÄÅóÙÃÇÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõķ´ÏòÊðÀí£¨ÈçNginx£©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡