阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Keepalived高可用Nginx

218次阅读
没有评论

共计 3229 个字符,预计需要花费 9 分钟才能阅读完成。

实验拓扑:

Keepalived 高可用 Nginx

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

Keepalived 高可用 Nginx

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 主机上

Keepalived 高可用 Nginx

4. 测试

通过 VIP 访问,可正常调度

Keepalived 高可用 Nginx

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

Keepalived 高可用 Nginx

Keepalived 高可用 Nginx

依然可以正常调度

Keepalived 高可用 Nginx

一些关于 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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3229字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中