作者:学海无涯 | 来源:互联网 | 2023-07-01 14:25
创建脚本etcinit.drealserver#geditetcinit.dmyservice#chkconfig:23451090#description:myservice..
创建脚本 /etc/init.d/realserver
#gedit /etc/init.d/myservice
# chkconfig: 2345 10 90
# description: myservice ...
SNS_VIP=10.28.0.130
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>$1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Start Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit0
解释
SNS_VIP=10.28.0.130 #定义VIP变量
. /etc/rc.d/init.d/functions #导脚本库
case "$1" in #case语句 $1传递给该shell脚步的第一个参数
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP #设置lo: 0 VIP netmask 及广播
/sbin/route add -host $SNS_VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>$1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Start Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit0
给脚本赋予权限
chmod 750 realserver
chkconfig realserver on #设置开机自启动
service realserver start
搭建负载均衡
1.安装ipvsadm keepalived
如果是centos内核2.6以上,自带ipvsadm
yum -y install ipvsadm keepalived
配置keepalived
> /etc/keepalived/keepalived.conf #覆盖掉原来的配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# otification_email_from Alexandre.Cassen@firewall.loc
# mtp_server 192.168.200.1
# mtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.28.0.130
# 192.168.200.17
# 192.168.200.18
}
}
virtual_server 10.28.0.130 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 10.28.0.133 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.28.0.134 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
解释
global_defs {
router_id LVS_DEVEL_1 //本路由器(服务器)的名称
}
vrrp_instance VI_1 { //定义vrrp热备实例
state MASTER //热本状态,MASTER表示主服务器 备份服务器设置为BACKUP
interface eth0 //承载vip的物理端口
virtual_router_id 51 //虚拟路由器的ID号,每个热备组保持一致
priority 150 //优先级,数值越大优先级越高,这里应该主服务器高于辅助的 BACKUP可以设置的比主服务器小
advert_int 1 //通告间隔秒数(心跳频率)
authentication { //认证信息,每个热备组保持一致
auth_type PASS //认证类型
auth_pass 1111 //认证密码
}
virtual_ipaddress { //指定漂移地址(vip),可以有多个
192.168.1.198
}
}
virtual_server 192.168.1.198 80 { //虚拟服务器地址(vip)、端口
delay_loop 6 //健康检查间隔时间
lb_algo rr //轮询(rr)调度算法
lb_kind DR //直接路由模式(DR)
protocol TCP //应用服务采用的模式是TCP模式
real_server 192.168.1.191 80 { //第一个Web节点的地址、端口
weight 1 //节点的权重
TCP-CHECK { //健康检查方式
connect_timeout 3 //连接超时(秒)
nb_get_retry 3 //重试次数
delay_before_retry 3 //重试间隔(秒)
connect_port 80 //检查的目标端口
}
}
real_server 192.168.1.192 80 { //第二个Web节点
weight 1
TCP-CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动keepalived
service keepalived start
查看启动日志
tail -f /var/log/messages
访问10.28.0.130