mysql主从复制,mycat配置 传送门
一.HAproxy安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#gcc 安装 yum install gcc-c++ #下载 wget http://fossies.org/linux/misc/haproxy-1.6.9.tar.gz #解压 tar -zxvf haproxy-1.6.9.tar.gz cd haproxy-1.6.9 #安装 make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy #参数说明 TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel大于2.6.28的用:TARGET=linux2628 ARCH=x86_64 #系统位数 PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径 |
二、创建配置文件
1 |
vim /usr/local/haproxy/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 44 45 46 47 48 49 50 51 52 |
global log 127.0.0.1 local0 #log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 chroot /usr/local/haproxy pidfile /usr/data/haproxy/haproxy.pid uid 99 gid 99 daemon #debug #quiet defaults log global mode tcp option abortonclose option redispatch retries 3 maxconn 2000 timeout connect 5000 timeout client 50000 timeout server 50000 listen proxy_status bind :48066 mode tcp balance roundrobin server mycat_1 192.168.6.121:8066 check inter 10s server mycat_2 192.168.6.122:8066 check inter 10s server mycat_3 192.168.6.123:8066 check inter 10s listen proxy_k8s bind :80 mode tcp balance roundrobin server k8s-master1 192.168.188.202:30001 check inter 10s server k8s-master2 192.168.188.203:30001 check inter 10s server k8s-master3 192.168.188.204:30001 check inter 10s frontend admin_stats bind :7777 mode http stats enable option httplog maxconn 10 stats refresh 30s stats uri /admin stats auth mldn:java stats hide-version stats admin if TRUE |


创建一个haproxy的数据保存路径:mkdir -p /usr/data/haproxy/
三、启动haproxy
1 |
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg |
四、查看状态
1 |
ps -ef|grep haproxy |

