×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùÔÚ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£©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