系统:centos6.5mini
环境:
机器名 |
Ip地址 |
角色 |
Vip-web: 192.168.20.50 Vip-mysql: 192.168.20.60 |
lvs01 |
192.168.20.100 |
Lvs |
|
lvs02 |
192.168.20.200 |
Lvs |
|
Web01 |
192.168.20.10 |
Web |
|
Web02 |
192.168.20.20 |
Web |
|
Mysql01 |
192.168.20.30 |
Mysql |
|
Mysql02 |
192.168.20.40 |
Mysql |
注: 这个实验做的是web和mysql 负载均衡
1.各个机器关掉iptables,selinux,如下命令
[root@lvs01 ~]# service iptables stop
[root@lvs01 ~]# chkconfig iptables off
[root@lvs01 ~]# getenforce
Disabled
2.web01和web02 上安装web服务,将VIP绑定到真实服务器的网口eth0,修改sysctl.conf
[root@web01 ~]# yum install -y httpd
[root@web01 ~]# /etc/init.d/httpd restart
[root@web01 ~]# chkconfig httpd on
[root@web01 ~]# cat /var/www/html/index.html
web01
[root@web01 ~]# ip addr add 192.168.20.50/24 dev eth0
[root@web01 ~]# vi /etc/rc.d/rc.local
ip addr add 192.168.20.50/24 dev eth0
[root@web01 ~]# vi /etc/sysctl.conf
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web01 ~]# sysctl -p
[root@web02 ~]# yum install -y httpd
[root@web02 ~]# /etc/init.d/httpd restart
[root@web02 ~]# chkconfig httpd on
[root@web02 ~]# cat /var/www/html/index.html
web2
[root@web02 ~]# ip addr add 192.168.20.50/24 dev eth0
[root@web02 ~]# vi /etc/rc.d/rc.local
ip addr add 192.168.20.50/24 dev eth0
[root@web01 ~]# vi /etc/sysctl.conf
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web01 ~]# sysctl -p
3.在mysql01和mysql02上安装mysql 服务,在mysql01上创建mysql01,并且赋予用户myusr01从任何服务器登录的权限,而在mysql02上创建mysql02,并且赋予用户myusr01从任何服务器登录的权限。将VIP绑定到真实服务器的网口eth0,修改sysctl.conf。
[root@mysql01 ~]# yum install -y mysql mysql-server
[root@mysql01 mysql]# /etc/init.d/mysqld start
[root@mysql01 mysql]# chkconfig mysqld on
[root@mysql01 ~]# mysql -uroot -p
Enter password:
mysql> create database mysql01;
mysql> grant all privileges on *.* to 'myusr01'@'%' identified by '00000000' with grant option;
mysql> flush privileges;
mysql> quit;
[root@mysql01 ~]# ip addr add 192.168.20.60/24 dev eth0
[root@mysql01 ~]# vi /etc/rc.d/rc.local
ip addr add 192.168.20.60/24 dev eth0
[root@mysql01 ~]# vi /etc/sysctl.conf
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@mysql01 ~]# sysctl -p
[root@mysql02 ~]# yum install -y mysql mysql-server
[root@mysql02 mysql]# /etc/init.d/mysqld start
[root@mysql02 mysql]# chkconfig mysqld on
[root@mysql02 ~]# mysql -uroot -p
Enter password:
mysql> create database mysql02;
mysql> grant all privileges on *.* to 'myusr01'@'%' identified by '00000000' with grant option;
mysql> flush privileges;
mysql> quit;
[root@mysql01 ~]# ip addr add 192.168.20.60/24 dev eth0
[root@mysql01 ~]# vi /etc/rc.d/rc.local
ip addr add 192.168.20.60/24 dev eth0
[root@mysql01 ~]# vi /etc/sysctl.conf
net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@mysql01 ~]# sysctl -p
4. 在lvs01和lvs02 上安装lvs+keepalived
[root@lvs01 ~]#yum install ipvsadm keepalived –y
[root@lvs02 ~]#yum install ipvsadm keepalived –y
5.Keepalived的配置
[root@lvs01 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int 1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.20.50
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id
priority
advert_int 1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.20.60
}
}
virtual_server 192.168.20.50 80{
delay_loop 6 #健康检查时间间隔,单位s
lb_algo rr #负载均衡调度算法设置为加权轮叫
lb_kind DR # LVS工作机制,这里是DR模式
nat_mask 255.255.255.0 #网络掩码,DR模式要保障真是服务器和lvs在同一网段
persistence_timeout 0 #会话保持时间,单位s,测试时数值尽量小一点,轮询结果明显。
protocol TCP #协议
real_server 192.168.20.10 80{ #设定真实服务器地址以及端口,
weight 1 # 设置权重,数字越低,调度的比例越小。
}
real_server 192.168.20.20 80{
weight 1
}
}
virtual_server 192.168.20.60 3306{
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.20.30 3306{
weight 1
}
real_server 192.168.20.40 3306{
weight 1
}
}
[root@lvs01 ~]# /etc/init.d/keepalived restart
[root@lvs02 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
priority
advert_int 1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.20.50
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int 1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.20.60
}
}
virtual_server 192.168.20.50 80{
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.20.10 80{
weight 1
}
real_server 192.168.20.20 80{
weight 1
}
}
virtual_server 192.168.20.60 3306{
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.20.30 3306{
weight 1
}
real_server 192.168.20.40 3306{
weight 1
}
}
[root@lvs02 ~]# /etc/init.d/keepalived restart
(注意:lvs+keepalived这是一种双主模式,注意route_id的不同,优先级,主备之分)
6.查看vip
[root@lvs01 ~]# ip addr list
[root@lvs02 ~]# ip addr list
7.验证web集群
7.1正常测试,可以看到的是通过vip 访问,可以以轮询的方式将客户的请求发送到web服务器上
7.2 当lvs01 down机了,可以看到vip 漂移到lvs02上,依然可以正常访问web服务
[root@lvs02 ~]# ip addr list
8.验证mysql集群
8.1 正常测试,可以看到的是通过vip 访问,可以以轮询的方式将客户的请求发送到mysql服务器上
[root@lvs01 ~]# mysql -h 192.168.20.60 -umyusr01 -p00000000 -e 'show databases'
[root@lvs01 ~]# mysql -h 192.168.20.60 -umyusr01 -p00000000 -e 'show databases'
8.2 当lvs02 down机了,vip 会飘到lvs01 上,mysql 服务依然可以正常服务
[root@lvs01 ~]# ip addr list
[root@lvs01 ~]# mysql -h 192.168.20.60 -umyusr01 -p00000000 -e 'show databases'
9.查看lvs