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

nginx负载均衡+keeplived

2015-10-22 12:10 531 查看
Nginx+Keepalived 负载均衡环境:两台负载均衡服务器nginx proxy 10.0.0.5 (keepalived +nginxproxy1)nginx proxy 10.0.0.6 (keepalived +nginxproxy2)两台后端RS服务器RS1 10.0.0.3 nginxRS2 10.0.0.4 nginx

下面nginx和keepalived都要在两台负载均衡服务器中安装=================================================================
1、安装nginx
#1.安装nginx依赖库yum install openssl openssl-devel -yyum install pcre-devel -y
#2.创建nginx用户:useradd -s /sbin/nologin nginxtail -1 /etc/passwd#3 安装nginxmkdir -p /home/lvnian/toolscd /home/lvnian/toolswget http://nginx.org/download/nginx-1.6.2.tar.gztar zxf nginx-1.6.2.tar.gz cd nginx-1.6.2./configure \--user=nginx \--group=nginx \--prefix=/application/nginx-1.6.2 \--with-http_stub_status_module \--with-http_ssl_module \--with-http_realip_modulemake && make install#4.制作软连接:ln -s /application/nginx-1.6.2 /application/nginxll /application#5 启动nginx#/application/nginx/sbin/nginx -t /application/nginx/sbin/nginx #(不用接参数,就是启动nginx)
######安装完成nginxnetstat -lnt |grep 80ps -ef |grep nginx |grep -v greplsof -i :80#########################################################################################################################################################################################################
2、安装keepalived
1.做相关内核软连接安装keepalived之前要先做软连接ls /usr/src/kernels/`uname -r` -ldln -s /usr/src/kernels/`uname -r` /usr/src/linuxls /usr/src/ -l
2.下载并安装keepalived软件[ ! -d /home/lvnian/tools ] && mkdir /home/lvnian/tools -pcd /home/lvnian/tools wget http://www.keepalived.org/software/keepalived-1.2.10.tar.gztar xf keepalived-1.2.10.tar.gz cd keepalived-1.2.10./configure make && make install
3.配置启动规范/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d//bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/mkdir /etc/keepalived /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived//bin/cp /usr/local/sbin/keepalived /usr/sbin//etc/init.d/keepalived startps -ef |grep keepalived安装完成keepalived服务
#########################################################################################################################################################################################################
3、配置Keepalived 服务MASTER:[root@KEEP1 keepalived]# cat keepalived.conf! Configuration File for keepalived
global_defs { notification_email { 759685538@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_1}
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.100/24 }}###################################################################BACKUP:[root@KEEP2 keepalived]# cat keepalived.conf! Configuration File for keepalived
global_defs { notification_email { 759685538@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_2}
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 55 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.100/24 }}[root@KEEP2 keepalived]# 配置成功Keepalived的标准是VIP可以随服务器是否正常进行漂移。这里用是否启动keepalived服务进行VIP漂移正常情况下:



停止KEEP1[root@KEEP1 ~]# /etc/init.d/keepalived stop Stopping keepalived: [ OK ][root@KEEP1 ~]#




#########################################################################################################################################################################################################4、配置nginx负载均衡功能
下面配置文件在两个负载均衡服务器上面配置相同
[root@KEEP1 conf]# cat nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_real_server{ # ip_hash; #用ip哈希算法保持会话 server 10.0.0.3:80 max_fails=3 fail_timeout=20s; server 10.0.0.4:80 max_fails=3 fail_timeout=20s; } server { listen 80; server_name www.lvnian.com; location / { index index.php index.htm index.html; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://web_real_server; } }}[root@KEEP1 conf]# ====================================================================proxy_pass http://web_real_server;用于指定反向代理的服务器池proxy_set_header Host $host;当后端web服务器上也配置有多个虚拟主机时,需要用改Header来区分反向代理那个主机名proxy_set_header X-Forwarded-For $remote_addr;如果后端Web服务器上的程序需要获取用户IP,从该Header头中获取 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;这个参数是用来定义故障转移策略的,当后端服务器节点返回500/502/503和执行超时等错误时,自动将请求转到upstream负载均衡中的另外一台服务器,实现故障转移====================================================================#########################################################################################################################################################################################################
2、配置调试用的nginx web服务器或者Apache(RS节点)操作((RS)nginx的安装和上面的nginx安装一样)RS1 10.0.0.3 nginxRS210.0.0.4nginxRS就把web服务配置成功即可[root@RS1 ~]# curl 10.0.0.310.0.0.3[root@RS1 ~]#[root@RS2 ~]# curl 10.0.0.410.0.0.4[root@RS2 ~]# 在实际的工作场景中。RS1和RS2提供的web服务应该是一样,在这里设置成不一样,主要是为了区分客户端访问的时候,是不同的服务器在提供服务。##################################################################################################################################################################################################################### 测试的客户端配置hosts文件 echo "10.0.0.100 www.lvnian.com">>/etc/hosts



上面的测试说明这个实验已经实现了负载均衡的功能了
部署完成你还可以把其中一台RS服务挂起;或者把其中一台Nginx负载均衡服务器断开;设置可以把一台nginx负载均衡服务器断开以及一台RS同时断开;服务都可以已经对外正常提供,这就是Keepalived+Nginx的高可用性。在这就不一一做实验了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: