keepalived+haproxy 高可用负载均衡集群

在这里插入图片描述
在这里插入图片描述

案例

在这里插入图片描述

chkconfig NetworkManager off
chkconfig iptables off
cat /etc/sysconfig/selinux  #例行公事四台都要这样selinux为disabled状态。
#web为配置好的状态 只有一个页面。

开始配置主调度器

/etc/sysctl.conf  #修改内核参数( /etc/sysctl.conf)文件,关闭ICMP重定向。
sysctl -p

在这里插入图片描述

yum -y install pcre-devel bzip2-devel keepalived haproxy
#装插件 从服务器同样需要装

在这里插入图片描述

/etc/haproxy/haproxy.cfg  #配置文件
global  #全局配置
    log   /dev/log     local0 info  #配置日志记录,local0为日志设备,默认存到系统日志。这里单独定义出来了
    log   /dev/log     local0 notice  #日志级别默认有24个级别

   # chroot      /var/lib/haproxy
    maxconn     4096  #最大连接数
    user        haproxy  #程序用户
    group       haproxy  #程序组
    daemon

defaults  #默认配置
    log                     global  #定义日志为global配置中的日志定义
    mode                    http  #模式为http
    option                  httplog  #采用http日志格式记录日志
    option                  dontlognull  #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
    option http-server-close  #主动关闭http请求选项
    retries                 3  #检查节点服务器失败次数,连续三次失败,则认为节点不可用
    maxconn                 2000  #最大连接数
    contimeout              5000  #连接超时时间
    clitimeout              50000  #客户端超时时间
 srvtimeout              50000  #服务器超时时间

listen  webcluster 0.0.0.0:80  #应用组件配置,定义一个webcluster的应用
        option httpchk GET /index.html  #检查服务器的index.htm文件l
        balance         roundrobin  #负载均衡调度算法使用轮询
        #option 	persist  #强制将请求发送到已经down掉的服务器
        server inst1    192.168.1.20:80 check   inter   2000    fall 3
        server inst2    192.168.1.30:80 check   inter   2000    fall 3   #后面加backup就认为是备份服务器  不加为在线节点
#2000为心跳频率,3表示3次失败认为不可用。  fall 3 backup  这样将认为是节点备份服务器。

在这里插入图片描述
在这里插入图片描述

service haproxy start  #启动服务

在这里插入图片描述

配置日志文件

vim /etc/rsyslog.d/haproxy.conf #配置文件
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~

在这里插入图片描述

service rsyslog restart

在这里插入图片描述

tail -f /var/log/haproxy/haproxy-info.log #动态查看日志

在这里插入图片描述

配置keepalived 热备

vim /etc/keepalived/keepalived.conf #配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
     authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

配置从调度器

yum -y install pcre-devel bzip2-devel keepalived haproxy #插件

在这里插入图片描述

scp [email protected]:/etc/sysctl.conf  /etc/
sysctl -p
scp [email protected]:/etc/keepalived/keepalived.conf /etc/keepalived
scp [email protected]:/etc/haproxy/haproxy.cfg /etc/haproxy/

在这里插入图片描述

vim /etc/keepalived/keepalived.conf  #修改配置文件 这里把需要改的列出来
router_id LVS_DEVEL2  #改名字
state BACKUP  #改类型
priority 90   #改优先级

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

service haproxy start
service keepalived start
#启动服务
配置日志文件
scp [email protected]:/etc/rsyslog.d/haproxy.conf /etc/rsyslog.d/
service rsyslog restart

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40084074/article/details/83379550