你好,游客 登录 注册 搜索
背景:
阅读新闻

Keepalived高可用Nginx

[日期:2017-03-27] 来源:Linux社区  作者:zhaizhai [字体: ]

实验拓扑:

1.在两台realserver上配置web服务

2.keepalived配置
master的配置
global_defs {
  notification_email {
      root@localhost
  }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node1
}
 
vrrp_script chk_down {    #keepalived控制脚本       
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"      #脚本内容
    interval 2
    weight -6
}
 
vrrp_script chk_nginx {            #检测nginx服务脚本
    script "killall -0 nginx && exit 0 || exit 1"  #脚本内容     
    interval 2
    weight -6
}
 
vrrp_instance VI_1 {    #配置实例
    state MASTER
    interface eno16777736
    virtual_router_id 88
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {
        10.1.88.88/16 dev eno16777736 label eno16777736:0
    }
 
    track_script {    #调用脚本
        chk_down
        chk_nginx
    }
 
    notify_master "/etc/keepalived/notify.sh master"    #定义通告脚本
    notify_backup "/etc/keepalived/notify.sh backup"   
    notify_fault  "/etc/keepalived/notify.sh fault"
}

backup的配置:

global_defs {
  notification_email {
      root@localhost
  }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node1
}
 
vrrp_script chk_down {
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 2
    weight -6
}
 
vrrp_script chk_nginx {
    script "killall -0 nginx && exit 0 || exit 1"
    interval 2
    weight -6
}
 
vrrp_instance VI_1 {
    state BACKUP   
    interface eno16777736
    virtual_router_id 88
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {
        10.1.88.88/16 dev eno16777736 label eno16777736:0
    }
 
    track_script {
        chk_down
        chk_nginx
    }
 
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault  "/etc/keepalived/notify.sh fault"
}

3.nginx调度器配置
两台nginx调度器的配置文件
#http段中的配置
http {
    include      /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush    on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
 
    upstream webserver {
        server 10.1.68.5 weight=1 max_fails=2;
        server 10.1.68.6 weight=1 max_fails=2;
    }
 
    server {
        listen 80;
        server_name localhost;
 
        location / {
            index index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://webserver;
        }
    }
 
}

在两台调度器上同时启动nginx和keepalived,此时VIP在nginx1主机上

4.测试

通过VIP访问,可正常调度

停止调度器1上面的nginx服务,VIP漂移至nginx2主机上

依然可以正常调度

一些关于Keepalived相关教程集合

CentOS 7下Keepalived + HAProxy 搭建配置详解  http://www.linuxidc.com/Linux/2017-03/141593.htm

Keepalived高可用集群应用场景与配置 http://www.linuxidc.com/Linux/2017-03/141866.htm

Nginx+Keepalived实现站点高可用  http://www.linuxidc.com/Linux/2016-12/137883.htm

Nginx+Keepalived实现站点高可用(负载均衡)  http://www.linuxidc.com/Linux/2016-12/138221.htm

构建高可用集群Keepalived+Haproxy负载均衡 http://www.linuxidc.com/Linux/2016-12/138917.htm

CentOS6.5下 Keepalived高可用服务单实例配置 http://www.linuxidc.com/Linux/2016-12/138110.htm

Keepalived安装与配置 http://www.linuxidc.com/Linux/2017-02/140421.htm

Linux下Keepalived服务安装文档  http://www.linuxidc.com/Linux/2017-03/141441.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-03/142236.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款