理论基础请点击下面的网址:
http://my.oschina.net/u/1454868/blog/208269
1.网络架构
hostname:
direct server:10.10.54.85(3306)
vip:10.10.54.89
real server:10.10.54.86(80,3306)
real server:10.10.54.88(80,3306)
2.在10.10.54.86/88两台主机上安装nginx
//编译nginx1.4.5
# wget http://nginx.org/download/nginx-1.4.5.tar.gz
# tar xvf nginx-1.4.5.tar.gz
# cd nginx-1.4.5
#./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module && make && make install
//启动
#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一次启动错误:
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] getpwnam("apache") failed
解决方法:
# useradd apache
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//nginx配置文件格式
user apache apache;
worker_processes 2;
//测试配置文件
#/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
3.安装ipvsadm,keepalived-1.2.9
(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,keepalived-1.2.9
//配置yum源
[root@nan85 lvs]# vim /etc/yum.repos.d/CentOS-ftp.repo
[Packages]
name=Packages
baseurl=ftp://10.201.1.221/Packages
gpgcheck=0
enable=1
[Packages2]
name=Packages2
baseurl=ftp://10.201.1.221/Packages2
gpgcheck=0
enable=1
#yum clean all
//安装依赖包
[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
./configure --prefix=/usr/local/keepalived --enable-snmp --sysconfdir=/etc/
出现错误:
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
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
[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/
4.配置real server
即配置10.10.54.86/88
在两台real server上做如下操作:
[root@nan86 ~]# vim /etc/init.d/realserver
#!/bin/bash
#description:start realserver
#script_name:realserver_config
VIP=10.10.54.89
source /etc/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
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
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
//上面代码解释
aro_ignore:默认值为0
0 --为默认值,回应任何网络接口对本地IP地址的ARP查询请求。
1 --只回答目标IP地址时来访问网络接口本地地址的ARP查询请求。
arp_announce:对网络接口上,本地IP地址发出的ARP请求,做出相应的级别的限制:确定不同程度的限制,宣布对来自本地源IP地址发出的APR请求的接口。
0 --为默认值,在任意网络接口上的任何本地地址。
2 --对查询目标使用最适当的本地地址。
[root@nan86 ~]# chmod +x /etc/init.d/realserver
[root@nan86 ~]# /etc/init.d/realserver start
5.配置direct server
在direct server上编译ipvsadm,keepalived软件包
//配置文件
相当与LVS的外壳
[root@nan85 keepalived]# pwd
/etc/keepalived
在direct server上编译ipvsadm,keepalived软件包
//配置文件
[root@tech2 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
yangry@shiwei.com
}
notification_email_from yangry@shiwei.com
smtp_server mail.shiwei.com
smtp_connect_timeout 30
router_id LVS_MASTER1
--表示运行kee 服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_instance VI_2 { --vrrp实例
state MASTER --master主机,从用BACKUP标识
interface eth0
virtual_router_id 51 --虚拟路由标识,是一个数字,
--同一个VRRP实例使用唯一的标识
priority 100 --优先级主从模式,BACKUP小于100
advert_int 1 --同步检查的时间间隔,检查时间间隔1秒
authentication {
auth_type PASS --PASS和AH验证密钥
auth_pass 1111 --密码
}
virtual_ipaddress { --虚拟IP
10.10.54.233/24 dev eth0 label eth0:1
}
}
virtual_server 10.10.54.233 80 {
delay_loop 6 --设置运行情况检查时间,单位为s
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50 --会话保持时间
protocol TCP
real_server 10.10.54.222 80 { --负载均衡真正的主机
weight 1 --权值
TCP_CHECK {
connect_timeout 3
nb_get_retry 3 --重试的次数
delay_before_retry 3 --重试的间隔
connect_port 80
}
}
real_server 10.10.54.226 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
6.搭建主从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
(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> slave start;
从2:
mysql> slave start;
(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
7.测试
非10.10.54.85上
telnet 10.10.54.89 80 --10.10.54.89为VIP
telnet 10.10.54.89 3306
//查看当前链接数
# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.54.233:80 rr persistent 50
-> 10.10.54.222:80 Route 1 0 0
-> 10.10.54.226:80 Route 1 0 0
TCP 10.10.54.233:3306 rr persistent 50
-> 10.10.54.226:3306 Route 1 1 0
-> 10.10.54.228:3306 Route 1 0 0