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

本文涉及的产品
应用型负载均衡 ALB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
简介: 上篇文章介绍了nginx作为反向代理/负载均衡服务器,假如nginx出现宕机的话,那么将无法转发请求到我们后端的网站服务器,现在介绍nginx+keepalived实现前端反向代理/负载均衡高可用架构的搭建! 实验环境如下需要四台服务器,其实严格的讲只需要三台就可以了,后端的网站服务器可以是单台也可以是多台,说一下我这里四台机器的软件包都是安装操作系统时全部安装的。
上篇文章介绍了nginx作为反向代理/负载均衡服务器,假如nginx出现宕机的话,那么将无法转发请求到我们后端的网站服务器,现在介绍nginx+keepalived实现前端反向代理/负载均衡高可用架构的搭建!
实验环境如下需要四台服务器,其实严格的讲只需要三台就可以了,后端的网站服务器可以是单台也可以是多台,说一下我这里四台机器的软件包都是安装操作系统时全部安装的。这里环境如下:
IP地址          用途                      系统版本             nginx版本        keepalived版本
192.168.2.73    nginx+keepalived(MASTER) RedHat 4.8(64位)     1.3.5            1.1.15    
192.168.5.55    nginx+keepalived(BACKUP) RedHat 4.8(64位)     1.3.5            1.1.15    
192.168.5.54    apache(系统自带)         RedHat 4.8(64位)     N/A              N/A
192.168.5.57    apache(系统自带)         RedHat 4.8(64位)     N/A              N/A    
192.168.2.100   VIP(用于切换)
1、MASTER上安装nginx
groupadd www
useradd -g www www
tar zxvf nginx-1.3.5.tar.gz
cd nginx-1.3.5
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install
2、修改/usr/local/nginx/conf/nginx.conf配置文件如下:
user  www www;
worker_processes 1;
pid        logs/nginx.pid;
worker_rlimit_nofile 1024; 
events
{
 use epoll;
 worker_connections 1024;

http
{
 include       mime.types;
 default_type  application/octet-stream;
 keepalive_timeout 120;
 server_tokens off;
 send_timeout 60;
 tcp_nodelay on;
 upstream  https  {
 server 192.168.5.54:8080;
 server 192.168.5.57:8080; 
 }
 log_format access_log  '$remote_addr - $remote_user [$time_local] $request'
 '"$status" $body_bytes_sent "$http_referer"'
 '"$http_user_agent" "$http_x_forwarded_for"';
 access_log  /usr/local/nginx/logs/access.log  access_log;
 server
 {
 listen  80;
 server_name  192.168.2.73;
 location / {
 proxy_pass        http://https;
 proxy_set_header   Host             $host;
 proxy_set_header   X-Real-IP        $remote_addr;
 proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 } 
 } 
}
3、检查配置文件是否有错误,出现如下两行则说明没问题!
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、安装keepalived
tar zxvf keepalived-1.1.15.tar.gz
vi /usr/src/kernels/2.6.9-89.EL-smp-x86_64/include/linux/types.h
将如下两行注释掉,否则编译会出错,跟我这个版本的系统有关系,你的也许不要!
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
cd keepalived-1.1.15
./configure
make
make install
将keepalived作为系统服务启动
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
5、修改/etc/keepalived/keepalived.conf配置文件如下:
! Configuration File for keepalived
global_defs {
 router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
 script "/root/scripts/monitor_nginx.sh"   #根据自己的实际路径放置monitor_nginx.sh    
 interval 2
 weight 2
}
vrrp_instance VI_1 {
 state MASTER
 interface eth0
 virtual_router_id 51
 priority 100
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass 1111
}
 track_script {
 Monitor_Nginx
}
 virtual_ipaddress {
 192.168.2.100
 }
}
6、从keepalived配置文件里面看到了有一处调用了一个脚本,脚本内容如下:
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
 /usr/local/nginx/sbin/nginx
 sleep 5
 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
 then
 killall keepalived
 fi
fi
7、增加可执行权限
chmod +x /root/scripts/monitor_nginx.sh
注:备机的Nginx、keepalived和以上安装步骤一样,只是个别的地方要修改!
例如nginx的配置文件里面的server_name  192.168.2.73的IP地址改为server_name  192.168.5.55
例如keepalived的配置文件里面修改两处
state MASTER修改为state BACKUP
priority 100修改为priority 99
至此MASTER和BACKUP就配置完毕了!!!
7、配置两台apache服务器
登录192.168.5.54上操作:
[root@hadoop5 ~]# echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@hadoop5 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@hadoop5 ~]# /etc/init.d/httpd start
登录192.168.5.57上操作:
[root@service ~]# echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@service ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@service ~]# /etc/init.d/httpd start
8、测试
启动MASTER的keepalived服务
/etc/init.d/keepalived start
执行ip a命令看是否有192.168.2.100的VIP出现,再查看nginx是否已经启动?
ps -ef | grep nginx
[root@hadoop3 ~]# for i in $(seq 20); do curl http://192.168.2.100/; done
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
启动BACKUP的keepalived服务
/etc/init.d/keepalived start
查看nginx服务也随之启动了
停止MASTER的keepalived服务,查看BACKUP是否已接替了VIP地址?
/etc/init.d/keepalived stop
[root@nagios-server scripts]# ip a
1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:14:22:4a:ec:39 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.55/21 brd 192.168.7.255 scope global eth0
    inet 192.168.2.100/32 scope global eth0
    inet6 fe80::214:22ff:fe4a:ec39/64 scope link
       valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
    link/sit 0.0.0.0 brd 0.0.0.0
