环境准备
realServer
rs1:192.168.1.203
rs2:192.168.1.204
Directorserver:192.168.1.201
虚拟IP:192.168.1.222
1.安装依赖包
所有机器都要进行
已经把安装keepalived +lvs软件编译时需要的全部依赖包下载到installpages目录下。
把提供的installpages目录文件放入到所有机器的/usr/local/下。
执行下列语句
rpm -Uvh --force --nodeps /usr/local/installpages/*
2.防火墙配置
停用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl mask firewalld.service
##关闭selinux
setenforce 0
vi /etc/selinux/config
修改SELINUX=enforcing => SELINUX=disabled
配置iptables防火墙
##关闭防火墙
systemctl stop iptables.service
systemctl disable iptables.service
3.realserver配置
只有realserver机器需要配置
拷贝配置环境脚本/real-server.sh 到/etc/init.d/目录下。
##赋执行权限:
chmod +x /etc/init.d/real-server.sh
##因为realserver脚本中用到了/etc/rc.d/init.d/functions,所以一并设置权限。
chmod +x /etc/rc.d/init.d/functions
执行脚本
sh real-server.sh start
查看脚本是否执行成功,执行 ip a 指令,如下图所示就是成功.
4.Directorserver机器安装keepalived
拷贝提供的安装包/keepalived/keepalived-2.0.20.tar.gz到Directorserver的/usr/local/src下。然后执行命令安装keepalived.
sudo su root
cd /usr/local/src
tar -zxvf keepalived-2.0.20.tar.gz
mv keepalived-2.0.20 ../keepalived
cd /usr/local/keepalived/
./configure
make && make install
keepalived配置
##新建目录
mkdir /etc/keepalived //存放keepalied启动的配置文件及健康检查脚本
##把 keepalived的启动文件复制到init.d下,加入开机启动项。
cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
##将keepalived文件拷贝到etc下
cp /usr/local/keepalived/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
##把keepalived加入系统命令目录
cp /usr/local/sbin/keepalived /usr/sbin/
配置文件赋权和修改
拷贝提供的keepalived 配置文件/keepalived/keepalived.conf到/etc/keepalived/下
拷贝提供的keepalived 配置文件/keepalived/keepalivedcheck.sh到/etc/keepalived/下
cd /etc/keepalived
mv keepalived.conf keepalived-back.conf //备份配置文件
chmod +x keepalivedcheck.sh //赋执行权限
keepalived启动与测试
在directorserver机器上启动keepalived
echo "1" > /proc/sys/net/ipv4/ip_forward //打开转发配置
route add -host 192.168.1.222 dev enp0s3 //添加路由信息
启动|重启|停止
/etc/init.d/keepalived start|restart|stop
或
systemctl start|restart|stop keepalived
检查keepalived是否拉起负载均衡:
查看连接情况
脚本内容
real-server.sh
#!/bin/sh
VIP=192.168.41.222
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "Start LVS of RealServer"
/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/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
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del -host $VIP dev lo:0
echo "Stop LVS of RealServer"
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 "Usage:$0{start|stop}"
exit 1
esac
keepalivedcheck.sh
#!/bin/bash
## IP : 检测得IP
#USER :登录得用户 root
#PASS :用户密码
#PORT : 检测端口
IP=$1
USER=$2
PASS=$3
PORT=$4
checkport=`sshpass -p "$PASS" ssh $USER@$IP "netstat -tunlp |grep $PORT"`
KEYY=X
if [[ X$checkport = $KEYY ]]
then
exit 1
fi
if [[ $checkport =~ $PORT ]]
then
exit 0
else
exit 1
fi
此脚本是健康检查脚本,此处只是监控端口,可以根据实际需求进行修改.
注:exit 0 代表正常退出 ;exit 1 代表异常退出.
配置文件
global_defs {
router_id LVS_matser
}
vrrp_sync_group VGM {
group {
VI_CACHE
}
}
vrrp_instance VI_CACHE {
state MASTER
interface p2p1
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.222
}
}
virtual_server 192.168.1.222 7777 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 11
protocol TCP
real_server 192.168.1.203 7777 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/keepalivedcheck.sh 192.168.1.203 root root 7777"
misc_timeout 5
}
}
real_server 192.168.1.204 7777 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/keepalivedcheck.sh 192.168.1.204 root root 7777"
misc_timeout 5
}
}
}
提到的程序包:
链接:https://pan.baidu.com/s/1F5W1MF7vt4sX9Df_uMt9Vg
提取码:xg45