nginx负载均衡配置,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;
}
}
服务类型 | ip | 应用 | 系统 |
---|---|---|---|
LB | 192.168.159.101 | nginx | centos8 |
httpd | 192.168.159.146 | httpd | centos8 |
nginx | 192.168.159.147 | nginx | centos8 |
首先要关闭防火墙和selinux,步骤略
//先httpd机子上安装httpd
[root@apache ~]# dnf -y install httpd
/修改httpd访问页面,实验好分辨出来,因为我是yum安装的所以访问页面目录是在这里,如果你是源码安装的话就要去安装目录下找了
[root@apache ~]# cd /var/www/html/
[root@apache html]# ls
index.html
[root@apache html]# echo 'this is apache' > index.html
[root@apache html]# systemctl enable --now httpd
[root@apache html]# curl 192.168.159.146
this is apache
//在nginx机子上安装nginx
[root@nginx ~]# dnf -y install nginx
/修改nginx访问页面,实验好分辨出来,因为我是yum安装的所以访问页面目录是在这里,如果你是源码安装的话就要去安装目录下找了
[root@nginx ~]# cd /usr/share/nginx/html/
[root@nginx html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@nginx html]# echo 'this is nginx' > index.html
[root@nginx html]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx html]# curl 192.168.159.147
this is nginx
//修改LB机子上的nginx配置文件实现负载均衡
[root@LB ~]# dnf -y install nginx
[root@LB ~]# cd /etc/nginx/
[root@LB nginx]# vim nginx.conf
upstream idfsoft.com{
server 192.168.159.146;
server 192.168.159.147;
}
……
location / {
proxy_pass http://idfsoft.com;
}
[root@LB nginx]# systemctl restart nginx.service
//测试
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is nginx
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is nginx
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is nginx
在upstream
段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:
[root@LB nginx]# vim nginx.conf
upstream idfsoft.com{
ip_hash;
server 192.168.159.146;
server 192.168.159.147;
}
//设置ip_hash后如果你访问第一次是apache的话就会后面一直访问apache,要是访问nginx的话后面就会一直访问的nginx
[root@LB nginx]# systemctl restart nginx.service
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
//类似能者多劳一样,访问apache3次在访问nginx1次。默认的就是一次
[root@LB nginx]# vim nginx.conf
upstream idfsoft.com{
ip_hash;
server 192.168.159.146 weight=3;
server 192.168.159.147;
}
[root@LB nginx]# systemctl restart nginx.service
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is nginx
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is apache
[root@LB nginx]# curl 192.168.159.101
this is nginx
nginx负载均衡调度器高可用配置
服务类型 | ip | 应用 | 系统 |
---|---|---|---|
backup | 192.168.159.100 | keepalived,nginx | centos8 |
master | 192.168.159.101 | keepalived,nginx | centos8 |
apache | 192.168.159.146 | http | centos8 |
nginx | 192.168.159.147 | nginx | centos8 |
//apache和nginx的配置和上面是一样的无需更改,一样的关闭防火墙和selinx
//配置backup,安装nginx,keepalived
[root@backup ~]# dnf -y install nginx
[root@backup ~]# dnf -y install keepalived
[root@backup ~]# cd /etc/keepalived/
[root@backup keepalived]# ls
keepalived.conf
[root@backup keepalived]# mv keepalived.conf{,.bak} //备份一下
[root@backup keepalived]# vim keepalived.conf
[root@backup keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.159.250
}
}
virtual_server 192.168.159.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.159.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.159.101 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
//BACKUP端要关闭nginx服务
[root@backup keepalived]# systemctl stop nginx.service
//配置master,安装keepalived,因为上面安装了nginx,这里就不再演示
[root@master nginx]# dnf -y install keepalived
[root@master keepalived]# vim keepalived.conf
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02 //这里要更改,因为不能唯一
}
vrrp_instance VI_1 {
state MASTER
interface eth0 //这里要更具自己的网卡名称来写
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.159.250
}
}
virtual_server 192.168.159.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.159.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.159.101 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master keepalived]# systemctl enable --now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
//VIP在master端
[root@master keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:18:f6:44 brd ff:ff:ff:ff:ff:ff
inet 192.168.159.100/24 brd 192.168.159.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.159.250/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe18:f644/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:18:f6:4e brd ff:ff:ff:ff:ff:ff
//使用VIP访问。
[root@master keepalived]# curl 192.168.159.250
this is apache
[root@master keepalived]# curl 192.168.159.250
this is nginx
[root@master keepalived]# curl 192.168.159.250
this is apache
[root@master keepalived]# curl 192.168.159.250
this is apache
[root@master keepalived]# curl 192.168.159.250
this is apache
[root@master keepalived]# curl 192.168.159.250
this is nginx