查看BACKUP的/var/log/messages日志是否接管VIP?
Oct 11 12:27:18 nagios-server Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 11 12:27:18 nagios-server Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.2.100
Oct 11 12:27:22 nagios-server Keepalived_vrrp: ip address associated with VRID not present in received packet : 1677895872
Oct 11 12:27:22 nagios-server Keepalived_vrrp: one or more VIP associated with VRID mismatch actual MASTER advert
然后再启动MASTER的keepalived服务,看是否接管VIP?
/etc/init.d/keepalived start
执行ip a命令查看是否有192.168.2.100地址?
查看messages日志
Oct 11 13:06:27 hadoop3 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.2.100
Oct 11 13:06:28 hadoop3 Keepalived_vrrp: ip address associated with VRID not present in received packet : 1677895872
Oct 11 13:06:28 hadoop3 Keepalived_vrrp: one or more VIP associated with VRID mismatch actual MASTER advert
Oct 11 13:06:28 hadoop3 Keepalived_vrrp: bogus VRRP packet received on eth0 !!!
这样说明就OK了!!!

相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
20天前
|
负载均衡 网络协议 算法
一文读懂什么是Nginx?它能否实现IM的负载均衡?
Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡方案,从某种意义上来讲,Nginx几乎是低成本、高负载Web服务端代名词。 如此深入人心的Nginx,很多人也想当然的认为,在IM或消息推送等场景下是否也能使用Nginx来解决负载均衡问题? 另外,即时通讯网的论坛和QQ群里也经常有人问起,Nginx是否能支持TCP、UDP、WebSocket的负载
67 4
|
4月前
|
负载均衡 算法 搜索推荐
Nginx 常用的负载均衡算法
【10月更文挑战第17天】在实际应用中,我们需要根据具体的情况来选择合适的负载均衡算法。同时,还可以结合其他的优化措施,如服务器健康检查、动态调整权重等,来进一步提高负载均衡的效果和系统的稳定性。
194 59
|
1月前
|
存储 负载均衡 NoSQL
搭建高可用及负载均衡的Redis
通过本文介绍的高可用及负载均衡Redis架构,可以有效提升Redis服务的可靠性和性能。主从复制、哨兵模式、Redis集群以及负载均衡技术的结合,使得Redis系统在应对高并发和数据一致性方面表现出色。这些配置和技术不仅适用于小型应用,也能够支持大规模企业级应用的需求。希望本文能够为您的Redis部署提供实用指导和参考。
200 9
|
2月前
|
负载均衡 算法 Linux
LVS+Keepalived:实现高效软负载均衡的利器
本文介绍了如何使用LVS(Linux Virtual Server)和Keepalived搭建高可用负载均衡集群。LVS通过不同调度算法将请求转发给后端服务器,而Keepalived基于VRRP协议实现服务高可用,避免IP单点故障。具体步骤包括环境准备、安装配置ipvsadm和Keepalived、启动服务及测试。文中还详细解释了配置文件中的关键参数,并提供了故障转移测试方法。最后,文章简要对比了软件、硬件和云负载均衡方案的特点,帮助读者选择合适的负载均衡策略。
416 4
|
2月前
|
弹性计算 负载均衡 网络协议
ECS中实现nginx4层7层负载均衡和ALB/NLB原SLB负载均衡
通过本文的介绍,希望您能深入理解并掌握如何在ECS中实现Nginx四层和七层负载均衡,以及如何使用ALB和NLB进行高效的负载均衡配置,以提高系统的性能和可靠性。
248 9
|
3月前
|
缓存 负载均衡 算法
如何配置Nginx反向代理以实现负载均衡?
如何配置Nginx反向代理以实现负载均衡?
|
2月前
|
负载均衡 算法 应用服务中间件
Nginx的负载均衡
Nginx 是一款高性能的Web服务器与反向代理服务器,支持负载均衡功能,能有效提升系统性能与可靠性。其负载均衡策略包括基于轮询和权重的分配方法,以及IP哈希、最小连接数等算法,可根据实际需求灵活选择。
211 5
|
2月前
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
193 3
|
4月前
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
242 4
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
3月前
|
负载均衡 算法 应用服务中间件
Nginx 常用的负载均衡算法
【10月更文挑战第22天】不同的负载均衡算法各有特点和适用场景。在实际应用中,需要根据具体的业务需求、服务器性能和网络环境等因素来选择合适的算法。
174 3