标签:.sh 机器 调度 ati 5.0 mac ann schedule .com
LVS DR 模式搭建三台机器,三台机器均有公网IP。
调度器(director)
IP:192.168.159.131
real server 1 (real1)
IP:192.168.159.132
real server 2 (real2)
IP:192.168.159.133
创建一个脚本:
[root@localhost ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
#开启端口转发
ipv=/usr/sbin/ipvsadm
vip=192.168.159.100
rs1=192.168.159.132
rs2=192.168.159.133
#注意这里的网卡名字
ifdown ens33
ifup ens33
#在此重启网卡的目的是避免重复设置命令行提供的IP
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
#绑定VIP到dir的虚拟网卡ens33:2
route add -host $vip dev ens33:2
#添加网关
$ipv -C
$ipv -A -t $vip:80 -s wrr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
#设置ipvsadm规则,-g=gateway:使用默认网关(DR模式)
执行脚本:
[root@localhost ~]# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 ‘ens33‘。
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
查看地址:
[root@localhost ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:43:65:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.159.131/24 brd 192.168.159.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.159.100/32 brd 192.168.159.100 scope global ens33:2
valid_lft forever preferred_lft forever
inet6 fe80::cfb6:b956:82de:6f77/64 scope link
valid_lft forever preferred_lft forever
// vip 绑定到了ens33 网卡上
分别在real1、real2上配置脚本:
#!/bin/bash
vip=192.168.8.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
脚本定义/lo/arp_ignore和/all/arp_ignore的理解:
*0 - (默认值): 回应任何网络接口上对任何本地IP地址的arp查询请求
脚本定义/lo/arp_announce和/all/arp_announce的理解
执行脚本:
[root@localhost ~]# sh /usr/local/sbin/lvs_rs.sh
// 执行成功后无返回值
查看路由表:
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.159.2 0.0.0.0 UG 100 0 0 ens33
192.168.159.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.159.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
// 可以看见vip的路由
查看ip:
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet 192.168.159.100/32 brd 192.168.159.100 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
// vip 绑定到了lo上。
完整的架构需要两台服务器(角色为dir),分别安装Keepalived工具,目的是实现高可用,但Keepalived本身也有负载均衡功能,所以本次使用可以只安装一台Keepalived。Keepalived内置了ipvsadm的功能,所以不需要安装ipvsadm包,也不用编写和执行lvs_dr脚本。
准备三台机器:
调度器(director)
IP:192.168.159.133;安装keepalived
real server (real1)
IP:192.168.159.131;
real server (real2)
IP:192.168.159.132
安装keepalived:
[root@centos04 ~]# yum install -y keepalived
自定义keepalived配置文件
[root@centos04 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
// 备用服务器上为 BACKUP
state MASTER
// 绑定vip的网卡为ens33,你的网卡和阿铭的可能不一样,这里需要你改一下
interface ens33
virtual_router_id 51
// 备用服务器上为90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.8.100
}
}
virtual_server 192.168.8.100 80 {
// (每隔10秒查询realserver状态)
delay_loop 10
// (lvs 算法)
lb_algo wlc
// 算法(DR模式)
lb_kind DR
// (同一IP的连接60秒内被分配到同一台realserver)
persistence_timeout 0
// (用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.8.134 80 {
// (权重)
weight 100
TCP_CHECK {
// (10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.8.135 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动keepalived 服务:
[root@centos04 ~]# systemctl start keepalived
查看网卡信息:
[root@centos04 ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:bb:0b:5a brd ff:ff:ff:ff:ff:ff
inet 192.168.159.133/24 brd 192.168.159.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.159.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:febb:b5a/64 scope link
valid_lft forever preferred_lft forever
// vip 在ens33网卡上
查看ipvsadm规则:
[root@centos04 ~]# ipvsadm -ln
[root@centos04 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.159.100:80 wlc
-> 192.168.159.131:80 Route 100 0 0
-> 192.168.159.132:80 Route 100 0 0
// 如果规则和rs数量不一致,那可能是没有规则的那台机器没有启动nginx
// 在生产环境下,查看ipvsadm规则时,如果有机器没有规则,则说明那台机器可能宕机了
配置路由转发脚本:
[root@localhost ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.159.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
执行脚本:
[root@real2 ~]# sh /usr/local/sbin/lvs_rs.sh
在浏览器访问VIP:192.168.8.100,刷新网页,访问结果由real1、real2交替回复。
或者使用同网段的主机访问vip,访问结果由real1、real2交替回复。
[root@localhost ~]# curl 192.168.159.100
This is real server 2
[root@localhost ~]# curl 192.168.159.100
this is real server 1
[root@localhost ~]# curl 192.168.159.100
This is real server 2
[root@localhost ~]# curl 192.168.159.100
this is real server 1
标签:.sh 机器 调度 ati 5.0 mac ann schedule .com
原文地址:http://blog.51cto.com/754599082/2067053