本文主要记录下lvs和keepalived安装配置以及使用。
主机ip | 角色 | 安装软件 |
10.135.112.92 | Lvs+keepalived MASTER | Ipvsadm keepalived |
10.135.112.91 | Real server | 测试用web服务 |
10.135.112.199 | VIP |
一、安装LVS
10.135.112.92 上执行:
yum install ipvsadm –y
ipvsadm (执行ipvsadm(modprobe ip_vs)把ip_vs模块加载到内核)
lsmod |grep ip_vs #查看IPVS加载到内核没有
[root@localhost ~]# yum install ipvsadm –y
[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@localhost ~]# lsmod |grep ip_vs
ip_vs_wrr 12697 0
ip_vs 141092 2 ip_vs_wrr
nf_conntrack 133387 1 ip_vs
libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
二、安装keepalived
10.135.112.92上安装keepalived:
yum install -y keepalived (需要提前安装epel)
配置keepalived,vi /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL-1
}
vrrp_instance VI_1 {
state MASTER
interface ens2f0
virtual_router_id 77
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.135.112.199
}
}
virtual_server 10.135.112.199 12345 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 10.135.112.91 12345 {
weight 1
TCP_CHECK {
connect_port 12345
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
开启路由转发,在10.135.112.92执行:
echo 1 > /proc/sys/net/ipv4/ip_forward
启动服务,在10.135.112.92执行:
service keepalived start
keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。可以执行 ps aux|grep keepalived 查询进程信息,如下:
[root@localhost ~]# ps aux|grep keepalived
root 398152 0.0 0.0 118616 1372 ? Ss 08:58 0:00 /usr/sbin/keepalived -D
root 398153 0.0 0.0 118736 2588 ? S 08:58 0:00 /usr/sbin/keepalived -D
root 398154 0.0 0.0 118616 1860 ? S 08:58 0:00 /usr/sbin/keepalived -D
root 398684 0.0 0.0 112660 972 pts/1 S+ 08:59 0:00 grep --color=auto keepalived
三、配置Real Server机器
在10.135.112.91上创建一个脚本/opt/lv.sh,内容如下:
VIP=10.135.112.199
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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
sysctl -p >/dev/null 2>&1
然后运行lv.sh脚本,运行脚本后再10.135.112.91上通过ifconfig查看信息,能看到如下:
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 10.135.112.199 netmask 255.255.255.255
loop txqueuelen 1 (Local Loopback)
四、测试
10.135.112.91上对应12345端口的web服务启动后,在10.135.112.92上通过命令ipvsadm -ln能查看到对应规则,如下:
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.135.112.199:12345 wrr
-> 10.135.112.91:12345 Route 1 8 30
在10.135.112.92上运行ip a命令,能看到VIP地址绑定到网卡上,如下:
[root@localhost ~]# ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens2f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq portid 6805caa2795c state UP qlen 1000
link/ether 68:05:ca:a2:79:5c brd ff:ff:ff:ff:ff:ff
inet 10.135.112.92/20 brd 10.135.127.255 scope global ens2f0
valid_lft forever preferred_lft forever
inet 10.135.112.199/32 scope global ens2f0
valid_lft forever preferred_lft forever
inet6 fe80::6a05:caff:fea2:795c/64 scope link
valid_lft forever preferred_lft forever