目录
一. keepalived工具介绍
1.专为LVS和HA设计的一款健康检查工具
2.keepalived实现原理
3.VRRP(虚拟路由冗余协议)
二.LVS+Keepalived高可用群集的搭建
1.主DR服务器(192.168.68.200)
1.安装Keepalived程序
2. 给主配置文件做备份
3.修改主配置文件
4.关闭Linux内核的重定向参数响应
5.刷新配置
6.加载模块
2.备DR服务器(192.168.68.30)
1.安装Keepalived程序
2. 给主配置文件做备份
3.修改主配置文件
4.关闭Linux内核的重定向参数响应
5.刷新配置
6.加载模块
3. Web 服务器1(192.168.68.105)
1.安装http服务并开启
2.在网页站点目录中创建网页文件,并写入内容
3.在web1的浏览器中输入自己的ip进行验证
4.创建虚拟接口并进行相关配置
5.开启虚接口并查看
6.调整proce响应参数
7.更新配置
4. Web 服务器2(192.168.68.40)
1.安装http服务并开启
2.在网页站点目录中创建网页文件,并写入内容
3.在web1的浏览器中输入自己的ip进行验证
4.创建虚拟接口并进行相关配置
5.开启虚接口并查看
6.调整proce响应参数
7.更新配置
5.测试
1.抢占式
2.非抢占式
3. 延时抢占式
4. 多播地址
5.单播地址
高可用
Keepalived采用VRRP热备份协议,实现Linux服务器的多机热备功能
工作方式:抢占式,非抢占式,延迟抢占模式
针对路由器的一种备份
在基于LVS+Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,三台以上的节点服务器
主DR 服务器:192.168.68.200
备DR 服务器:192.168.68.30
Web 服务器1:192.168.68.105
Web 服务器2:192.168.68.40
vip:192.168.68.95
客户端:192.168.68.138
配置负载调度器(主备相同)
[root@localhost keepalived]# vim keepalived.conf
将42行后面的内容都删掉改为
[root@localhost keepalived]# vim /etc/sysctl.conf
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y ipvsadm.x86_64 keepalived.x86_64
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
10 smtp_server 127.0.0.1
12 router_id LVS_01
14 #vrrp_strict ##将14行注释掉
19 vrrp_instance VI_1 {
20 state MASTER
21 interface ens33
22 virtual_router_id 10
23 priority 100
24 advert_int 1
25 authentication {
26 auth_type PASS
27 auth_pass 123123
28 }
29 virtual_ipaddress {
30 192.168.68.95
31 }
34 virtual_server 192.168.68.95 80 {
35 delay_loop 6
36 lb_algo rr
37 lb_kind DR
38 persistence_timeout 50
39 protocol TCP
41 real_server 192.168.68.105 80 {
42 weight 1
43 TCP_CHECK {
44 connect_port 80
45 connect_timeout 3
46 nb_get_retry 3
47 delay_before_retry 3
48 }
49 }
50 real_server 192.168.68.40 80 {
51 weight 1
52 TCP_CHECK {
53 connect_port 80
54 connect_timeout 3
55 nb_get_retry 3
56 delay_before_retry 3
57 }
58 }
59 }
[root@localhost keepalived]# vim /etc/sysctl.conf
##在末尾添加
11 net.ipv4.ip_forward = 0
12 net.ipv4.conf.all.send_redirects = 0
13 net.ipv4.conf.default.send_redirects = 0
14 net.ipv4.conf.ens33.send_redirects = 0
[root@localhost keepalived]# sysctl -p
[root@localhost keepalived]# modprobe ip_vs
[root@localhost keepalived]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP C0A8445F:0050 rr persistent 50000 FFFFFFFF
[root@localhost keepalived]# ipvsadm-save >/etc/sysconfig/ipvsadm
[root@localhost keepalived]# systemctl start keepalived.service
[root@localhost keepalived]# systemctl start ipvsadm.service
[root@localhost keepalived]# systemctl restart keepalived.service
[root@localhost keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.68.95:80 rr
-> 192.168.68.40:80 Route 1 0 0
-> 192.168.68.105:80 Route 1 0 0
[root@localhost keepalived]# ipvsadm-save >/etc/sysconfig/ipvsadm
法一:
keepalived.conf和sysctl.conf可以从主服务器复制过来。再修改一下名称和优先级即可。
法二:也可以手动修改配置
[root@localhost keepalived]# vim keepalived.conf
将42行后面的内容都删掉改为
[root@localhost keepalived]# vim /etc/sysctl.conf
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y ipvsadm.x86_64 keepalived.x86_64
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
10 smtp_server 127.0.0.1
12 router_id LVS_01
14 #vrrp_strict ##将14行注释掉
19 vrrp_instance VI_1 {
20 state MASTER
21 interface ens33
22 virtual_router_id 10
23 priority 100
24 advert_int 1
25 authentication {
26 auth_type PASS
27 auth_pass 123123
28 }
29 virtual_ipaddress {
30 192.168.68.95
31 }
34 virtual_server 192.168.68.95 80 {
35 delay_loop 6
36 lb_algo rr
37 lb_kind DR
38 persistence_timeout 50
39 protocol TCP
41 real_server 192.168.68.105 80 {
42 weight 1
43 TCP_CHECK {
44 connect_port 80
45 connect_timeout 3
46 nb_get_retry 3
47 delay_before_retry 3
48 }
49 }
50 real_server 192.168.68.40 80 {
51 weight 1
52 TCP_CHECK {
53 connect_port 80
54 connect_timeout 3
55 nb_get_retry 3
56 delay_before_retry 3
57 }
58 }
59 }
[root@localhost keepalived]# vim /etc/sysctl.conf
##在末尾添加
11 net.ipv4.ip_forward = 0
12 net.ipv4.conf.all.send_redirects = 0
13 net.ipv4.conf.default.send_redirects = 0
14 net.ipv4.conf.ens33.send_redirects = 0
[root@localhost keepalived]# sysctl -p
[root@localhost keepalived]# modprobe ip_vs
[root@localhost keepalived]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP C0A8445F:0050 rr persistent 50000 FFFFFFFF
[root@localhost keepalived]# ipvsadm-save >/etc/sysconfig/ipvsadm
[root@localhost keepalived]# systemctl start keepalived.service
[root@localhost keepalived]# systemctl start ipvsadm.service
[root@localhost keepalived]# systemctl restart keepalived.service
[root@localhost keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.68.95:80 rr
-> 192.168.68.40:80 Route 1 0 0
-> 192.168.68.105:80 Route 1 0 0
[root@localhost keepalived]# ipvsadm-save >/etc/sysconfig/ipvsadm
[root@localhost html]# vim index.html
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
hello oyyyyy!!!!!!!
[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
DEVICE=lo
IPADDR=192.168.68.95
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
#BROADCAST=127.255.255.255
ONBOOT=yes
#NAME=loopback
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig lo:0
[root@localhost network-scripts]# vim /etc/sysctl.conf
##在最后添加这些参数
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
[root@localhost html]# vim index.html
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
hello wangyibo!!!!!!!!!
[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
DEVICE=lo
IPADDR=192.168.68.95
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
#BROADCAST=127.255.255.255
ONBOOT=yes
#NAME=loopback
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig lo:0
[root@localhost network-scripts]# vim /etc/sysctl.conf
##在最后添加这些参数
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
在浏览器中输入192.168.68.95
等待1分钟后刷新
在主服务器器中将keepalived关掉
然后再web服务器中抓包
在浏览器中输入虚拟IP 192.168.68.95
刷新后
主服务器
备服务器
都开启服务
[root@localhost keepalived]# systemctl start keepalived.service
将主服务器的keepalived关掉,备用服务器就顶上,但是当主服务器的keepalived开启后,还是备用服务器在工作。此时非抢占式就生效了。
主服务器
备服务器
都开启服务
[root@localhost keepalived]# systemctl start keepalived.service
将主服务器的keepalived关掉,备用服务器就顶上,但是当主服务器的keepalived开启后,延迟5秒后,主服务器启动工作。
主服务器
备服务器
都开启服务
[root@localhost keepalived]# systemctl start keepalived.service
将主服务器的keepalived关掉,备用服务器就顶上,当主服务器的keepalived开启后,主服务器启动工作。
主服务器
备服务器
抓包