本文主要向大家介绍了Linux运维知识之keepalived + haproxy + mysql 构建高可用数据库,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
keepalived + haproxy + mysql 构建高可用
keepalived 的高可用是主备,有一台作为备用
keepalived + haproxy 搭建的高可用是可以两台都会调度的高可用
拓扑图:
keepalived:负责抢占虚拟ip,使用vrrp协议haproxy:负责做访问调度,减轻单点压力,单独监听一个端口,这里用23306
1、安装mysql
分别在两台机器上面搭建mysql,并做主从配置,这里不做介绍
2、搭建haproxy
download 源码包,下载地址:http://www.haproxy.org/#down
在81.128和81.129解压缩安装
tar xf haproxy-1.8.4.tar.gzcd haproxy-1.8.4yum install -y gccmake TARGET=linux310 ARCH=x86_64 # uname -a查看主机信息填写make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/
3、提供启动脚本
#!/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 0exec="/usr/sbin/haproxy"prog=$(basename $exec)[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$progcfgfile=/etc/haproxy/haproxy.cfgpidfile=/var/run/haproxy.pidlockfile=/var/lock/subsys/haproxycheck() { $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 2esac
4、提供配置文件
mkdir /etc/haproxymkdir /var/lib/haproxyuseradd -r haproxy
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/statsdefaults 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 600listen 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 bind *:23306 default_backend mysqlbackend mysql balance leastconn server m1 192.168.81.128:3306 check port 3306 maxconn 300 server m2 192.168.81.129:3306 check port 3306 maxconn 300
5、修改日志系统
###Provides UDP syslog reception //去掉下面两行注释,开启UDP监听$ModLoad imudp$UDPServerRun 514local2.* /var/log/haproxy.log //添加此行service rsyslog restart
6、启动测试haproxy
service haproxy start chkconfig --add haproxychkconfig haproxy onnetstat -tnlpmysql -P23306 -uroot -p123456 -h192.168.81.129 # 查看
server_id,判断是否成功
7、搭建keepalived
1、download 源码包,下载地址:http://www.keepalived.org/download.html
2、在81.128和81.129解压缩安装
tar xf keepalived-1.2.7.tar.gz cd keepalived-1.2.7./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/make && make installchkconfig --add keepalivedchkconfig keepalived on
3、提供配置文件
vim /etc/keepalived/keepalived.conf # 两个机器配置文件不同! Configuration File for keepalivedglobal_defs { notification_email { # 忽略 acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_haproxy { script "/etc/keepalived/chk.sh" # 检查haproxy的脚本 interval 2 # 每两秒检查一次}vrrp_instance VI_1 { state BACKUP # 定义为BACKUP节点 nopreempt # 开启不抢占,另一个不写 interface ens33 virtual_router_id 51 priority 100 # 开启了不抢占,所以此处优先级必须高于另一台,另一个写99 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.81.150 # 配置VIP } track_script { chk_haproxy # 调用检查脚本 } notify_backup "/etc/init.d/haproxy restart" notify_fault "/etc/init.d/haproxy stop"}
4、创建check文件
vim /etc/keepalived/chk.sh
#!/bin/bashif [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then /etc/init.d/keepalived stopfichmod +x /etc/keepalived/chk.sh service keepalived start
5、测试
ip addr # 查看是否绑定了虚iptcpdump -nn -i ens33 vrrp # 抓包查看http://192.168.81.128:6677/haproxyadmin?stats # 通过haproxy查看状态
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!