nginx+keepalived实现高可用负载均衡

一、nginx+keepalived-高可用负载均衡

规划

keepalived-master   192.168.100.11

keepalived-backup   192.168.100.12

VIP     192.168.100.20

web-nginx   192.168.100.14

配置nginx负载均衡

安装nginx

master 和 backup都要安装(后端web服务器可选择httpd或者nginx,本例后端web服务器使用nginx)

yum install -y nginx

修改nginx配置文件

# master和backup服务器都要修改
# vim /etc/nginx/conf.d/lb.conf     # 增加子配置文件lb.conf-master和backup保持一致
# 注意主配置文件和子配置文件监听端口不能冲突

upstream  pythonwebs {
    server 10.0.0.14   weight=2 max_fails=2 fail_timeout=2;
}


server {
    listen 80;
    server_name _;
    access_log /var/log/nginx/access.log main;

    location / {
        proxy_pass http://pythonwebs;
        proxy_redirect default;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

设置完成,备份的第二台代理服务器也同样的设置; 至此,nginx实现负载均衡功能已经设置完成。访问两台代理服务器,都会自动跳转到两台后台真实服务器上。

配置keepalived

master和backup都要安装

yum -y install keepalived

备份keepalived配置文件

keepalived-master配置文件

#  vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id keep-master
}

vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx.sh"
   interval 5
}

vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        192.168.100.20
    }

    track_script {
      check_nginx
    }
}

nginx检测脚本

# vim  check_nginx.sh  # nginx检测脚本
#!/bin/bash
#检查http是否在线
curl -I http://localhost &> /dev/null
if [ $? -ne 0 ]
  then
      systemctl stop keepalived
  # else
fi

keepalived-backup配置文件

#  vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id keep-slave
}

vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx.sh"
   interval 5
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 50
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        192.168.100.20
    }

    track_script {
      check_nginx
    }
}

nginx检测脚本

# vim  check_nginx.sh  # nginx检测脚本
#!/bin/bash
#检查http是否在线
curl -I http://localhost &> /dev/null
if [ $? -ne 0 ]
  then
      systemctl stop keepalived
  # else
fi

至此keepalived+nginx高可用集群搭建完毕,浏览器访问vip:192.168.100.20验证即可

本文作者:dqforgive-sudo

本文链接:https://www.cnblogs.com/dqforgive-sudo/p/16156274.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   dqforgive-sudo  阅读(374)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.