实验环境

hostnameIP
VIP地址 172.16.0.100
master172.16.0.10
backup172.16.0.8
mysqla172.16.0.4
mysqlb172.16.0.5
--双主模式的数据库

环境准备

全部服务器都需要执行以下

--在所有服务器上面进行配置
systemctl stop firewalld        #关闭防火墙
setenforce 0                #关闭selinux,临时生效

--编辑hosts 文件
vim /etc/hosts
172.16.0.10 master
172.16.0.8  backup
172.16.0.4  mysqla
172.16.0.5  mysqlb

安装Haproxy

master:
[root@master /]# yum -y install haproxy
[root@master /]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@master /]# vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend mysql
    mode tcp
    bind *:6039
    default_backend back_mysql

backend back_mysql
    mode  tcp
    balance     roundrobin
    server  mysqla 172.16.0.4:3306 check
    server  mysqlb 172.16.0.5:3306 check

backup:
[root@backup /]# yum -y install haproxy
[root@backup /]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@backup /]# vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend mysql
    mode tcp
    bind *:6039
    default_backend back_mysql

backend back_mysql
    mode  tcp
    balance     roundrobin
    server  mysqla 172.16.0.4:3306 check
    server  mysqlb 172.16.0.5:3306 check

部署keepalived

--master和backer 机器都需要安装keepalived
[root@master /]# yum install -y keepalived
[root@backup /]# yum install -y keepalived

编辑配置文件

master:
[root@master /]# cp /etc/keepalived/keepalived.conf{,.bak}
[root@master /]# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id directory1
}
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy_status.sh"
   interval 5  #每5秒执行一次
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.0.100/24
    }

    track_script {
        check_haproxy
    }
}

backup:
[root@backup /]# cp /etc/keepalived/keepalived.conf{,.bak}
[root@backup /]# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id directory2
}
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy_status.sh"
   interval 5  #每5秒执行一次
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.0.100/24
    }

    track_script {
        check_haproxy
    }
}

编辑脚本

master:
vim /etc/keepalived/check_haproxy_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null   
if [ $? -ne 0 ];then                                                                     
#       /etc/init.d/keepalived stop
            systemctl stop keepalived
fi     
    
backup:
vim /etc/keepalived/check_haproxy_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null   
if [ $? -ne 0 ];then                                                                     
#       /etc/init.d/keepalived stop
            systemctl stop keepalived
fi

测试

--在master和backup上面启动haproxy和keepalived
master:
[root@master /]# systemctl start haproxy
[root@master /]# systemctl start keepalived

backup:
[root@backup /]# systemctl start haproxy
[root@backup /]# systemctl start keepalived
--查看VIP IP绑定成功了没
[root@master /]# ip add |grep 172.16.0.
inet 172.16.0.10/24 brd 172.16.0.255 scope global dynamic ens33
inet 172.16.0.100/24 scope global secondary ens33

[root@backup /]# ip add |grep 172.16.0.
inet 172.16.0.8/24 brd 172.16.0.255 scope global dynamic ens33

连接Mysql数据库

在连接数据库的时候需要使用VI+haproxy_PORT,上面我们配置haproxy监听了6039端口,那么连接的时候就是用VIP+6039端口

请输入图片描述

Last modification:November 28th, 2020 at 05:11 am
如果觉得我的文章对你有用,请随意赞赏