文章目录
- nginx负载均衡介绍
- 部署高可用负载均衡
- Masters
- Masters_A
- Masters_B
- Masters_C
- Masters
- Masters_A
nginx负载均衡介绍
nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。
nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。
但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。
Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache
如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:
./configure –add-module=…/ngx_cache_purge-1.0 …
nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内
在upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:
upstream idfsoft.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。
定义好upstream后,需要在server段内添加如下内容:
server {
location / {
proxy_pass http://idfsoft.com;
}
}
部署高可用负载均衡
部署前提:
关闭防火墙
关闭Selinux
配置阿里源
部署环境说明:
主机名称
|
IP
|
环境
|
Masters
|
192.168.79.140
|
nginx keepalive
|
Masters_A
|
192.168.79.135
|
nginx keepalive
|
Masters_B
|
192.168.79.136
|
nginx
|
Masters_C
|
192.168.79.139
|
nginx
|
负载均衡部署
Masters
//安装nginx
[root@Masters ~]# yum -y install nginx
//修改配置文件
[root@Masters ~]# vim /etc/nginx/nginx.conf
......
upstream webserver {
server 192.168.79.136;
server 192.168.79.139;
}
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://webserver;
}
.....
//开启nginx
[root@Masters ~]# systemctl enable --now nginx.service
//访问
[root@Masters ~]# curl 192.168.79.140
Masters_B
[root@Masters ~]# curl 192.168.79.140
Masters_C
Masters_A
//安装nginx
[root@Masters_A ~]# yum -y install nginx
//修改配置文件
[root@Masters_A ~]# vim /etc/nginx/nginx.conf
......
upstream webserver {
server 192.168.79.136;
server 192.168.79.139;
}
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://webserver;
}
......
//开启nginx
[root@Masters_A ~]# systemctl enable --now nginx.service
//访问
[root@Masters_A ~]# curl 192.168.79.135
Masters_C
[root@Masters_A ~]# curl 192.168.79.135
Masters_B
Masters_B
//安装nginx
[root@Masters_B ~]# yum -y install nginx
//修改配置文件
root@Masters_B html]# vim index.html
vice.
[root@Masters_B html]# cat index.html
Masters_B
//开启nginx
[root@Masters_B html]# systemctl enable --now nginx.service
//访问
[root@Masters_B html]# curl 192.168.79.136
Masters_B
Masters_C
//安装nginx
[root@Mastres_C ~]# yum -y install nginx
//修改配置文件
[root@Mastres_C html]# vim index.html
[root@Mastres_C html]# cat index.html
Masters_C
//开启nginx
[root@Mastres_C html]# systemctl enable --now nginx.service
//访问
[root@Mastres_C html]# curl 192.168.79.139
Masters_C
高可用部署
Masters
//安装keepalived
[root@Masters ~]# yum -y install keepalived
//修改配置文件
[root@Masters keepalived]# vim keepalived.conf
[root@Masters keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.79.250
}
}
virtual_server 192.168.79.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.79.140 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.79.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
//开启keepalive
[root@Masters keepalived]# systemctl start keepalived.service
//查看ip
[root@Masters keepalived]# ip a
.....
2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d8:24:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.79.140/24 brd 192.168.79.255 scope global dynamic noprefixroute ens160
valid_lft 1336sec preferred_lft 1336sec
inet 192.168.79.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::740c:3f84:6387:618e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
//访问
[root@Masters keepalived]# curl 192.168.79.250
Masters_B
[root@Masters keepalived]# curl 192.168.79.250
Masters_C
Masters_A
//安装keepalived
[root@Masters_A ~]# yum -y install keepalived
//修改配置文件
[root@Masters_A keepalived]# vim keepalived.conf
[root@Masters_A keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass wangqing
}
virtual_ipaddress {
192.168.79.250
}
}
virtual_server 192.168.79.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.79.140 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.79.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
//开启keepalive
[root@Masters keepalived]# systemctl start keepalived.service
//查看ip
[root@Masters keepalived]# ip a
.....
2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:ae:14:ad brd ff:ff:ff:ff:ff:ff
inet 192.168.79.135/24 brd 192.168.79.255 scope global dynamic noprefixroute ens160
valid_lft 1463sec preferred_lft 1463sec
inet 192.168.79.250/32 scope global ens160
valid_lft forever preferred_lft forever
inet6 fe80::5796:4d22:9dc8:d82e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
//访问
[root@Masters_A keepalived]# curl 192.168.79.250
Masters_C
[root@Masters_A keepalived]# curl 192.168.79.250
Masters_B
关于访问问题
两台的高可用主机的Nginx不可以同时开启 只能开启一个然后进行访问
如:
使用Masters主机访问
需要关闭Masters_A主机的Nginx服务
需要关闭Masters_A主机的Keepadlive服务
[root@Masters_A keepalived]# systemctl stop nginx.service
[root@Masters_A keepalived]# systemctl stop keepalived.service
[root@Masters_A keepalived]# systemctl start nginx.service
[root@Masters keepalived]# curl 192.168.79.250
Masters_B
[root@Masters keepalived]# curl 192.168.79.250
Masters_C
使用Masters_A主机访问
需要关闭Masters主机的Nginx服务
需要关闭Masters主机的Keepadlive服务
[root@Masters keepalived]# systemctl stop nginx.service
[root@Masters keepalived]# systemctl stop keepalived.service
[root@Masters_A keepalived]# systemctl start nginx.service
[root@Masters_A keepalived]# curl 192.168.79.250
Masters_C
[root@Masters_A keepalived]# curl 192.168.79.250
Masters_B
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net