1 2 3 4 5 6 7 8 9 |
http://192.168.6.121:7777/admin http://192.168.6.122:7777/admin http://192.168.6.123:7777/admin (三台机器都要如上安装haproxy) #说明: 7777即haproxy.cfg配置文件中监听端口 /admin 即haproxy的访问路径 用户名:mldn 密码:java |
haproxy启动脚本
1 |
vim /etc/init.d/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 |
#!/bin/bash # # haproxy # # chkconfig: 35 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.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 config="/usr/local/haproxy/haproxy.cfg" exec="/usr/local/haproxy/sbin/haproxy" prog=$(basename $exec) [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=/var/lock/subsys/haproxy check() { $exec -c -V -f $config } start() { $exec -c -q -f $config 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 $config -p /var/run/$prog.pid 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 $config if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi stop start } reload() { $exec -c -q -f $config if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Reloading $prog: " $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid) retval=$? echo return $retval } force_reload() { restart } fdr_status() { status $prog } case "$1" in start|stop|restart|reload) $1 ;; force-reload) force_reload ;; checkconfig) check ;; status) fdr_status ;; condrestart|try-restart) [ ! -f $lockfile ] || restart ;; *) echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}" exit 2 esac |
1 2 3 4 5 6 7 8 |
chmod +x /etc/init.d/haproxy /etc/init.d/haproxy start|stop|restart #设置haproxy开机启动 systemctl enable haproxy #设置开机自动启动 systemctl disable haproxy #取消开机自动启动 chkconfig --list #查看开机启动列表 |
关于HAproxy设置日志 请看这里 这个人写的很好,我这里就不重复了 传送门
keepalive安装与配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#第一步,安装基础库 yum -y install openssl-devel libnl3-develipset-devel iptables-devel libnfnetlink-devel net-snmp-devel #第二步,下载源码安装 wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar zxvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure --prefix=/usr/local/keepalived/ make make install cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir -p /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf #将keepalived添加到开机启动服务中,并进行测试 chkconfig keepalived on chkconfig --list | grep keepalived #启动 /etc/init.d/keepalived start 或者 /usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived.conf-D |
1.配置 keepalive haproxy检测脚本
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin A=`ps -C haproxy --no-header |wc -l` if [ $A -eq 0 ] then echo 'haproxy server is died' killall keepalived fi |
注意,有些机器可能没有killall命令,需要安装
1 |
yum -y install psmisc |
配置在/opt/.local/check_haproxy_alive.sh 下(这个看个人喜好)
chmod +x /opt/.local/check_haproxy_alive.sh #添加执行权限,下面keepalived配置文件要用到
2.配置(192.168.6.121) node1 的 keepalive
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 |
! Configuration File for keepalived vrrp_script check_haproxy_alive { script "/opt/.local/check_haproxy_alive.sh" #运行脚本,脚本内容上面有,就是起到一个nginx宕机以后,自动结束掉keepalived服务, interval 3 #检测时间间隔 weight -10 #如果条件成立的话,则权重 -10 } global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 {##定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称 state MASTER ##设置节点node1为master 主分支 角色 interface ens192 ##绑定虚拟 IP 的网络接口,与本机IP地址所在的网络接口相同 mcast_src_ip 192.168.6.121 ## 本机 IP 地址 virtual_router_id 51 #虚拟路由的ID号,两个节点设置必须一样 priority 100 ##节点优先级,值范围 0-254, MASTER 要比 BACKUP 高 advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s authentication { ### 设置验证信息,两个节点必须一致 auth_type PASS auth_pass 1111 } #将 track_script 块加入 instance 配置块 track_script { check_haproxy_alive #执行 Nginx 监控的服务 } virtual_ipaddress { ##提供给Tomcat应用的虚拟服务地址 192.168.6.120 ## 虚拟 ip,可以定义多个 } } |
端口映射(根据自己需求):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
virtual_server 192.168.200.100 0 {# 虚拟服务器地址 IP 对外提供服务的端口 0代表所有端口 delay_loop 6 # 健康检查时长 单位秒 lb_algo rr # 负载均衡算法 rr|wrr|lc|wlc|lblc|sh|dh:LVS调度算法 lb_kind NAT # 负载均衡转发规则,一般用dr,nat调度器会有瓶颈问题 persistence_timeout 50 # http服务会话时长 单位秒 protocol TCP # TCP|UDP|SCTP:4层协议 real_server 192.168.201.100 443 { # 真实的对外提供服务的地址跟IP weight 1 # 权重 权重越高转发优先级越高 SSL_GET { # HTTP_GET | SSL_GET | TCP_CHECK url { path /index.html digest e93e7f6cfbc7c343707f21e2f681dd31 } connect_timeout 3 # 服务连接端口 nb_get_retry 3 # 服务连接失败重试次数 delay_before_retry 3 # 重试连接间隔 单位 秒 } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
virtual_server 192.168.188.204 0 { delay_loop 3 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.188.204 0 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 6443 } } } |
3.配置(192.168.6.122) node2的keepalive
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 |
! Configuration File for keepalived vrrp_script check_haproxy_alive { script "/opt/.local/check_haproxy_alive.sh" interval 3 weight -10 } global_defs { router_id LVS_DEVEL } vrrp_instance VI_2 { state BACKUP interface ens192 mcast_src_ip 192.168.6.122 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy_alive } virtual_ipaddress { 192.168.6.120 } } |
4.配置(192.168.6.123) node3的keepalive
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 |
! Configuration File for keepalived vrrp_script check_haproxy_alive { script "/opt/.local/check_haproxy_alive.sh" interval 3 weight -10 } global_defs { router_id LVS_DEVEL } vrrp_instance VI_3 { state BACKUP interface ens192 mcast_src_ip 192.168.6.123 virtual_router_id 51 priority 30 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy_alive } virtual_ipaddress { 192.168.6.120 } } |
配置好后重新启动keepalive
1 2 |
systemctl daemon-reload #重新加载keepalived配置文件 systemctl restart keepalived.service #重新启动 |
1 2 3 4 5 6 7 8 9 |
#多种启动方法 启动 keepalive systemctl start keepalived systemctl enable keepalived #设置开机自启动 /etc/init.d/keepalived start /usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived.conf-D |
查看三台机器是否正常启动



5.查看虚拟ip 是否已经绑定到ens192

6.验证访问
分别修改 haproxy 账号密码(通过密码不同来区分keepalive访问的哪台机器HA后台)
1 2 3 |
node1 :admin1 111111 node2 :admin2 222222 node3 : admin3 333333 |



设置完成后重新启动下HA(三个节点都要重启)
1 2 3 |
ps -ef|grep haproxy kill -9 [id] |
先来通过vip 访问下HA后台(默认应该指向的node1,因为node1是keepalive的主节点master,其他2个都是BACKUP)


模拟node1故障(关闭node1 HA)
1 2 |
ps -ef|grep haproxy kill -9 xxx |

访问:关闭掉node1节点后,再次访问 http://192.168.6.124:7777/admin 会再次提示你输入账号密码,因为主机器已经切换到了第二台,或者三台

输入第二台或者第三台的密码登录成功,这样就证明keepalive已经成功了!
提示:测试的时候最好关闭你的防火墙,因为会出幺蛾子,可能是虚拟ip不存在的原因吧
- 本文固定链接: https://www.yoyoask.com/?p=1028
- 转载请注明: shooter 于 SHOOTER 发表