负载均衡与高可用简单配置
lvs 实现负载均衡
linux virtual server
- 1
- 2
- 1:配置高可用yum源`
/var/www/html/rhel6.5
[root@server1 haproxy]# cat /etc/yum.repos.d/yum.repo
[yum]
name = yum
baseurl=http://172.25.200.250/rhel6.5
gpgcheck=0
[HighAvailability] #高可用模块
name = HighAvailability
baseurl=http://172.25.200.250/rhel6.5/HighAvailability
gpgcheck=0
[LoadBalancer] #负载均衡模块
name = LoadBalancer
baseurl=http://172.25.200.250/rhel6.5/LoadBalancer
gpgcheck=0
[ResilientStorage]
name = ResilientStorage
baseurl=http://172.25.200.250/rhel6.5/ResilientStorage
gpgcheck=0
[ScalableFileSystem]
name = ScalableFileSystem
baseurl=http://172.25.200.250/rhel6.5/ScalableFileSystem
gpgcheck=0
#######################################################
HighAvailability HighAvailability 56
LoadBalancer LoadBalancer 4
ResilientStorage ResilientStorage 62
ScalableFileSystem ScalableFileSystem 7
yum yum 3,690
repolist: 3,819
#######################################################
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 2:安装ipvsadm
yum install -y ipvsadm
- 1
- 3:添加后端
ipvsadm -A -t 172.25.200.100:80 -s rr
ipvsadm -a -t 172.25.200.100:80 -r 172.25.200.2:80 -g
ipvsadm -a -t 172.25.200.100:80 -r 172.25.200.3:80 -g
ipvsadm -ln #查看策略
/etc/init.d/ipvsadm save
/etc/init.d/ipvsadm stop
/etc/init.d/ipvsadm start
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 4:添加vip
server1
ip addr add 172.25.200.100/24 dev eth0
server*
ip addr add 172.25.200.100/24 dev eth0
做vip掩饰,不对外宣布
yum install -y arptables_jf
arptables -A IN -d 172.25.200.100 -j DROP #对input进行拒绝
arptables -A OUT -s 172.25.200.100 -j mangle --mangle-ip-s 172.25.200.* #output伪装
/etc/init.d/arptables_jf save
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 测试
curl 172.25.200.100
arp -an | grep 100 查看mac地址
arp -d 172.25.200.100 清空缓存
arp -a
- 1
- 2
- 3
- 4
ldirectord + lvs
- 装包
yum install -y ldirectord-3.9.5-3.1.x86_64.rpm
rpm -ql ldirectord #查找配置文件
mv /usr/share/doc/ldirectord-3.9.5/ldirectord.cf /etc/ha.d/
#移动到这里的原因是/etc/init.d/ldirectord中配置文件指向为这里
- 1
- 2
- 3
- 4
- 修改配置文件
vim ldirectord.cf
24 # Sample for an http virtual service
25 virtual=172.25.200.100:80 #vip指定
26 real=172.25.200.2:80 gate #后端指定
27 real=172.25.200.3:80 gate
28 fallback=127.0.0.1:80 gate #最后一道防线,前面的都挂了自己上
29 service=http
30 scheduler=rr
31 #persistent=600
32 #netmask=255.255.255.255
33 protocol=tcp
34 checktype=negotiate
35 checkport=80
36 request="index.html" #后端检查位置
37 #receive="Test Page" #后端检查页面内容
38 #virtualhost=www.x.y.z
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
/etc/init.d/ipvsadm stop
ipvsadm -L
/etc/init.d/ldirectord start
- 1
- 2
- 3
- 这个需要在server2和server3上新建vip,但是server1vip由ldirectord配置文件创建
keepalived + lvs
#keepalived 源码安装
tar zxf keepalived-1.4.0.tar.gz
cd keepalived-1.4.0/
yum install -y openssl-devel libnl3-devel ipset-devel iptables-devel libnfnetlink-devel
yum install -y libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
yum install -y libnl-devel
./configure --prefix=/usr/local/keepalived --with-init=SYSV
make && make install
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
chmod +x /etc/init.d/keepalived
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- keepalived 配置文件
[root@foundation1 day02]# cat keepalived.conf
! Configuration File for keepalived
global_defs { #email
notification_email {
root@localhost
}
notification_email_from keepalived@localhost #name
smtp_server 127.0.0.1 #ip
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict #zhushi
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER /SLAVE 高可用另一台配置
interface eth0
virtual_router_id 200 #
priority 100 #权重 /50 高可用配置副调度器
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.200.100/24 #vip
}
}
#http服务
virtual_server 172.25.200.100 80 { #vip
delay_loop 6
lb_algo rr
lb_kind DR #模式
#persistence_timeout 50
protocol TCP
real_server 172.25.200.2 80 { #server2
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.200.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
#vsftpd 服务
virtual_server 172.25.200.100 21 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50 #持续连接
protocol TCP
real_server 172.25.200.2 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 172.25.200.3 21 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- real server端
yum install -y vsftpd httpd #两个服务进行测试
/var/www/html
/var/ftp