项目环境: 系统:CentOS 7.6 关闭:selinux 关闭:firewalld

防火墙:外网口填写的是电信运营商给的公网IP地址,然后映射内网vip地址,再开启一些安全策略。

Nginx + Keepalived:Nginx 接受到用户的请求后copy一份 转给后端的某台Web服务器,如果已经接收到请求的Web服务器down机了,此时Nginx调度器会 把请求再copy一份给另一台Web服务器,直至完成用户的响应。

Keepalived 负责虚拟出一个虚拟IP 称 VIP

WebServer:负责解析用户的请求抓取相应的数据给前端的Nginx调度器,关于WebServer优化,请看下集续讲。

1、Nginx负载均衡的配置文件可以是保留用到的其它部分全注释或者删除

Nginx调度器一:IP 192.168.1.16 [root@lb1 ~]# cat /etc/nginx/conf.d/default.conf upstream groupweb {
server 192.168.1.21:80 max_fails=2 fail_timeout=5s; server 192.168.1.22:80 max_fails=2 fail_timeout=5s; server 192.168.1.23:80 max_fails=2 fail_timeout=5s; }

server { listen 80; server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://groupweb;
}

}

Nginx调度器二: IP 192.168.1.17 [root@lb2 ~]# cat /etc/nginx/conf.d/default.conf upstream groupweb { server 192.168.1.21:80 max_fails=2 fail_timeout=5s; server 192.168.1.22:80 max_fails=2 fail_timeout=5s; server 192.168.1.23:80 max_fails=2 fail_timeout=5s; }

server { listen 80; server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://groupweb;
}

}

2、Keepalived 的配置文件的修改,保留需要的,其它部分建议注释或者删除

Keepalived高可用一 IP: 192.168.1.16 [root@lb1 ~]# cat /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100/24 } }

Keepalived高可用二 IP: 192.168.1.17 [root@lb2 ~]# cat /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100/24 }

}

3、Web服务器 :为了简单方便 我就直接装了Apache服务

Web服务器一 IP :192.168.1.21 [root@web1 ~]# echo web1 > /var/www/html/index.html

Web服务器二 IP :192.168.1.22 [root@web1 ~]# echo web2 > /var/www/html/index.html

Web服务器三 IP :192.168.1.23 [root@web1 ~]# echo web3 > /var/www/html/index.html