最近对lvs、keepalived进行了安装、配置熟悉,对过程进行整理同时记录遇到的问题。此处只做单独的lvs、keepalived的配置,假设后台服务已配置完成。
1、需要的安装包:ipvsadm、keepalived
2、安装规划:
主机 | 真实IP | VIP |
master | 10.20.13.153 | 10.20.13.101 |
backup | 10.20.13.231 | 10.20.13.101 |
real server | 10.20.13.85 |
3、lvs的安装配置:
如果是tar的包使用:make&&make install
如果使用的是rpm包:rpm -ivh rpm包
4、验证ipvsadm是否安装成功
查看pvsadm --help命令是否能执行
5、分别为主机配置vip:
ifconfig eth0:1 10.20.13.101 netmask 255.255.255.0 此种方式网卡重启后失效,如果永久生效可以在/etc/sysconfig/network-script/下安装eth0的复制一份修改保存
6、配置lvs:
mster服务器
ipvsadm -A -t 10.20.13.153:80 -s rr -p 30 # -A 增加 -s 指定算法 -t 使用tcp协议 -p 指定用户超时时间
ipvsadm -a -t 10.20.13.153:80 -r 10.20.13.85:80 -g -r指定real server -g 采用dr模式
backup服务器
ipvsadm -A -t 10.20.13.153:80 -s rr -p 30 # -A 增加 -s 指定算法 -t 使用tcp协议 -p 指定用户超时时间
ipvsadm -a -t 10.20.13.231:80 -r 10.20.13.85:80 -g -r指定real server -g 采用dr模式
说明ipvsadm -c是清除
7、配置完后查看
ipvsadm -n -L
[root@DEV-L002595 ipvsadm-1.26]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.13.101:http rr persistent 50
-> 10.20.13.85:http Route 1 0 0
9、keepalived的安装
./configure --prefix=/home/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/ #prefix指定安装路径,with-kernel-dir指定系统内核
需要指定内核,否则会少一个进程,正确的是有3个进程。当少一个进程的时候是编译时内核不对,或sbin中的keepalived未放。通过which keepalived查看生效的路径。
make&&make install
cp /home/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
/home/keepalived//etc/sysconfig/keepalived /etc/sysconfig/
cp /home/keepalived/sbin/keepalived /usr/sbin/
cp /home/keepalived/sbin/keepalived /sbin/
cp /home/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
10、配置keepalived.conf (两个文件内容类似,只需要修改标红处)
[root@DEV-L002595 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_2
}
vrrp_instance VI_2 {
state BACKUP #设置主备
interface eth0 #监听的网卡
virtual_router_id 100
priority 100 优先级,master大于backup
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.20.13.101
}
}
virtual_server 10.20.13.101 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.20.13
.85 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
开启服务:chkconfig keepalived on
启动服务:service keepalived start|stop|restart
keepalived日志:/var/log/message
查看lvs:ipvsadm -n -L
说明:当使用keepalived时5、6步无需配置,keepalived.conf已配置会自动加载
哪个lvs生效,vip在哪个主机上可以查看到,当发生问题或变化的时候message中的日志才有滚动
[root@property-service-01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 06:40:f0:00:07:33 brd ff:ff:ff:ff:ff:ff
inet 10.20.13.153/22 brd 10.20.15.255 scope global eth0
inet 10.20.13.101/32 scope global eth0
inet6 fe80::440:f0ff:fe00:733/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 06:35:60:00:02:9f brd ff:ff:ff:ff:ff:ff
[root@property-service-01 ~]#
keepalived的日志如下:
Jan 12 15:26:34 localhost Keepalived_healthcheckers: Activating healtchecker for service [10.20.14.85:80]
Jan 12 15:26:35 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Transition to MASTER STATE
Jan 12 15:26:35 localhost Keepalived_healthcheckers: TCP connection to [10.20.14.85:80] failed !!!
Jan 12 15:26:35 localhost Keepalived_healthcheckers: Removing service [10.20.14.85:80] from VS [10.20.13.101:80]
Jan 12 15:26:35 localhost Keepalived_healthcheckers: Lost quorum 1-0=1 > 0 for VS [10.20.13.101:80]
Jan 12 15:26:36 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Entering MASTER STATE
Jan 12 15:26:36 localhost Keepalived_vrrp: VRRP_Instance(VI_2) setting protocol VIPs.
Jan 12 15:26:36 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 10.20.13.101
Jan 12 15:26:38 localhost ntpd[1509]: Listening on interface #36 eth0, 10.20.13.101#123 Enabled
Jan 12 15:26:41 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 10.20.13.10