HAProxy实现slave负载均衡[高可用]

下面要执行的是HAProxy部分

这是一个集群,其他的部分在:

mysql-cluster 7.3.5安装部署

mysql主备部署[高可用]

mysql主备切换[高可用]

mysql读写分离[高可用]

mysql-slave负载均衡[高可用] 

1.安装haproxy

1
2
3
4
5
6
7
8
9
tar zxvf haproxy-1.4.13.tar.gz
 
cd haproxy-1.4.13/
 
mkdir -p /usr/local/haproxy
 
make TARGET=linux26 PREFIX=/usr/local/haproxy  ##我的系统内核为2.6,所以target=linux26
 
make install PREFIX=/usr/local/haproxy

2.配置文件

1
2
3
4
5
mkdir /etc/haproxy
 
touch /etc/haproxy/haproxy.cfg
 
vim /etc/haproxy/haproxy.cfg

haproxy.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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                    tcp
    log                     global
    option                  dontlognull
    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                 600
 
listen stats
    mode http
    bind :6677
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
 
frontend  main 192.168.99.55:9090
    default_backend             mysql
 
backend mysql
    balance     leastconn
    server slave75.57 192.168.99.57:3306 check port 3306 maxconn 300
    server slave75.62 192.168.99.62:3306 check port 3306 maxconn 300

haproxy服务脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
#
# haproxy
#
# chkconfig:   - 85 15
# description:  HAProxy is a free, very fast and reliable solution \
#               offering high availability, load balancing, and \
#               proxying for TCP and  HTTP-based applications
# processname: haproxy
# config:      /etc/haproxy/haproxy.cfg
# pidfile:     /var/run/haproxy.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
exec="/usr/sbin/haproxy"
prog=$(basename $exec)
 
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
 
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
 
check() {
    $exec -c -V -f $cfgfile $OPTIONS
}
 
start() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
 
    echo -n $"Starting $prog: "
    # start it up here, usually something like "daemon $exec"
    daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    stop
    start
}
 
reload() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    echo -n $"Reloading $prog: "
    $exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
    retval=$?
    echo
    return $retval
}
 
force_reload() {
    restart
}
 
fdr_status() {
    status $prog
}
 
case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    force-reload)
        force_reload
        ;;
    check)
        check
        ;;
    status)
        fdr_status
        ;;
    condrestart|try-restart)
        [ ! -f $lockfile ] || restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
        exit 2
esac

3.启动

1
2
3
4
5
6
7
8
9
10
11
12
service haproxy start
 
netstat -tlnp | grep haproxy
 
tcp        0      0 0.0.0.0:6677                0.0.0.0:*                   LISTEN      32360/haproxy      
tcp        0      0 192.168.75.55:9090          0.0.0.0:*                   LISTEN      32360/haproxy 
 
 
ps aux | grep haproxy
 
haproxy  32360  0.0  0.0  14416  1524 ?        Ss   15:12   0:00 /usr/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
root     32685  0.0  0.0 103244   840 pts/1    S+   15:32   0:00 grep haproxy

4.检测

 4.1浏览器

浏览器地址栏: http://192.168.99.55:6677/haproxyadmin?stats

上面的这个配置在haproxy.cfg里面

4.2navicat

第一次连接连的是62关闭, 重新连接这次连的是57

看数据库不同从而分析, 成功!

 

 完毕!

 

posted @   扶苏公子x  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战
· AI工具推荐:领先的开源 AI 代码助手——Continue

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示