centos7配置lvs+keepalived高可用生产环境配置

环境:
centos7配置lvs+keepalived高可用生产环境配置_第1张图片
192.168.5.100 vip
192.168.5.103 lvs主机
192.168.5.108 lvs从机
192.168.5.104 tomcat1
192.168.5.105 tomcat2
1.安装ipvsadm(管理工具)+keepalived
yum -y install ipvsadm keepalived
2.创建lvs主备服务器运行脚本程序
[root@localhost ~]# vim /sbin/lvsdr.sh
#!/bin/bash
VIP=192.168.5.100
RIP1=192.168.5.104
RIP2=192.168.5.105

/etc/rc.d/init.d/functions
case “$1” in
start)
echo “start LVS of DirectorServer”
#Set the Virtual IP Address
/sbin/ifconfig ens33:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev ens33:1
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs Add route link
/sbin/ipvsadm -A -t $VIP:80 -s wrr -p 60
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g

/sbin/ipvsadm -A -t $VIP:80 -s wrr -p 60

/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g

/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g

   #Run Lvs
   /sbin/ipvsadm

;;
stop)
echo “Close LVS Directorserver”
/sbin/ifconfig ens33:1 down
/sbin/ipvsadm -C
;;
*)
echo “Usage0{start|stop}”
exit 1
esac
3.添加权限并执行
[root@localhost ~]# chmod 755 /sbin/lvsdr.sh
[root@localhost ~]# /sbin/lvsdr.sh start
[root@localhost ~]# ipvsadm -ln
4.添加到开机启动
[root@localhost ~]# vim /etc/rc.local
/sbin/lvsdr.sh start #追加此纪录
5.将配置程序SCP到backup,并执行4、5步骤
[root@localhost ~]# scp /sbin/lvsdr.sh 192.168.0.62:/sbin/lvsdr.sh
6.tomcat1创建real服务器运行脚本程序
[root@localhost ~]# vim /sbin/realdr.sh

#!/bin/bash
VIP=192.168.0.60
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo “1”>/proc/sys/net/ipv4/conf/default/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/default/arp_announce
echo “1”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p
7.分配权限并执行
[root@localhost ~]# chmod 755 /sbin/realdr.sh
[root@localhost ~]# /sbin/realdr.sh start
[root@localhost ~]# ifconfig
可查看到lo:0上已经有了vip地址
(注:最小化安装系统ifconfig命令没有,可yum -y install net-tools进行安装)
8.设置此real运行程序自启动
[root@localhost ~]# vim /etc/rc.local
/sbin/realdr.sh start #追加此内容
9.将此real运行程序scp到tomcat2,并执行8、9步骤
10.配置keepalived
global_defs{ router_id LVS_DEVEL}vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.100
}
}

virtual_server 192.168.5.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.5.104 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.5.105 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
11.将master的keepalived配置文件scp到backup
[root@localhost ~]# scp /etc/keepalived/keepalived.conf 192.168.0.62:/etc/keepalived/keepalived.conf
修改名称、id以及优先级
12.开通防火墙端口,或者关闭防火墙
此为防火墙在开启状态,访问被阻,关闭防火墙或者允许页面端口通过都可以。
实验环境可关闭防火墙:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
本人搭建一个运维经验分享网站,有好的运维技术和经验欢迎分享到博客地址:http://www.jkkcss.cn/

你可能感兴趣的:(centos7配置lvs+keepalived高可用生产环境配置)