Loading

22、lnmp_nginx反向代理(负载均衡)、高可用


负载均衡,根据ip和端口号找到相应的web服务器站点(即端口区分):

22.1、nginx的负载均衡:

1、介绍:

网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,

将压力分散到不同的机器上。有时候来自web前端的压力,也能让人十分头痛。怎样将同一个域名的访问分散到两台或更多的机器上呢?这其实就是另一

种负载均衡了,nginx自身就可以做到,只需要做个简单的配置就行。nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx

还可以按照调度规则实现动态、静态页面的分离,可以按照轮询、ip哈希、URL哈希、权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查。

2、nginx的反向代理和负载均衡的区别:

(1)负载均衡需要通过反向代理来实现;

(2)反向代理就是指nginx作为前端服务器,将请求转发到后端,再将后端服务器的结果,返回给客户端;

(3)它在中间做了一个代理服务器的角色;

(4)负载均衡对反向代理增加了一些策略,因为后端是多台服务器,nginx会根据设定的策略将请求转发给

一个相对空闲的服务器,对负载进行分流,减轻服务器压力;

22.2、nginx 的 upstream目前支持 4 种方式的分配

1、轮询(默认)

  每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight

  指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

3、ip_hash

  每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,也可以搭建session服务器,共享所有用户的session。

4、fair(第三方)

  按后端服务器的响应时间来分配请求,响应时间短的优先分配。

5、url_hash(第三方)

22.3、反向代理和负载均衡的区别:

nginx仅仅作为nginx Proxy反向代理使用,因为这个反向代理功能表现的效果是负载均衡均衡集群的效果,

所以称之为nginx的负载均衡。普通的负载均衡软件,例如大名鼎鼎的lvs,其实现的功能只是对请求数据包的转发

(也可能会改写数据包)、传递,其中DR模式明显的特征是从负载均衡下面的节点服务器来看,接收到的请求还是

来自访问负载均衡器的客户端的真实用户。反向代理接收到访问用户的请求后,会代理用户重行发起请求代理下的

节点服务器,最后把数据返回给客户端用户,在节点服务器看来,访问节点服务器的客户端用户就是反向大力服务器,而非真实的网站访问用户。

几大负载均衡的软件:

nginx(小企业 l4 l7)、lvs(中等企业 l4)、haproxy(特大企业 l4 l7)

ois网络七层模型:l4:tcp(传输层)负载均衡;l7:http(应用层)负载均衡;

nginx负载均衡的组件模块:

ngx_http_proxy_module:proxy代理模块,用于把请求后抛给服务器节点或upstrem服务器池;

ngx_http_upstream_module:负载均衡模块,可以实现网站的负载均衡功能及节点的健康检(upstrem服务器池);

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#负载均衡的配置官网

22.4、配置nginx反向代理:

1、安装反向代理软件nginx(和安装nginx web服务器是一样的操作,lb01)

2、nginx负载均衡配置:

(1)普通配置介绍:

pstream backend {

server backend1.example.com weight=5; #weight,加权操作

server backend2.example.com:8080;

server backup1.example.com:8080 backup;//备份,不参与转发,只有当所有服务器都挂掉时才参与转发;

server backend3.example.com:8080 down;//临时停机维护,不参与任何转发,是关闭状态,

server 192.168.1.66 max_fails=2 fail_timeout=60s;//达到2次访问失败后认为服务器挂掉;挂掉60s后再次测试是否已经挂掉;

}

server {

location / {

proxy_pass http://backend;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上,此时需要在需要在nginx上使用设置日志接收模式为X-Forwarded-For即可;

}

}

(2)部署实例:

1)负载均衡服务器配置(lb01):

[root@lb01 ~]# cd /application/nginx/conf/

[root@lb01 conf]# cat nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream php-pools {

server 10.0.0.8:80;

server 10.0.0.7:80;

ip_hash;

}

server {

listen 80;

server_name www.proxy.com;

location / {

proxy_pass http://php-pools;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

}

2)web01服务器配置:

[root@web01 nginx]# cat conf/nginx.conf

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

