|NO.Z.00015|——————————|^^ 构建 ^^|——|多级负载.V2|——|7台server|

一、实验构建
### --- 系统硬件规划

~~~     Windows:10.10.10.240:client
~~~     HA-server1:10.10.10.11:LVS-DR
~~~     HA-server2:10.10.10.12:Nginx
~~~     HA-server3:10.10.10.13:Nginx
~~~     HA-server4:10.10.10.14:Apache
~~~     HA-server5:10.10.10.15:Apache
~~~     HA-server6:10.10.10.16:Apache
二、10.10.10.14    10.10.10.15    10.10.10.16构建三台Apache服务器
### --- 10.10.10.14    10.10.10.15    10.10.10.16构建三台Apache服务器
### --- 启动10.10.10.14Apache服务并配置网站

[root@server14 ~]# service httpd start
[root@server14 ~]# echo "www.yanqi.com-1" >> /var/www/html/index.html
[root@server14 ~]# curl localhost
www.yanqi.com-1
### --- 启动10.10.10.15Apache服务并配置网站

[root@server15 ~]# service httpd start
[root@server15 ~]# echo "www.yanqi.com-2" >> /var/www/html/index.html
[root@server15 ~]# curl localhost
www.yanqi.com-2
### --- 启动10.10.10.16Apache服务并配置网站
[root@server16 ~]# service httpd start
[root@server16 ~]# echo "www.yanqi.cn" >> /var/www/html/index.html
[root@server16 ~]# curl localhost
www.yanqi.cn
三、10.10.10.12 部署nginx
### --- 10.10.10.12部署nginx
### --- 关闭网卡的守护进程

[root@server12 ~]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]
[root@server12 ~]#  chkconfig NetworkManager off
~~~     将nginx-1.2.6.tar.gz上传到服务器中并make部署nginx

[root@server12 ~]# yum install -y pcre pcre-devel zlib zlib-devel
[root@server12 ~]# tar -zxvf nginx-1.2.6.tar.gz 
[root@server12 ~]# useradd -s /bin/nologin -M nginx
[root@server12 ~]# cd nginx-1.2.6
[root@server12 nginx-1.2.6]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@server12 nginx-1.2.6]# make && make install
### --- 修改nginx配置文件

[root@server12 nginx-1.2.6]# cd /usr/local/nginx/conf/ 
[root@server12 conf]# vim nginx.conf
http {
    #gzip  on;
    upstream yanqi.com {
        server 10.10.10.14:80;
        server 10.10.10.15:80;
    }

    upstream yanqi.cn {
        server 10.10.10.16:80;
    }
    
    server {
        listen       80;
        server_name  www.yanqi.com;
        
        location / {
            proxy_pass http://yanqi.com;                                    // 通过反向代理的方式去访问
        }
    }
    
    server {
        listen       80;
        server_name  www.yanqi.cn;

        location / {
            proxy_pass http://yanqi.cn;
        }
    }
}
### --- 检测配置是否OK并启动nginx

[root@server12 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful    
[root@server12 conf]# /usr/local/nginx/sbin/nginx                           // 启动nginx
### --- 配置本地hosts文件

C:\Windows\System32\drivers\etc\hosts
10.10.10.12    www.yanqi.com
10.10.10.12    www.yanqi.cn
### --- 验证:

~~~     通过IE浏览器访问http://www.yanqi.com/
~~~     返回结果:发现在www.yanqi.com-1 www.yanqi.com-2 在这两台主机之间跳转,而不能跳转到www.yanqi.cn
~~~     通过IE浏览器访问http://www.yanqi.cn/ 
~~~     返回结果:发现在www.yanqi.cn ,不管怎么刷新,都不会再跳转到.com地址上。
四、10.10.10.13 部署nginx
### --- 10.10.10.13部署nginx
### --- 关闭网卡的守护进程

[root@server13 nginx-1.2.6]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]
[root@server13 nginx-1.2.6]#  chkconfig NetworkManager off
~~~     将nginx-1.2.6.tar.gz上传到服务器中并make部署nginx

[root@server13 ~]# yum install -y pcre pcre-devel zlib zlib-devel
[root@server13 ~]# tar -zxvf nginx-1.2.6.tar.gz 
[root@server13 ~]# useradd -s /bin/nologin -M nginx
[root@server13 ~]# cd nginx-1.2.6
[root@server13 nginx-1.2.6]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@server13 nginx-1.2.6]# make && make install
### --- 修改nginx配置文件                                                    // 从10.10.10.12推入配置文件到10.10.10.13节点

[root@server12 ~]# scp /usr/local/nginx/conf/nginx.conf root@10.10.10.13:/usr/local/nginx/conf/nginx.conf
[root@server13 nginx-1.2.6]# cat /usr/local/nginx/conf/nginx.conf           // 查看配置文件是否OK
### --- 检测配置是否OK并启动nginx

[root@server13 nginx-1.2.6]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server13 nginx-1.2.6]# /usr/local/nginx/sbin/nginx                    // 启动nginx
### --- 配置本地hosts文件

C:\Windows\System32\drivers\etc\hosts
10.10.10.13    www.yanqi.com
10.10.10.13    www.yanqi.cn
### --- 验证:

