以下服务器:
....:100::10 mysql1 master
....:100::20 mysql2 master
....:100::30 loadbalancer (keepalived) with virtual ip ...:100::40
配置:
vrrp_instance V1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 150
advert_int 1
smtp_alert
virtual_ipaddress {
...:100::40
}
}
virtual_server ...:100::40 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server ...:100::10 3306 {
weight 50
TCP_CHECK {
connect_timeout 3
connect_port 3306
}
}
real_server ...:100::20 3306 {
weight 10
TCP_CHECK {
connect_timeout 3
connect_port 3306
}
}
}
直接与两个服务器的连接均正常:(bind-address = ::
并且没有激活 ip6tables 防火墙)
$ mysql -uroot -p -h...:100::10
Welcome to the MySQL monitor. Commands end with ; or \g.
$ mysql -uroot -p -h...:100::20
Welcome to the MySQL monitor. Commands end with ; or \g.
...:100:30 当然不是:
$ mysql -uroot -p -h...:100::30
ERROR 2003 (HY000): Can't connect to MySQL server on '...:100::30' (111)
不幸的是我超时了...:100::40
$ mysql -uroot -p -h...:100::40
ERROR 2003 (HY000): Can't connect to MySQL server on '...:100::40' (110)
ipvsadm 对我来说看起来不错:
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP [...:100::40]:mysql wrr
-> [...:100::10]:mysql Route 50 0 0
# ifconfig
eth0 Link encap:Ethernet HWaddr ..........
inet6 addr: ............/64 Scope:Link
inet6 addr: ...:100::30/64 Scope:Global
inet6 addr: ...:100::40/128 Scope:Global
我现在的问题是,keepalived 没有监听端口 3306:
# netstat -nlta
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
# keepalived --help
Keepalived v1.2.2 (12/18,2011)
错误在哪里?
答案1
每个 realserver 都需要更改配置,否则它将丢弃网络数据包,因为它没有响应虚拟 IP 地址(例如...:100:40)
# nano /etc/sysctl.conf
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
或直接
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
需要将虚拟 IP 地址添加到本地环回:
ip addr add 2a00:14e0:600:1200:100::40 dev lo
并编辑 /etc/network/interfaces 以便重启后更改仍然有效:
iface eth0 inet6 static
address ...:100::20
netmask 64
gateway ...::1
post-up ip addr add ...:100::40 dev lo
目前,第一次连接超时,但第二次连接工作正常。我会测试一下。