LVS+Keepalived群集

Keepalived工具介绍
  • 专为LVS和HA设计的一款健康检查工具
    • 支持故障自动切换(Failover)
    • 支持节点健康状态检查(Health Checking)
Keepalived实现原理刨析
  • Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能
shell 复制代码
#主服务器keepalived的安装
[root@localhost ~]# systemctl stop NetworkManager #网络托管 网络管理
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# systemctl enable keepalived

#主服务器keepalived的配置
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak

[root@localhost keepalived]# vi keepalived.conf
global_defs { :w#全局参数
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   #vrrp_strict #严格模式 禁用单播模式
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_01 #路由器名称
}

vrrp_instance VI_1 { #示例参数
    state MASTER
    interface ens33
    virtual_router_id 51 #虚拟路由id在群集中需一致
    priority 100   #优先级  0~255
    advert_int 1   #通告发送间隔 
    authentication {     
        auth_type PASS #验证类型
        auth_pass 1111 #验证密码
    }
    virtual_ipaddress {
        192.168.10.172
    }
}

virtual_server 192.168.10.172 80 { #群集ip
    delay_loop 6 #健康检查间隔
    lb_algo wrr #负载均衡算法
    lb_kind DR #负载均衡类型
    nat_mask 255.255.255.0 #非不许
!    persistence_timeout 50
    protocol TCP #虚拟服务器协议

real_server 192.168.10.103 80 { #真实服务器
        weight 1 #权重
        TCP_CHECK { #健康检查
            connect_port 80
            connect_timeout 3  
            nb_get_retry 3  #超时次数
            delay_before_retry 3 #重连时间
        }
    }
real_server 192.168.10.104 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}
#主服务器内核参数的配置
[root@localhost ~]# vi /etc/sysctl.conf
#添加
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p


#开启主服务器的keepalived服务
[root@localhost keepalived]# systemctl start keepalived
[root@localhost keepalived]# ip add show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ae:7f:64 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.101/24 brd 172.16.16.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.10.172/32 scope global ens33
       valid_lft forever preferred_lft forever

#配置从调度器
#从调度器keepalived安装
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# systemctl enable keepalived

#从调度器keepalived的配置
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak

[root@localhost keepalived]# vi keepalived.conf
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   #vrrp_strict
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_02
}

vrrp_instance VI_1 {
    state BACKUP #备份
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.172
    }
}

virtual_server 192.168.10.172 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
!    persistence_timeout 50
    protocol TCP

    real_server 192.168.10.101 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.10.102 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}
#从服务器内核参数的配置
[root@localhost ~]# vi /etc/sysctl.conf
在末尾添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p
#开启从服务器的keepalived服务
[root@localhost keepalived]# systemctl start keepalived
#web网络的配置
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.10.172
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# systemctl restrt network
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.10.172 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.10.172 dev lo:0
#httpd服务的安装
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vi /var/www/html/index.html
test web01
#内核参数的设置
[root@localhost ~]# vi /etc/sysctl.conf
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
#开启httpd服务
[root@localhost ~]# systemctl start httpd
相关推荐
西猫雷婶3 小时前
python学opencv|读取图像(十九)使用cv2.rectangle()绘制矩形
开发语言·python·opencv
liuxin334455663 小时前
学籍管理系统:实现教育管理现代化
java·开发语言·前端·数据库·安全
码农W3 小时前
QT--静态插件、动态插件
开发语言·qt
ke_wu3 小时前
结构型设计模式
开发语言·设计模式·组合模式·简单工厂模式·工厂方法模式·抽象工厂模式·装饰器模式
code04号4 小时前
python脚本:批量提取excel数据
开发语言·python·excel
小王爱吃月亮糖4 小时前
C++的23种设计模式
开发语言·c++·qt·算法·设计模式·ecmascript
hakesashou4 小时前
python如何打乱list
开发语言·python
007php0074 小时前
linux服务器上CentOS的yum和Ubuntu包管理工具apt区别与使用实战
linux·运维·服务器·ubuntu·centos·php·ai编程
网络风云4 小时前
【魅力golang】之-反射
开发语言·后端·golang
Want5955 小时前
Java圣诞树
开发语言·python·信息可视化