负载均衡的高可用集群(8)-haproxy负载均衡+keepalived高可用

haproxy负载均衡+keepalived高可用

1.实验环境

sever2(172.25.254.2)haproxy+keepalived 安装并配置 haproxy+keepalived
sever3(172.25.254.3)haproxy+keepalived 安装并配置 haproxy+keepalived
sever4(172.25.254.4)apache 服务器 安装 apache 服务器
sever5(172.25.254.5)apache 服务器 安装 apache 服务器

 

 

 

 

 

2.配置 haproxy+keepalived

配置keeplived

[root@server2 keepalived]# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
 root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_haproxy {
 script "/opt/check_haproxy.sh"
 interval 2
 weight 0
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
 check_haproxy
    }
    virtual_ipaddress {
 172.25.0.100
    }
}
server3 
! Configuration File for keepalived

global_defs {
   notification_email {
 root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_haproxy {
 script "/opt/check_haproxy.sh"
 interval 2
 weight 0
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
 check_haproxy
    }
    virtual_ipaddress {
 172.25.0.100
    }
}

配置haproxy  两台都一样

#    use_backend static          if url_static
acl read_request method GET
 acl read_request method HEAD
 acl write_request method PUT
 acl write_request method POST

    use_backend static          if read_request
    use_backend app             if write_request
    default_backend             static
#    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 172.25.254.4:80 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
  balance     roundrobin
  #  balance     source
    # balance static-rr
    server  app1 172.25.254.5:80 check
#    server  app2 172.25.254.5:8080 check
    server  backup 127.0.0.1:8000 backup
                                             

两台的脚本文件 给定+x的权限

[root@server2 keepalived]# cat /opt/check_haproxy.sh 
#!/bin/bash
systemctl status haproxy &> /dev/null || systemctl restart haproxy &> /dev/null

killall -0 haproxy

if [ $? -ne 0 ];then
 systemctl stop keepalived
fi

测试  能过实现高可用

 

你可能感兴趣的:(企业自动化运维架构)