LVS+Keepalived 高可用群集的介绍和搭建步骤

目录

一. 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工具介绍

高可用

1.专为LVS和HA设计的一款健康检查工具

  • 支持故障自动切换
  • 支持节点健康状态检查
  • 官方网站:http://www.keepalived.org/

2.keepalived实现原理

Keepalived采用VRRP热备份协议,实现Linux服务器的多机热备功能

3.VRRP(虚拟路由冗余协议)

工作方式:抢占式,非抢占式,延迟抢占模式

针对路由器的一种备份

  • 由多台路由器组成一个热备组,通过共用的虚拟IP地址对外提供服务
  • 每个热备组内同时只有一台路由器提供服务,其他路由器处于冗余状态
  • 若当前在线的路由器失效,则其他路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务

LVS+Keepalived 高可用群集的介绍和搭建步骤_第1张图片

二.LVS+Keepalived高可用群集的搭建

在基于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 

配置负载调度器(主备相同)

1.主DR服务器(192.168.68.200)

1.安装Keepalived程序

2. 给主配置文件做备份

3.修改主配置文件 

 [root@localhost keepalived]# vim keepalived.conf

LVS+Keepalived 高可用群集的介绍和搭建步骤_第2张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第3张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第4张图片

将42行后面的内容都删掉改为

LVS+Keepalived 高可用群集的介绍和搭建步骤_第5张图片

4.关闭Linux内核的重定向参数响应

 [root@localhost keepalived]# vim /etc/sysctl.conf 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第6张图片

5.刷新配置

6.加载模块

LVS+Keepalived 高可用群集的介绍和搭建步骤_第7张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第8张图片

[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

2.备DR服务器(192.168.68.30)

法一:

keepalived.conf和sysctl.conf可以从主服务器复制过来。再修改一下名称和优先级即可。

LVS+Keepalived 高可用群集的介绍和搭建步骤_第9张图片

法二:也可以手动修改配置 

1.安装Keepalived程序

2. 给主配置文件做备份

3.修改主配置文件 

 [root@localhost keepalived]# vim keepalived.conf

LVS+Keepalived 高可用群集的介绍和搭建步骤_第10张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第11张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第12张图片

将42行后面的内容都删掉改为

LVS+Keepalived 高可用群集的介绍和搭建步骤_第13张图片

4.关闭Linux内核的重定向参数响应

 [root@localhost keepalived]# vim /etc/sysctl.conf 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第14张图片

5.刷新配置

6.加载模块

LVS+Keepalived 高可用群集的介绍和搭建步骤_第15张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第16张图片

[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

3. Web 服务器1(192.168.68.105)

1.安装http服务并开启

2.在网页站点目录中创建网页文件,并写入内容

 [root@localhost html]# vim index.html

 

3.在web1的浏览器中输入自己的ip进行验证

4.创建虚拟接口并进行相关配置 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第17张图片

5.开启虚接口并查看

6.调整proce响应参数

[root@localhost network-scripts]# vim /etc/sysctl.conf

LVS+Keepalived 高可用群集的介绍和搭建步骤_第18张图片

7.更新配置

LVS+Keepalived 高可用群集的介绍和搭建步骤_第19张图片

[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

4. Web 服务器2(192.168.68.40)

1.安装http服务并开启

2.在网页站点目录中创建网页文件,并写入内容

 [root@localhost html]# vim index.html

 

3.在web1的浏览器中输入自己的ip进行验证

4.创建虚拟接口并进行相关配置 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第20张图片

5.开启虚接口并查看

6.调整proce响应参数

[root@localhost network-scripts]# vim /etc/sysctl.conf

LVS+Keepalived 高可用群集的介绍和搭建步骤_第21张图片

7.更新配置

LVS+Keepalived 高可用群集的介绍和搭建步骤_第22张图片

[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

5.测试

LVS+Keepalived 高可用群集的介绍和搭建步骤_第23张图片

LVS+Keepalived 高可用群集的介绍和搭建步骤_第24张图片

在浏览器中输入192.168.68.95 

等待1分钟后刷新

LVS+Keepalived 高可用群集的介绍和搭建步骤_第25张图片

1.抢占式

 在主服务器器中将keepalived关掉

然后再web服务器中抓包

LVS+Keepalived 高可用群集的介绍和搭建步骤_第26张图片

在浏览器中输入虚拟IP 192.168.68.95 

刷新后

LVS+Keepalived 高可用群集的介绍和搭建步骤_第27张图片

2.非抢占式

主服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第28张图片

 备服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第29张图片

都开启服务

[root@localhost keepalived]# systemctl start keepalived.service 

将主服务器的keepalived关掉,备用服务器就顶上,但是当主服务器的keepalived开启后,还是备用服务器在工作。此时非抢占式就生效了。

LVS+Keepalived 高可用群集的介绍和搭建步骤_第30张图片

3. 延时抢占式

主服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第31张图片

 备服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第32张图片

都开启服务

[root@localhost keepalived]# systemctl start keepalived.service 

将主服务器的keepalived关掉,备用服务器就顶上,但是当主服务器的keepalived开启后,延迟5秒后,主服务器启动工作。

LVS+Keepalived 高可用群集的介绍和搭建步骤_第33张图片

4. 多播地址

主服务器 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第34张图片

  备服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第35张图片

都开启服务

[root@localhost keepalived]# systemctl start keepalived.service 

将主服务器的keepalived关掉,备用服务器就顶上,当主服务器的keepalived开启后,主服务器启动工作。

LVS+Keepalived 高可用群集的介绍和搭建步骤_第36张图片

5.单播地址

主服务器 

LVS+Keepalived 高可用群集的介绍和搭建步骤_第37张图片

  备服务器

LVS+Keepalived 高可用群集的介绍和搭建步骤_第38张图片

 抓包

LVS+Keepalived 高可用群集的介绍和搭建步骤_第39张图片

你可能感兴趣的:(Linux,nginx,nginx,linux,运维,LVM,集群)