您的位置:首页 > 运维架构 > Nginx

Nginx+Keepalived负载均衡高可用

2017-11-13 16:58 543 查看

Nginx+Keepalived负载均衡高可用方案:

Nginx 使用平台:unix、linux、windows。 功能: A.www web服务  http 80 b.负载均衡(方向代理proxy) c.web cache(web缓存) 优点: 0.配置简单,灵活。 1.高并发(静态小文件),静态1-2w 2.占用资源少。2w并发 开10个线程服务,内存消耗几百M。 3.功能种类比较多(web,cache,proxy) 4.支持epoll模型。使得Nginx可以支持高并发。 5.nginx配合动态服务和apache有区别。 6.利用nginx可以对IP限速,可以限制连接数。 LNMP实现原理,架构: Nginx的应用场合: 1.静态服务器(图片,视频服务),另一个lighttpd。并发:1-3w Html、js、css、flv。 2.动态服务,nginx+fastcgi的方式运行php,jsp。并发:500-1500 Apache+php  ,lighttpd+fcgi php 3.反向代理,负载均衡。日PV2000w以下,都可以直接用NGINX做代理。 Haproxy,F5,a10 4.缓存服务。 Nginx和其他web服务器的对比 1.apache          2.2版本非常稳定强大,据官方说,其2.4版本性能超强          Prefork模式取消了进程创建开销,性能很高。          处理动态业务数据时,因关联到后端的引擎和数据库,瓶颈不在于Apache本身。          高并发时消耗系统资源相对比较多一些。          基于传统的select模型。          扩展库,DOS方法,apxs          功能多,更稳定 2.nginx          基于异步IO模型(epoll,kqueue),性能强,能够支持上万并发。          对小文件支持很好,性能很高(限静态小文件1M)          代码优美,扩展库必须编译进主程序          消耗系统资源比较低 3.Lighttpd(百度贴吧,豆瓣)          基于异步IO模型,性能和Nginx相近          扩展库是SO模式,比Nginx要灵活          全球使用率比较低,安全性没有上面两个好。          通过插件(mod_secdownload)可实现文件URL地址加密。

1、实验环境:(centos6.9 minimal)

主机名 IP 作用
Nginx_master  192.168.128.155 主负载均衡
Nginx_backup  192.168.128.156 备负载均衡
Web_1  192.168.128.157 web服务器
Web_2  192.168.128.158 web服务器
Nginx_vip  192.168.128.199 网站vip地址

2、实验拓扑图:

3、分别安装nginx负载均衡器及配置脚本

1)安装nginx

yum install -y gcc gcc+ gcc-c++ openssl opensll-devel  wget  vim          //安装基础环境

/etc/init.d/iptables stop                                                 //关闭防火墙

groupadd www                                             //添加组

useradd -g www www                                       //添加用户

mkdir -p /data/logs                                                      //创建日志文件夹

chown -R www:www /data/logs/                                //更改属主

cd /usr/local/src/                                                      //安装路径
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar xf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --without-http_rewrite_module
报错解决 yum install openssl openssl-devel –y make && make install

 备份:cp /usr/local/nginx/conf/nginx.conf{,.bak}

修改配置文件:vim /usr/local/nginx/conf/nginx.conf
user  www www;
worker_processes  4;                       //nginx进程数
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
worker_connections  1024;
}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
tcp_nopush     on;
keepalive_timeout  65;
gzip  on;
gzip_min_length 1k;
gzip_buffers   4 16k;
gzip_http_version 1.0;
upstream backend                       //负载均衡
{
ip_hash;                               //轮询算法
server 192.168.128.157:80;
server 192.168.128.158:80;
}
server {
listen       80;
server_name  www.123.com;
location / {
root   /var/www/html;
index  index.php index.html index.htm;
proxy_redirect off;
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://backend;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

2)安装keepalive

yum install keepalived -y

修改配置文件:(Master) vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.128.155
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.128.155
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.128.199      //虚拟VIP
}
}

 修改配置文件:(Backup)

! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.128.155
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.128.156
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.128.199
}
}

 

 启动keepalive:

service keepalived start

在主keepalive上查看,已经接管VIP:

ip addr show

3 ) 安装web服务

yum install httpd -y

修改配置文件:

echo "ServerAdmin 192.168.128.157" >>/etc/httpd/conf/httpd.conf

启动web服务:

service httpd start

为了测试添加一个web网页:

cat >>/var/www/html/index.html<<EOF
> </h>
> 157
> <h>
> EOF

 测试1:

测试2 关闭158web服务:

 编写Nginx监控脚本:

!/bin/bash
while :
do
nginxpid=`ps -C nginx --no-header|wc -l`
if [ $nginxpid -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 5
nginxpid=`ps -C nginx --no-header|wc -l`
echo $nginxpid
if [ $nginxpid -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
sleep 5
done

放入后台nohup /bin/bash /root/nginx_pid.sh &

测试3:模拟主Nginx宕机:

备keepalive上自动接管VIP:

查看网站正常打开:

 另外:

1. 要使得Nginx支持https只需在负载均衡器上开启ssl功能,监听443端口(防火墙做好映射)将证书放在负载均衡器上而不是后面的web服务器。

server {
listen       443 ;
server_name  www.123.com;
ssl_certificate      /usr/local/nginx/keys/www.123.com.crt;
ssl_certificate_key  /usr/local/nginx/keys/www.123.com.key;
ssl_session_cache    shared:SSL:1m;
ssl_session_timeout  5m;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers  on;
location / {
root   html;
index  index.html index.htm;
}
}

2. 获取客户端真实IP

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. 上述为nginx主从架构配置(即一个Nginx_VIP)若无故障从nginx长期处于备份状态,而主Nginx负载就很高,如果我想两台负载均衡器都处在工作状态来实现负载均衡也很容易(配置两个VIP)。通过keepalive生成两个实例,两台Nginx互为备机。

主Nginx机器之一的keepalived.conf配置如下:

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.128.198
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 55
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.128.199
}

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: