欢迎访问IT教程网

LVS+keepalived高可用

Linux admin 129℃ 0评论

环境部署

192.168.57.3          lvs1
192.168.57.4          lvs2
192.168.57.100        vip
192.168.57.5          realserver1
192.168.57.6           realserver2

在lvs1和lvs2上安装lvs和keepalived

yum install ipvsadm keepalived -y

在realserver1和realserver2上安装nginx

[root@localhost ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable] 
name=nginx stable repo 
baseurl=http://nginx.org/packages/centos/7/$basearch/ 
gpgcheck=1 
enabled=1 
gpgkey=https://nginx.org/keys/nginx_signing.key
yum install nginx -y

配置nginx并启动

[root@realserver2 ~]# echo realserver2 >/usr/share/nginx/html/index.html 
[root@realserver2 ~]# systemctl start nginx
[root@realserver2 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@realserver2 ~]# 
[root@realserver1 ~]# echo realserver1 >/usr/share/nginx/html/index.html
[root@realserver1 ~]# systemctl start nginx
[root@realserver1 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@realserver1 ~]# 

配置keepalived

[root@lvs1 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
notification_email {
root@local              #邮件通知人
xxx@xx.com
}
notification_email_from root@local
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER           #从节点为BACKUP
interface enp0s8
virtual_router_id 50
priority 100           #从节点要小于这个数,比如90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.57.100/24 dev enp0s8

}
}

virtual_server 192.168.57.100 80 {
delay_loop 2
lb_algo wrr
lb_kind DR
protocol TCP
persistence_timeout 0
real_server 192.168.57.5 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.57.6 80 {
weight 1
TCP_CHECK {
connect_timeout 3
connet_port 80
nb_get_retry 3
delay_before_retry 3
}
}
}

启动keepalived

[root@lvs1 ~]# systemctl start keepalived
[root@lvs1 ~]# systemctl enable keepalived

查看lvs

[root@lvs1 ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP lvs1:http wrr
-> 192.168.57.5:http Route 2 0 0 
-> 192.168.57.6:http Route 1 0 0 
[root@lvs1 ~]# 

查看网卡绑定

[root@lvs1 ~]# ip addr | grep enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.57.3/24 brd 192.168.57.255 scope global enp0s8
inet 192.168.57.100/24 scope global secondary enp0s8

配置lvs脚本,在两台realserver上

[root@realserver1 init.d]# cat lvs-web.sh 
#!/bin/sh
VIP=192.168.57.100
. /etc/rc.d/init.d/functions

case $1 in

start)
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/sysctl -p >/dev/null 2>&1
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up 
/sbin/route add -host $VIP dev lo:0
echo LVS-DR real server starts successfully.n
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
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
echo LVS-DR real server stopped.n
;;
status)
isLoOn=`/sbin/ifconfig lo:0 | grep $VIP`
isRoOn=`/bin/netstat -rn | grep $VIP`
if [ $isLoON ==  -a $isRoOn ==  ]; then
echo LVS-DR real server has run yet.
else
echo LVS-DR real server is running.
fi
exit 3
;;
*)
echo Usage: $0 {start|stop|status}
exit 1
esac
exit 0

添加执行权限并启动

chmod +x  lvs-web.sh
sh lvs-web.sh start

至此,完成所有配置, 使用curl http://192.168.57.100测试是否可以调度

本文固定链接:IT教程网 » LVS+keepalived高可用
本站内容除特别标注外均为原创,欢迎转载,但请保留出处!

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址