keepalived + haproxy + mycat + mysql搭建高可用

1.安装Mysql

链接

2.安装Mycat

链接

3.安装haproxy

yum install -y haproxy
#配置
vim /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  tcplog
    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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    #acl url_static       path_beg       -i /static /images /javascript /stylesheets
    #acl url_static       path_end       -i .jpg .gif .png .css .js

    #use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
 #   balance     roundrobin
 #  server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 192.168.139.101:3306 check
    server  app2 192.168.139.102:3306 check
    #server  app3 127.0.0.1:5003 check
    #server  app4 127.0.0.1:5004 check


4.安装keepalived

yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
yum install -y keepalived

#启动 keepalived
systemctl start keepalived   
#加入开机启动 keepalived
systemctl enable keepalived  
#重新启动 keepalived
systemctl restart keepalived  
#查看 keepalived 状态
systemctl status keepalived   

配置文件对格式有严格要求 {}前面必须要有空格

! Configuration File for keepalived

global_defs {
     
   notification_email {
     
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.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 chk_haproxy {
     
	script "killall -0 haproxy"
	#(检测脚本执行的间隔)
	interval 2
}

vrrp_instance VI_1 {
     
	# MASTER / BACKUP  从服务器用BACKUP
    state BACKUP
	# 网卡名称
    interface ens33
	# 主、备机的 virtual_router_id 必须相同
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
     
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     
	# 虚拟地址 主从配置必须一样  才可切换
        192.168.139.199
    }
	# 使用 vrrp_script 脚本
	track_script {
     
		chk_haproxy
	}
	
}

virtual_server 192.168.139.199 6000 {
     
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
	# 真实地址
    real_server 192.168.139.102 5000 {
     
        weight 1      
    }
}

5.检验

# 查看ip 可以看到虚拟网址 192.168.139.199
ip addr
# 关闭本机haproxy进程
ps -ef|grep haproxy
kill -9 'pid'
# 再次查看ip 将看不到虚拟网址
ip addr
# 另一台机器查看 ip a  将可以发现192.168.139.199网址

你可能感兴趣的:(mysql,mycat,linux,centos,mysql,keepalived,haproxy)