zoukankan      html  css  js  c++  java
  • nginx+keepalived高可用及双主模式

    高可用有2中方式。

    1、Nginx+keepalived 主从配置

    这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠。

    2、Nginx+keepalived 双主配置

    这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境。

    1、Nginx+keepalived 主从配置

    1.环境如下

    lb-01:192.168.96.130  nginx+keepalived-master
    lb-02:192.168.96.131  nginx+keepalived-backup
    VIP:192.168.75.135
     
    

    nginx+tomcat负载均衡集群参照 nginx+tomcat负载均衡集群参照

    两台机器都要安装nginx 配置文件相同

    [html] view plain copy
     
    1. <span style="font-size:18px;">upstream myServer{      
    2.       server 192.168.96.130:8080;      
    3.       server 192.168.96.131:8080;      
    4.     }     
    5.  server {    
    6.         listen       80;    
    7.         server_name  localhost;    
    8.     
    9.         #charset koi8-r;    
    10.     
    11.         #access_log  logs/host.access.log  main;    
    12.     
    13.         location / {     
    14.           proxy_pass  http://myServer;      
    15.         }    
    16.   </span>  

    nginx结合keepalived高可用

    为什么使用keepalived呢?使用keepalived就用来做高可用的,提供虚拟VIP

    分别在2台机子上安装keepalived

    # yum install keepalived -y

    查看keepalived版本

    # keepalived -v
    Keepalived v1.2.13 (11/20,2015)

    关于2台keepalived配置文件/etc/keepalived

    在130master keepalived配置文件内容如下

    [html] view plain copy
     
    1. <span style="font-size:18px;">global_defs {  
    2.    notification_email {  
    3.      acassen@firewall.loc  
    4.      failover@firewall.loc  
    5.      sysadmin@firewall.loc  
    6.    }  
    7.    notification_email_from Alexandre.Cassen@firewall.loc  
    8.    smtp_server 192.168.200.1  
    9.    smtp_connect_timeout 30  
    10.    router_id LVS_DEVEL  
    11. }  
    12.   
    13. vrrp_script chk_http_port {  
    14.   
    15.    script "/usr/local/src/check_nginx_pid.sh"  
    16.   
    17.    interval 2      #(检测脚本执行的间隔)  
    18.   
    19.    weight 2  
    20.   
    21. }  
    22.   
    23. vrrp_instance VI_1 {  
    24.     state MASTER   # 备份服务器上将 MASTER 改为 BACKUP    
    25.     interface eno16777736  //网卡  
    26.     virtual_router_id 51   # 主、备机的virtual_router_id必须相同  
    27.     priority 100     # 主、备机取不同的优先级,主机值较大,备份机值较小  
    28.     advert_int 1  
    29.     authentication {  
    30.         auth_type PASS  
    31.         auth_pass 1111  
    32.     }  
    33.     virtual_ipaddress {  
    34.         192.168.96.138  // VRRP H虚拟地址  
    35.     }  
    36. }  
    37. </span>  


    在131-backup keepalived配置文件内容如下

    [html] view plain copy
     
    1. <span style="font-size:18px;">global_defs {  
    2.    notification_email {  
    3.      acassen@firewall.loc  
    4.      failover@firewall.loc  
    5.      sysadmin@firewall.loc  
    6.    }  
    7.    notification_email_from Alexandre.Cassen@firewall.loc  
    8.    smtp_server 192.168.200.1  
    9.    smtp_connect_timeout 30  
    10.    router_id LVS_DEVEL  
    11. }  
    12.   
    13. vrrp_script chk_http_port {  
    14.   
    15.    script "/usr/local/src/check_nginx_pid.sh"  
    16.   
    17.    interval 2      #(检测脚本执行的间隔)  
    18.   
    19.    weight 2  
    20.   
    21. }  
    22.   
    23. vrrp_instance VI_1 {  
    24.     state BACKUP  
    25.     interface eno16777736  
    26.     virtual_router_id 51  
    27.     priority 90  
    28.     advert_int 1  
    29.     authentication {  
    30.         auth_type PASS  
    31.         auth_pass 1111  
    32.     }  
    33.     virtual_ipaddress {  
    34.         192.168.96.138  
    35.     }  
    36. }</span>  

    然后分别启动2台keepalived服务

    [root@lb-01 ~]# systemctl start keepalived.service
    
    [root@lb-02 ~]# systemctl start keepalived.service

    查看虚拟VIP

    130-master机器查看

    从上面可以看到虚拟VIP地址192.168.96.138


    131-backup机器查看

    从上面可以看到没有虚拟VIP地址

    测试访问虚拟VIP

    打开浏览器访问 

    此时虚拟VIP可以轮询访问了

    模拟故障

    把130-master nginx和keepalived停止查看是否还能正常提供服务

    [root@lb-01 sbin]# ./nginx -s stop
    [root@lb-01 sbin]# systemctl stop keepalived.service

    此时虚拟VIP已经不再master上了

    客户端打开浏览器访问是否正常访问

    130nginx和keepalived挂了也不影响服务

    查看131backup机器VIP情况

    此时虚拟VIP 已经在131机器上

    那么如何实现nginx+keepalived双主模式呢?

    1.其实只是需要更改下keepalived配置文件即可,配置文件实例如下

    增加新的VIP192.168.96.139,192.168.75.138是130机器上主虚拟VIP,192.168.96.139是131机器上主虚拟VIP

    130的keepalived配置文件内容如下

    [html] view plain copy
     
    1. ! Configuration File for keepalived  
    2.   
    3. global_defs {  
    4.    notification_email {  
    5.      acassen@firewall.loc  
    6.      failover@firewall.loc  
    7.      sysadmin@firewall.loc  
    8.    }  
    9.    notification_email_from Alexandre.Cassen@firewall.loc  
    10.    smtp_server 192.168.200.1  
    11.    smtp_connect_timeout 30  
    12.    router_id LVS_DEVEL  
    13. }  
    14.   
    15. vrrp_script chk_http_port {  
    16.   
    17.    script "/usr/local/src/check_nginx_pid.sh"  
    18.   
    19.    interval 2      #(检测脚本执行的间隔)  
    20.   
    21.    weight 2  
    22.   
    23. }  
    24.   
    25. vrrp_instance VI_1 {  
    26.     state MASTER  
    27.     interface eno16777736  
    28.     virtual_router_id 51  
    29.     priority 100  
    30.     advert_int 1  
    31.     authentication {  
    32.         auth_type PASS  
    33.         auth_pass 1111  
    34.     }  
    35.     virtual_ipaddress {  
    36.         192.168.96.138  
    37.     }  
    38. }  
    39.   
    40. vrrp_instance VI_2 {  
    41.     state BACKUP  
    42.     interface eno16777736  
    43.     virtual_router_id 52  
    44.     priority 90  
    45.     advert_int 1  
    46.     authentication {  
    47.         auth_type PASS  
    48.         auth_pass 1111  
    49.     }  
    50.     virtual_ipaddress {  
    51.         192.168.96.139  
    52.     }  
    53. }  


    131的keepalived配置文件内容如下

    [html] view plain copy
     
    1. ! Configuration File for keepalived  
    2.   
    3. global_defs {  
    4.    notification_email {  
    5.      acassen@firewall.loc  
    6.      failover@firewall.loc  
    7.      sysadmin@firewall.loc  
    8.    }  
    9.    notification_email_from Alexandre.Cassen@firewall.loc  
    10.    smtp_server 192.168.200.1  
    11.    smtp_connect_timeout 30  
    12.    router_id LVS_DEVEL  
    13. }  
    14.   
    15. vrrp_script chk_http_port {  
    16.   
    17.    script "/usr/local/src/check_nginx_pid.sh"  
    18.   
    19.    interval 2      #(检测脚本执行的间隔)  
    20.   
    21.    weight 2  
    22.   
    23. }  
    24.   
    25. vrrp_instance VI_1 {  
    26.     state BACKUP  
    27.     interface eno16777736  
    28.     virtual_router_id 51  
    29.     priority 90  
    30.     advert_int 1  
    31.     authentication {  
    32.         auth_type PASS  
    33.         auth_pass 1111  
    34.     }  
    35.     virtual_ipaddress {  
    36.         192.168.96.138  
    37.     }  
    38. }  
    39.   
    40.   
    41. vrrp_instance VI_2 {  
    42.     state MASTER  
    43.     interface eno16777736  
    44.     virtual_router_id 52  
    45.     priority 100  
    46.     advert_int 1  
    47.     authentication {  
    48.         auth_type PASS  
    49.         auth_pass 1111  
    50.     }  
    51.     virtual_ipaddress {  
    52.         192.168.96.139  
    53.     }  
    54. }  

    分别启动2台lb上nginx和keepalived服务

    [root@lb-01 sbin]# ./nginx
    [root@lb-01 sbin]# systemctl start keepalived.service

    130查看虚拟ip

    131查看虚拟ip

    客户端测试访问虚拟VIP

    访问虚拟VIP:192.168.96.138结果如下

    访问虚拟VIP:192.168.96.139结果如下

    模拟故障

    把130服务停止

    查看130虚拟VIP是否存在结果如下



    没了

    测试访问虚拟VIP**

    从上面结果可以看到,即使130机器发生了故障也不影响使用,这样也利用131资源了

    此时查看一下131虚拟ip结果情况如下

    注:此时虚拟VIP地址都已经在lb-02机器上了

     

  • 相关阅读:
    基于Metaweblog API 接口一键发布到国内外主流博客平台
    uva144 Student Grants
    Uva 10452
    Uva 439 Knight Moves
    Uva 352 The Seasonal War
    switch语句
    java——基础知识
    我的lua学习2
    codeforces 431 D. Random Task 组合数学
    codeforces 285 D. Permutation Sum 状压 dfs打表
  • 原文地址:https://www.cnblogs.com/felixzh/p/8690794.html
Copyright © 2011-2022 走看看