~~~     通过IE浏览器访问http://www.yanqi.com/
~~~     返回结果:发现在www.yanqi.com-1 www.yanqi.com-2 在这两台主机之间跳转,而不能跳转到www.yanqi.cn
~~~     通过IE浏览器访问http://www.yanqi.cn/ 
~~~     返回结果:发现在www.yanqi.cn ,不管怎么刷新,都不会再跳转到.com地址。
五、10.10.10.11    配置LVS-DR模式
### --- 负载调度器有后端服务状态健康监测的一个要求,
### --- 对于nginx来说它是默认自带了后端服务状态健康监测的功能;
### --- 说明服务节点有一个死亡,不会造成我们业务中断。
### --- 在真正的生产环境中再加入LVS的高可用,用Keepalived去做,
### --- 这样12,13类似的服务死亡,就会自动剔除使用节点,不会造成整个的网络中断。
### --- 配置网卡子接口

[root@server11 ~]# cd /etc/sysconfig/network-scripts/
[root@server11 network-scripts]# cp -a ifcfg-eth0 ifcfg-eth0:0
[root@server11 network-scripts]# vim !$
DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.10.10.100
NETMASK=255.255.255.0
### --- 关闭网卡的守护进程
[root@server11 network-scripts]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]
[root@server11 network-scripts]# chkconfig NetworkManager off

~~~     启动网卡
[root@server11 ~]# ifup eth0:0
[root@server11 ~]# ifconfig
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:FA:34:71  
          inet addr:10.10.10.100  Bcast:10.10.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
### --- 关闭内核参数,修改网卡重定向功能

[root@server11 ~]# vim /etc/sysctl.conf 
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
[root@server11 ~]# sysctl -p                                                // 刷新内核参数
### --- 安装Ipvsadm

[root@server11 ~]# yum install -y ipvsadm
### --- 写入集群配置
 
[root@server11 ~]# ipvsadm -A -t 10.10.10.100:80 -s rr
[root@server11 ~]# ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.12:80 -g
[root@server11 ~]# ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.13:80 -g
[root@server11 ~]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [  OK  ] 
[root@server11 ~]# chkconfig ipvsadm on                                     // 设置开机自动加载
六、10.10.10.12真实服务器节点
### --- 开启一个子接口:lo:0

[root@server12 ~]# cd /etc/sysconfig/network-scripts/
[root@server12 network-scripts]# cp -a ifcfg-lo ifcfg-lo:0
[root@server12 network-scripts]# vim !$
DEVICE=lo:0
IPADDR=10.10.10.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
### --- 修改内核参数

[root@server12 network-scripts]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@server12 network-scripts]# sysctl -p
### --- 启动lo:0网卡

[root@server12 network-scripts]# ifup lo:0
[root@server12 network-scripts]# ifconfig 
lo:0      Link encap:Local Loopback  
          inet addr:10.10.10.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
### --- 写入路由记录

[root@server12 network-scripts]# route add -host 10.10.10.100 dev lo:0
[root@server12 network-scripts]# echo "route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local              //并设置开机自启
七、10.10.10.13真实服务器节点
### --- 开启一个子接口:lo:0

[root@server13 ~]# cd /etc/sysconfig/network-scripts/
[root@server13 network-scripts]# cp -a ifcfg-lo ifcfg-lo:0
[root@server13 network-scripts]# vim !$
DEVICE=lo:0
IPADDR=10.10.10.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
### --- 修改内核参数

[root@server13 network-scripts]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@server13 network-scripts]# sysctl -p
### --- 启动lo:0网卡

[root@server12 network-scripts]# ifup lo:0
[root@server12 network-scripts]# ifconfig 
lo:0      Link encap:Local Loopback  
          inet addr:10.10.10.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
### --- 写入路由记录

[root@server13 network-scripts]# route add -host 10.10.10.100 dev lo:0
[root@server13 network-scripts]# echo "route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local              //并设置开机自启

八、验证
### --- 打开hosts文件:C:\Windows\System32\drivers\etc\hosts
~~~     通过IE浏览器访问:http://www.yanqi.com/
~~~     返回结果:www.yanqi.com-1    www.yanqi.com-2 在这两个地址间切换;不会跳到www.yanqi.cn
~~~     通过IE浏览器访问:http://www.yanqi.cn/
~~~     返回结果:www.yanqi.cn 不会跳转到:www.yanqi.com-1 www.yanqi.com-2 
~~~     此刻用户入口是LVS,LVS的负载能力比nginx强大很多的。这样既可以识别域名,又可以达到负载量的增加。

10.10.10.100    www.yanqi.com
10.10.10.100    www.yanqi.cn
### --- 查看连接数
~~~     以上环境就是:四层+七层的的一个负载均衡真实方案。
~~~     在生产环境中使用的比较少;
~~~     但有助于我们对四层和七层负载的调度方案和功能实现理解的特点
~~~     非常典型的特点。

[root@server11 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  10.10.10.100:80                     5      168        0    40000        0
  -> 10.10.10.12:80                      2       44        0     9359        0
  -> 10.10.10.13:80                      3      124        0    30641        0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(26)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示