include extra/php.conf;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

}

[root@web01 ~]# cat /application/nginx/conf/extra/php.conf

server {

listen 80;

server_name www.php.com;

location / {

root html/php/;

index index.php index.html;

}

location ~ .*\.(php|php5)?$ {

root html/php/;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

3)web02配置:

web02的配置(因为之前使用的是apache搭建的服务器,所以使用该服务器,最后是使用nginx做web服务器):

[root@web02 ~]# vim /application/apache/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

ServerAdmin 250416476-5@qq.com

DocumentRoot "/application/apache-2.2.31/htdocs/php/"

ServerName www.php.com

#ServerAlias www.dummy-host.example.com

ErrorLog "logs/www.php.com_error.log"

CustomLog "logs/www.php.com-access_log" common

</VirtualHost>

4)在本机的host文件添加域名解析:

10.0.0.5 www.proxy.com

5)在网页中进行测试:

先部署然后挂掉一个web服务器看访问日志如果有变化表示代理成功,或通过访问页面去判断;

也可以在linux中使用curl命令进行测试:

for n in `seq 20` ;do curl 10.0.0.5;sleep 1;done

22.5、配置vip:

vip(浮动ip,辅助ip) 、当lb01宕机之后,lb02顶上(平时lb02处于暂停服务的状态),即负载均衡的高可用;

在lb02上配置和lb01一样的配置;

(1)自动模式的浮动ip(keepalived):

1)lb01和lb02安装keeplived软件包:

[root@lb01 ~]# yum install keepalived -y

[root@lb01 ~]# rpm -qa keepalived

keepalived-1.2.13-5.el6_6.x86_64

2)配置lb01和lb02上的keeplived配置文件:

[root@lb01 ~]# vim /etc/keepalived/keepalived.conf #主

! Configuration File for keepalived

global_defs {

notification_email {

25041647-65@qq.com #发送邮件的地址 root@localhost.localdomain;

}

notification_email_from 25041647-65@qq.com #发送邮件的地址;

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL #id,不同的keepalived的id不同

}

vrrp_instance VI_1 { #实例

state MASTER #状态

interface eth0 #通信的网卡

virtual_router_id 51 #实例的id,相同

priority 100 #优先级,优先级大的获取资源

advert_int 1 #心跳间隔

authentication { #两台机器通信的密码

auth_type PASS

auth_pass 1111

}

virtual_ipaddress { #绑定的vip

10.0.0.3/24 dev eth0 label eth0:1

}

}

[root@lb02 ~]# vim /etc/keepalived/keepalived.conf #备

! Configuration File for keepalived

global_defs {

notification_email {

25041647-65@qq.com

}

notification_email_from 25041647-65@qq.com

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL1

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.0.0.3/24 dev eth0 label eth0:1

}

}

3)启动lb01和lb02上的keepalived服务:

[root@lb01~]# /etc/init.d/keepalived start

正在启动 keepalived: [确定]

4)lb01和lb02上的keepalived服务开机自启:

[root@lb01 ~]# chkconfig --list keepalived

keepalived 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭

[root@lb01~]# chkconfig keepalived on

[root@lb01 ~]# chkconfig --list keepalived

keepalived 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

[root@lb01 ~]# ip addr | grep 10.0.0.3 #此时lb01的浮动ip地址已经启动

inet 10.0.0.3/24 scope global secondary eth0:1

5)在windows本地hosts中添加主机和域名的解析文件:

在lb01宕机后lb02能够自动顶上;

10.0.0.3 www.proxy.com

6)测试:

查看lb02的浮动ip没有结果,因为lb02的优先级小于lb01的优先级:

[root@lb02 ~]# ip addr | grep 10.0.0.3

当lb01宕机后,此时查看lb01的浮动ip地址已经出现:

[root@lb02 ~]# ip addr | grep 10.0.0.3

inet 10.0.0.3/24 scope global secondary eth0:1



posted @ 2020-02-15 23:09  云起时。  阅读(311)  评论(0编辑  收藏  举报