配置LVS_DR模式以及nginx负载均衡

一、配置LVS--DR模式:

yum install ipvsadm
配置 LVS 负载均衡服务
1 )手动添加 LVS 转发
1 )用户访问: www.uolookking.com-->vip 192 .168.79.110 ##==> 这个是在 DNS 配置
hzitedu 域的 DNS 记录设置
www IN A 192 .168.79.110
没有配置 DNS 可以使用 hosts 文件配置方式来实现域名解析。
192 .168.79.110 www.hzitedu.com
2 )配置 LVS 虚拟 IP (VIP)
[root@Directory ~] # ifconfig eth1:110 192.168.79.110 netmask
255.255.255.0 up
# 这里采用子接口配置
3 )手工执行配置添加 LVS 服务并增加两台 RS
[root@Directory ~] # ipvsadm -C
[root@Directory ~] # ipvsadm --set 30 5 60
[root@Directory ~] # ipvsadm -A -t 192.168.79.110:80 -s wrr -p 20
[root@Directory ~] # ipvsadm -a -t 192.168.79.110:80 -r
192.168.79.118:80 -g -w 1
[root@Directory ~] # ipvsadm -a -t 192.168.79.110:80 -r
192.168.79.119:80 -g -w 1
[ 删除方法 ]
# ipvsadm -D -t 192.168.79.110:80 -s wrr
# ipvsadm -d -t 192.168.79.110:80 -r 192.168.79.118:80
[ 相关参数说明 ]
[root@Directory ~] # ipvsadm –help
-A 添加虚拟服务器
-t 设置群集地址(
VIP Virtual IP
-s 指定负载调度算法
-a 添加真实服务器
-d 删除真实服务器
-r 指定真实服务器( Real Server )的地址
-m 使用 NAT 模式; -g -i 分别对应 DR TUN 模式
-w 为节点服务器设置权重,默认为 1
4 )查看之前配置
[root@Directory ~] # ipvsadm -L -n
IP Virtual Server version 1 .2.1 (size = 4096 )
Prot LocalAddress:Port Scheduler Flags
- > RemoteAddress:Port Forward Weight ActiveConn
InActConn
TCP 192 .168.79.110:80 wrr persistent 20
- > 192 .168.79.119:80 Route 1 0
0
- > 192 .168.79.118:80 Route 1 0 0
2 )手工在 RS 端绑定 VIP
每台 real server 端执行
[root@RS1 ~] # ifconfig lo:110 192.168.79.110 netmask 255.255.255.255 up
添加本机访问 VIP 的路由
[root@RS1 ~] # route add -host 192.168.79.110 dev lo
3 )手工在 RS 端抑制 ARP 响应
每台 real server 端执行
调整内核参数,关闭 arp 响应
[root@RS1 ~] # echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@RS1 ~] # echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@RS1 ~] # echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@RS1 ~] # echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

 RS1:

echo "web1 " > /usr/share/nginx/html/index.html

RS2:

 echo "web2 " > /usr/share/nginx/html/index.html

 最后在网页测试!

使用nginx配置负载均衡
RHCE    192.168.100.146    负载均衡服务器
node2    192.168.100.148    nginx服务器
node3    192.168.100.149    nginx服务器
 安装nginx可以使用如下仓库:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
 
 关闭防火墙 以及SELinux宽容模式
systemctl  stop   firewalld 
 
setenforce   0
启动服务并向主机 node2 node3 写入内容       
systenctl start nginx    启动nginx服务
 
 echo "web test page ip is `hostname -I`" > /usr/share/nginx/html/index.html   #写入内容
 
systenctl start nginx    再次启动nginx服务
 在RHCE负载均衡服务器上配置
 vim /etc/nginx/nginx.conf 
 
#在 http模块中写入如下内容
 
        server {
                listen  80;
                server_name test.ng.test;    定义域名  也可以用ip
                location / {
                        proxy_pass http://web_server;
                }
        }
        upstream web_server {
                server 192.168.100.148:80;     nginx服务器地址  
                server 192.168.100.149:80;
        }
 
 


 测试结果:

你可能感兴趣的:(linux)