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

一、haproxy下做单点负载均衡上传

1.server2中修改haproxy的配置文件

[root@server2 ~]# vi /etc/haproxy/haproxy.cfg 
frontend main
    bind *:80
    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    ##如果是读请求 使用static ----> server3
    use_backend app       if write_request   ##如果是写请求 使用app ----> server4
    default_backend             app          ######默认是使用app

backend static
    balance     roundrobin
    server      static 172.25.7.4:80 check      ## server3

backend app
    balance     roundrobin
    server  app2 172.25.7.5:80 check            ##server4
    server  backup 127.0.0.1:8080  backup       ##local,
 ###修改端口为8080是避免server2的apache影响80端口的使用
---------------------------------------------------------------------------------
systemctl restart haproxy.service

haproxy+keepalived实现高可用集群的负载均衡_第1张图片

2.在server3和server4的操作

server3 && server4 都得做以下操作
在apache默认发布目录下创建上传目录upload以及index.php和upload_file.php
[root@server3 html]# pwd
/var/www/html
[root@server3 html]# ls
images  index.html  index.php  upload  upload_file.php
------------------------------------------------------------------
[root@server3 html]# cat index.php 




------------------------------------------------------------------- [root@server3 html]# cat upload_file.php 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>

3.上传图片看效果,写操作在server4里执行

http://172.25.7.3/index.php   ##访问server2
上传iso7.gif图片,上传格式在server4的upload_file.php里修改

haproxy+keepalived实现高可用集群的负载均衡_第2张图片 

二、haproxy+keepalived实现高可用集群的负载均衡

1.在server1中操作

<1>安装keepalived和haproxy
[root@server1 ~]# dnf install keepalived  haproxy  -y
-----------------------------------------------------------------------------
<2>将server2做好的haproxy负载均衡配置文件传输给server1-----
[root@server2 ~]# scp /etc/haproxy/haproxy.cfg [email protected]:/etc/haproxy/
-----------------------------------------------------------------------------
<3>修改keepalived的配置文件
[root@server1 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]
        root@localhost                ##注释之前的然后添加
   }
   notification_email_from keepalived@aocalhost         ##修改from后内容
   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 {         ##新添加执行脚本/opt/check_haproxy.sh
    script "/opt/check_haproxy.sh"
    interval 2
    weight 0
}

vrrp_instance VI_1 {
    state MASTER
    interface enp1s0               ##根据自己的情况看网卡 
    virtual_router_id 58           ##更改51避免被占用
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {                 ##添加check_haproxy
        check_haproxy
}

    virtual_ipaddress {
        172.25.7.100               ##获取vip
    }
}
#######################之后的内容全部删除#################
------------------------------------------------------------------
<4>在/opt/check_haproxy.sh
[root@server1 ~]# vi /opt/check_haproxy.sh
[root@server1 ~]# cat /opt/check_haproxy.sh 
#!/bin/bash
systemctl status haproxy &> /dev/null || systemctl restart haproxy &> /dev/null
killall -0 haproxy            ### -0  看进程是否存在
if [ $? -ne 0 ];then
	systemctl stop keepalived
fi
[root@server1 ~]# chmod +x /opt/check_haproxy.sh 
-------------------------------------------------------------------
<5>将server1上 <3>and<4> 的内容给server2上传输到相同路径下
scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
scp /opt/check_haproxy.sh [email protected]:/opt/
                                                                                                                                                            

haproxy+keepalived实现高可用集群的负载均衡_第3张图片

haproxy+keepalived实现高可用集群的负载均衡_第4张图片


2.在server2中的操作

[root@server2 ~]# vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP             ##修改为BACKUP
    interface enp1s0
    virtual_router_id 58                                            ##更改51避免被占用
    priority 50              ##优先级改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {                                                ##添加check_haproxy
        check_haproxy
}

    virtual_ipaddress {
        172.25.7.100                                              ##获取vip
    }
}

haproxy+keepalived实现高可用集群的负载均衡_第5张图片


3.在server3和server4中操作

srever3  and  server4

ip addr add 172.25.7.100/24 dev enp1s0
systemctl start  httpd
systemctl restart httpd

4.server1和server2重启haproxy和keepalived后看效果

<1>在server1里获得了vip,因为server优先级高

haproxy+keepalived实现高可用集群的负载均衡_第6张图片

<2>访问vip ,访问到server3,因为haproxy配置文件修改写才访问server4

haproxy+keepalived实现高可用集群的负载均衡_第7张图片

<3>server1崩了以后 server2 会获取vip作为调度器,高可用集群实现

haproxy+keepalived实现高可用集群的负载均衡_第8张图片

 

你可能感兴趣的:(linux实战,负载均衡,服务器,apache)