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

Keepalived+Nginx搭建高可用负载均衡集群

146次阅读
没有评论

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

前言

因生产环境需要,现需要搭建一个前端为 Nginx 的 HA 双向互备、后端为 Nginx 代理的 loadbalance 负载均衡集群。nginx 进程基于于 Master+Slave(worker) 多进程模型,自身具有非常稳定的子进程管理功能。在 Master 进程分配模式下,Master 进程永远不进行业务处理,只是进行任务分发,从而达到 Master 进程的存活高可靠性,Slave(worker) 进程所有的业务信号都由主进程发出,Slave(worker) 进程所有的超时任务都会被 Master 中止,属于非阻塞式任务模型。Keepalived 是 Linux 下面实现 VRRP 备份路由的高可靠性运行件。基于 Keepalived 设计的服务模式能够真正做到主服务器和备份服务器故障时 IP 瞬间无缝交接。

此架构需考虑的问题

1)Master 没挂,则 Master 占有 vip 且 nginx 运行在 Master 上

2)Master 挂了,则 backup 抢占 vip 且在 backup 上运行 nginx 服务

3)如果 master 服务器上的 nginx 服务挂了,则 vip 资源转移到 backup 服务器上

4)检测后端服务器的健康状态

Master 和 Backup 两边都开启 nginx 服务,无论 Master 还是 Backup,当其中的一个 keepalived 服务停止后,vip 都会漂移到 keepalived 服务还在的节点上,

如果要想使 nginx 服务挂了,vip 也漂移到另一个节点,则必须用脚本或者在配置文件里面用 shell 命令来控制。

以下实验过程由难及易,易于上手,便于操作。

一、前期环境准备

1.    系统环境均为 CentOS release 6.5 (Final)32 位,均在虚拟机上成功实现。

Keepalived+Nginx 搭建高可用负载均衡集群

所有的软件包均为 yum 方式安装,yum 源均在用阿里云服务器所使用的。

Keepalived+Nginx 搭建高可用负载均衡集群

为避免后续不必要的麻烦,全都关闭防火墙

本实验结构的拓扑图如下:

Keepalived+Nginx 搭建高可用负载均衡集群

机器 IP 规划如下:

Keepalived+Nginx 搭建高可用负载均衡集群

二、后端 apache 的安装配置

apache1:

1、安装

[root@apache1 ~]# yum install -y httpd

2、建立测试网页文件

echo “this is apache1” >/var/www/html/index.html

3、开启服务

service httpd start

apache2:

1、安装

yum -y install httpd

2、建立测试网页文件

echo “this is apache2” >/var/www/html/index.html

3、开启服务

service httpd start

Nginx 安装与配置

1 安装环境所需依赖包

yum -y install gcc gcc+ gcc-c++

yum install popt-devel opensslopenssl-devel libssl-dev libnl-devel popt-devel

yum install -y net-snmp.x86_64net-snmp-devel.x86_64

2. 安装 nginx

yum install -y nginx

3. 修改 nginx 的配置文件

yum install -y nginx

Keepalived+Nginx 搭建高可用负载均衡集群

创建自定义配置文件

mkdir /etc/nginx/conf.d/extra/

vim /etc/nginx/conf.d/extra/yang.conf

Keepalived+Nginx 搭建高可用负载均衡集群

4. 启动 nginx

service nginx start

此时可以在浏览器测试一下,输入 IP:192.168.1.135

Keepalived+Nginx 搭建高可用负载均衡集群

刷新一下

Keepalived+Nginx 搭建高可用负载均衡集群

这一台 nginx 已经配置成功,在另外一台机器上同样的方式配置 nginx,这里不再赘述。

以上配置文件里的内容虽然稀少,但都是重要部分,以后用的到的功能逐渐添加。

keepalived 的安装与配置

机器上安装 keepalived

yum install -y keepalived

2. 修改 keepalived 配置文件

vim/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
  notification_email {
    yangry@linuxidc.com
  }
  notification_email_fromyangry@linuxidc.com
  smtp_server mail.linuxidc.com
  smtp_connect_timeout 30
  router_id LVS_MASTER1  #
表示运行 keepalived 服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_script chk_http_port {
script “/usr/local/keepalived/nginx.sh”####
检测 nginx 状态的脚本链接
interval 2
weight 2
}
vrrp_instance VI_2 {#vrrp
实例
    stateMASTER    #MASTER/BACKUP
    interface eth1  ####HA 
监测网络接口
    virtual_router_id 51  #
虚拟路由标识,是一个数字,同一个 VRRP 实例使用唯一的标识,master 和 backup 要一样
    priority100          #
用于主从模式,优先级主高于 100,从低于 100
    advert_int1          #
主备之间的通告间隔秒数
    authentication{#
认证用于主从模式,mater 和 backup 配置一样
      auth_type PASS          ###
主备切换时的验证
      auth_pass 1111          #
密码
    }
track_script {
chk_http_port ### 
执行监控的服务
}
    virtual_ipaddress {
       
 192.168.1.100 dev eth1 label eth1:0  ###########
虚拟 ip 
    }
}

Keepalived+Nginx 搭建高可用负载均衡集群

3. 编写 nginx 检测脚本

vim /usr/local/keepalived/nginx.sh

if [`ps -C nginx –no-header |wc -l` -eq 0];then

killall keepalived

fi

一定要记得赋予其可执行权限哦

chmod +x /usr/local/keepalived/nginx.sh

在另外一台 backup 机器上做同样操作,注意 /etc/keepalived/keepalived.conf 要修改的地方如下:

state BACKUP

priority 99(一定要比 master 的值要小)

手动添加一个虚拟 IP

分别在 master 和 backup 上执行以下命令:

ifconfig eth1:0 192.168.1.100netmask 255.255.255.0 up

然后查看,就会有两个 IP 了

Keepalived+Nginx 搭建高可用负载均衡集群

在两台机器上分别重启 nginx 和 keepalived 做测试

service nginx restart

service keepalived restart

1. 在 master 上可以看到

Keepalived+Nginx 搭建高可用负载均衡集群

在 backup 上可以看到

Keepalived+Nginx 搭建高可用负载均衡集群

看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了。

2. 当我手动把 master 上的 nginx 停掉,可以看到 master 日志

Keepalived+Nginx 搭建高可用负载均衡集群

在看此时的 backup 日志

Keepalived+Nginx 搭建高可用负载均衡集群

显然已经成功把 vip 接管过来.

自此,本实验已经成功完成。

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

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

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