LVS的理论基础请点击下面的网址:
http://my.oschina.net/u/1454868/blog/208269
网络架构:
direct server:10.10.54.85(3306)
backup:10.10.54.84
vip:10.10.54.89(eth0)
real server:10.10.54.86(80,3306)
real server:10.10.54.88(80,3306)
mysql主从搭建,根据上面的网络架构搭建:
搭建主从mysql
(1)规划主从主机
主机:10.10.54.85
从机1:10.10.54.86
从机2:10.10.54.88
(2)更改主机配置文件
[root@nan85 etc]# vim /etc/my.cnf
log-bin=master-bin
binlog_format=mixed
server-id=1
(3)更改从机配置文件
从1
[root@nan86 ~]# vim /etc/my.cnf
log-bin=slave-bin
binlog_format=mixed
server-id=10
从2
[root@nan88 ~]# vim /etc/my.cnf
log-bin=slave-bin
binlog_format=mixed
server-id=11
在slave1和slave2上重启mysql
# /etc/init.d/mysqld restart
(4)在master上创建复制用户,并授予权限
mysql> grant replication slave on *.* to 'emp1'@'10.10.54.86' identified by 'emp1';
mysql> flush privileges;
mysql> grant replication slave on *.* to 'emp2'@'10.10.54.88' identified by 'emp2';
mysql> flush privileges;
(5)查看master上二进制日志和position位置
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000006 | 990 | | |
+-------------------+----------+--------------+------------------+
(6)备份master上的数据,把备份master数据库还原到从库上
(7)在slave上面change master操作
从1:
mysql> change master to master_host='10.10.54.85',master_user='emp1', master_password='emp1', master_log_file='master-bin.000006',master_log_pos=990;
从2:
mysql> change master to master_host='10.10.54.85',master_user='emp2',master_password='emp2',master_log_file='master-bin.000006',master_log_pos=990;
(8)在slave上启动slave
从1:
mysql> start slave;
从2:
mysql> start slave ;
(9)查看slave状态
确定slave上的I/O线程和SQL线程状态为YES
从1:
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
从2:
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
搭建LVS的DR模式(MASTER-BACKUP-DR):
1.网络架构
direct server:10.10.54.85(3306)
backup:10.10.54.84
vip:10.10.54.89(eth0)
real server:10.10.54.86(80,3306)
real server:10.10.54.88(80,3306)
2.在MASTER DIRECTOR SERVER上编译ipvsadm,keepalived
(1)下载软件
//本机内核版本:
# uname -r
2.6.32-358.el6.x86_64
下载地址: --相应版本
http://www.linuxvirtualserver.org/software/ipvs.html#kernel-2.6
ipvsadm-1.26.tar.gz
keepalived
http://www.keepalived.org/download.html
keepalived-1.2.9.tar.gz
(2)编译安装ipvsadm
//安装依赖包
[root@nan85 ipvsadm-1.26]# yum list|grep kernel-dev
[root@nan85 ipvsadm-1.26]# yum list|grep popt
[root@nan85 lvs]# yum install popt-static.x86_64 popt-devel.x86_64 popt.x86_64
[root@nan85 lvs]# yum -y install wget libnl* popt* gcc.x86_64 gcc-c++.x86_64 gcc-objc++.x86_64 kernel-devel.x86_64 make popt-static.x86_64
//确认模块
[root@nan85 lvs]# modprobe -l|grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
//编译keepalived-1.2.9
[root@nan85 lvs]# tar xvf keepalived-1.2.9.tar.gz
需要安装基础软件包openssl和popt
yum install net-snmp.x86_64 net-snmp-devel.x86_64
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --enable-snmp --sysconfdir=/etc/
Keepalived version : 1.2.9
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -Wl,-z,relro -Wl,-z,now -L/usr/lib64 -lnetsnmpagent -lnetsnmphelpers -lnetsnmpmibs -lnetsnmp -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -lssl -lcrypto -lcrypt -lnl
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : Yes
SHA1 support : No
Use Debug flags : No
###################################################################################
出现错误:
configure: error: unable to find net-snmp-config
解决方法:
[root@nan85 keepalived-1.2.9]# yum install net-snmp.x86_64 net-snmp-devel.x86_64
###################################################################################
[root@nan85 keepalived-1.2.9]# make && make install
[root@nan85 ~]# cp /usr/local/keepalived/sbin/keepalived /sbin/
[root@nan85 ~]# cp /usr/local/keepalived/bin/genhash /bin/
[root@nan85 ~]# cd /etc/keepalived/
[root@nan85 keepalived]# ls
keepalived.conf samples --keepalived软件不对keepalived语法进行检查,在编辑前需要备份
3.在BACKUP DIRECTOR SERVER上编译ipvsadm,keepalived
在两个从上
//安装依赖包
# yum list|grep kernel-dev
# yum list|grep popt
# yum install popt-static.x86_64 popt-devel.x86_64 popt.x86_64
# yum -y install wget libnl* popt* gcc.x86_64 gcc-c++.x86_64 gcc-objc++.x86_64 kernel-devel.x86_64 make popt-static.x86_64
//确认模块
# modprobe -l|grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
//编译keepalived-1.2.9
# tar xvf keepalived-1.2.9.tar.gz
需要安装基础软件包openssl和popt
yum install net-snmp.x86_64 net-snmp-devel.x86_64
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --enable-snmp --sysconfdir=/etc/
Keepalived version : 1.2.9
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -Wl,-z,relro -Wl,-z,now -L/usr/lib64 -lnetsnmpagent -lnetsnmphelpers -lnetsnmpmibs -lnetsnmp -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -lssl -lcrypto -lcrypt -lnl
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : Yes
SHA1 support : No
Use Debug flags : No
###################################################################################
出现错误:
configure: error: unable to find net-snmp-config
解决方法:
# yum install net-snmp.x86_64 net-snmp-devel.x86_64
###################################################################################
# make && make install
# cp /usr/local/keepalived/sbin/keepalived /sbin/
# cp /usr/local/keepalived/bin/genhash /bin/
# cd /etc/keepalived/
# ls
keepalived.conf samples --keepalived软件不对keepalived语法进行检查,在编辑前需要备份
4.配置MASTER DIRECTORY SERVER并通过测试
[root@nan85 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
lisn@shiwei.com
}
notification_email_from lisn@shiwei.com
smtp_server mail.shiwei.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.54.89/24 dev eth0 label eth0:1
}
}
virtual_server 10.10.54.89 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 10.10.54.86 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.10.54.88 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 10.10.54.89 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 10.10.54.86 3306 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.10.54.88 3306 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
[root@nan85 keepalived]# /etc/init.d/keepalived restart
5.复制MASTER DIRECTORY SERVER配置文件到BACKUP DIRECTORY SERVER
把主上的即10.10.54.85上的配置文件复制到10.10.54.84上,然后做如下修改
[root@nan84 keepalived]# pwd
/etc/keepalived
[root@nan84 keepalived]# vim keepalived.conf
修改SLVAE config:
router_id LVS_SLAVE
state BACKUP
priority 89
6.测试LVS高可用
(1)停止master
在BACKUP上面查看54.89时(vip)是否漂移过来
[root@nan85 lvs]# /etc/init.d/keepalived stop
//非direct server上
telnet 10.10.54.89 3306
[root@nan84 keepalived]# ifconfig
(2)开启MASTER
在MASTER上查看54.89(vip)是否漂移过来
[root@nan85 keepalived]# /etc/init.d/keepalived start
[root@nan85 keepalived]# ifconfig
//非direct server上
telnet 10.10.54.89 3306