今天搭建keepalived配合LVS实现高可用高负载集群,首先来了解一下keepalived。
keepalived起初是专门针对LVS设计的一款强大的辅助工具 主要用来提供故障切换
和见刊检查功能判断LVS负载调度器 节点服务器的可靠性 即使隔离并替换为新的服务器 当故障主机恢复后将其重新加入到群集
、
keepalived采用VRRP(虚拟路由冗余协议)热备份协议 以软件的方式实现linux服务器的多机热备份功能 主服务器瘫痪 备份服务器继承主服务器的vip地址接着公作
VIP地址是虚拟ip地址也被称为是群集ip地址 是访问群集的唯一入口
keepalived的作用和特点
keepalived的作用:配置高可用群集的工具
keepalived的特点:可靠性强 避免点点故障 保持服务连续性 检测服务运行状态
keepalived的核心技术通过VRRP虚拟路由冗余协议实现
接下来看一下实验环境:
上图实验一共开启七台虚拟机六台centos一台windows
一、首先安装两台web服务器
[root@centos01 ~]# yum -y install httpd
[root@centos01 ~]# systemctl start httpd
2】第二台web服务器同上
[root@centos02 ~]# yum -y install httpd
[root@centos02 ~]# systemctl start httpd
3】然后给这两台web服务器配置网关
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
GATEWAY=192.168.100.60
#这个地址是一会我们要用到的防火墙强地址同样也是内网网关
4】第二台web服务器配置网关
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.20
NETMASK=255.255.255.0
GATEWAY=192.168.100.60
5】然后我们用到的DR模式 web服务器需要配置监听网卡来监听vip地址
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo
IPADDR=192.168.100.254
NETMASK=255.255.255.255
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
6】第二台web服务器和第一台一样
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo
IPADDR=192.168.100.254
NETMASK=255.255.255.255
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
7】最后重启网卡服务
[root@centos01 ~]# systemctl restart network
[root@centos02 ~]# systemctl restart network
二、配置主从keepalived
1】安装keepalived和LVS
[root@centos03 ~]# yum -y install ipvsadm keepalived
2】从服务器也安装上
[root@centos04 ~]# yum -y install ipvsadm keepalived
3】编辑服务器主配置文件
[root@centos03 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER //主调度器的名字
}
vrrp_instance VI_1 {
state MASTER //主调度器
interface ens33 //监听网卡
virtual_router_id 51 //虚拟路由ID
priority 100 //主调度器优先级
advert_int 1 //状态检测间隔时间为1秒
authentication {
auth_type PASS //密码验证
auth_pass 1111 //指定密码
}
virtual_ipaddress {
192.168.100.254 //指定漂移地址
}
}
virtual_server 192.168.100.254 80 { //配置服务器池vip地址和端口
delay_loop 6 //健康状态检查间隔时间6秒
lb_algo rr //LVS算法使用轮询
lb_kind DR //LVS模式DR
persistence_timeout 50 // 访问LVS链接保持时间为50秒
protocol TCP //LVS调度协议使用TCP
real_server 192.168.100.10 80 { //服务器池中第一台网站服务器
weight 1 //权重
TCP_CHECK { //检查TCP
connect_timeout 3 //检查TCP超时时间
connect_prot 80 //检查端口
nb_get_retry 3 //TCP链接失败后重新链接3次
delay_before_retry 3 //每次间隔3秒
}
}
}
virtual_server 192.168.100.254 80 { //配置服务器池vip地址和端口
delay_loop 6 //健康状态检查间隔时间6秒
lb_algo rr //LVS算法使用轮询
lb_kind DR //LVS模式DR
persistence_timeout 50 // 访问LVS链接保持时间为50秒
protocol TCP //LVS调度协议使用TCP
real_server 192.168.100.10 80 { //服务器池中第二台网站服务器
weight 1 //权重
TCP_CHECK { //检查TCP
connect_timeout 3 //检查TCP超时时间
connect_prot 80 //检查端口
nb_get_retry 3 //TCP链接失败后重新链接3次
delay_before_retry 3 //每次间隔3秒
}
}
}
4】从服务器配置主配置文件(只需要改个优先级就行)
[root@centos04 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state BAKCUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.254
}
}
virtual_server 192.168.100.254 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.10 80 {
weight 1
TCP_CHECK {
connect_timeout 3
connect_prot 80
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 192.168.100.254 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.20 80 {
weight 1
TCP_CHECK {
connect_timeout 3
connect_prot 80
nb_get_retry 3
delay_before_retry 3
}
}
}
5】最后两台全部开启服务
systemctl start keepalived
三、配置DR模式配置内核关闭/开启响应
1】DR模式调度器肯定是要关闭响应配置内核
[root@centos03 ~]# vim /etc/sysctl.conf
[root@centos03 ~]# sysctl -f
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
2】关闭从调度器的响应
[root@centos04 ~]# vim /etc/sysctl.conf
[root@centos03 ~]# sysctl -f
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
3】配置web服务器开启arp响应
[root@centos01 ~]# sysctl -f
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
4】配置第二台web服务器开启arp响应
[root@centos02 ~]# sysctl -f
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
三、然后配置防火墙服务器和dns服务器
1】配置防火墙关闭默认地址伪装允许特定服务
[root@centos06 ~]# firewall-cmd --set-default-zone=trusted
success
[root@centos06 ~]# firewall-cmd --remove-masquerade --zone=external
success
[root@centos06 ~]# firewall-cmd --add-service=http --zone=external
success
[root@centos06 ~]# firewall-cmd --add-service=dns --zone=external
success
[root@centos06 ~]# firewall-cmd --add-port=80/tcp --zone=external
success
2】配置防火墙地址伪装和端口映射
[root@centos06 ~]# firewall-cmd --zone=external --add-rich-rule='rule family
=ipv4 destination address=192.168.200.10/24 forward-port port=80 protocol=tcp to-addr=192.168.100.254'success
[root@centos06 ~]# firewall-cmd --zone=external --add-rich-rule='rule family
=ipv4 source address=192.168.100.0/24 masquerade'success
2、安装dns服务器
root@centos06 ~]# rm -fr /etc/yum.repos.d/C*
[root@centos06 ~]# yum -y install bind bind-utils bind-chroot
1】编辑dns主配置文件
[root@centos06 ~]# vim /etc/named.conf
2】编辑区域配置文件
[root@centos06 ~]# vim /var/named/benet.com.zone
3】开启dns服务器
[root@centos06 ~]# systemctl start named
四、最后配置NFS存储服务器
1】安装存储服务器
[root@centos05 ~]# yum -y install nfs-utils rpcbind
2】创建存储根目录和网站主页
[root@centos05 ~]# mkdir /www
[root@centos05 ~]# echo "www.nfs.com" > /www/index,html
3】修改NFS存储配置文件
[root@centos05 ~]# vim /etc/exports
/www 192.168.100.10(ro) 192.168.100.20(ro)
4】启动服务设置开机自启
[root@centos05 ~]# systemctl start nfs
[root@centos05 ~]# systemctl enable nfs
[root@centos05 ~]# systemctl start rpcbind
[root@centos05 ~]# systemctl enbale rpcbind
5】最后配置两台web服务器挂载NFS共享存储
[root@centos01 ~]# vim /etc/fstab
192.168.100.30:/www /var/www/html/ nfs defaults,_netdev 0 0
6】配置网站服务器开机自动挂载
[root@centos02 ~]# vim /etc/fstab
192.168.100.30:/www /var/www/html/ nfs defaults,_netdev 0 0
重启验证:
实验至此结束