欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mycat、HAProxy、Keepalived整合高可用(有点问题)

程序员文章站 2022-05-08 23:08:08
...

Mycat HAProxy 整合高可用

我们可以使用 HAProxy + Keepalived 配合两台 Mycat 搭起 Mycat 集群,实现高可用性。

HAProxy 保证了 MyCat 多节点的负载均衡和集群高可用性。

Keepalived 用来保证 HAProxy 自身的高可用性。

1、整体规划

编号 角色 IP 地址 机器名
1 Mycat1 192.168.140.128 host79
2 Mycat2 192.168.140.127 host80
3 HAProxy(master) 192.168.140.126 host81
4 Keepalived(master) 192.168.140.126 host81
5 HAProxy(backup) 192.168.140.125 host82
6 Keepalived(backup) 192.168.140.125 host82

2、HAProxy 安装

2.1、HAProxy 安装

1、准备好 HAProxy 安装包,传到/opt目录下

haproxy-1.8.13.tar.gz

2、解压到 /usr/local/src

tar -zxvf haproxy-1.8.13.tar.gz -C  /usr/local/src
cd /usr/local/src/haproxy-1.8.13/

3、查询系统内核版本

[aaa@qq.com haproxy-1.8.13]# uname -r   
3.10.0-862.el7.x86_64

4、编译时,centos6.X需要使用 TARGET=linux26 ,centos7.x 使用 linux31

[aaa@qq.com haproxy-1.8.13]# make TARGET=linux31 PREFIX=/usr/local/haproxy ARCH=x86_64

5、安装到 /usr/local/haproxy 目录下

[aaa@qq.com haproxy-1.8.13]# make install PREFIX=/usr/local/haproxy

6、创建目录、创建HAProxy配置文件

[aaa@qq.com haproxy-1.8.13]# mkdir -p /usr/data/haproxy/
[aaa@qq.com haproxy-1.8.13]# vi /usr/local/haproxy/haproxy.conf

2.2、haproxy.conf 配置文件


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.140.128:8066 check inter 10s
	server mycat_2 192.168.140.127:8066 check inter 10s

frontend admin_stats
	bind *:7777   #前端的主机和端口号 
	mode http
	stats enable
	option httplog
	maxconn 10
	stats refresh 30s
	stats uri /admin
	stats auth admin:123123
	stats hide-version
	stats admin if TRUE

2.3、HAProxy 启动

1、启动HAProxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf

2、查看HAProxy进程

ps -ef|grep haproxy

2.4、登录 HAProxy、查看

打开浏览器访问

http://192.168.140.125:7777/admin

在弹出框输入用户名: admin ,密码: 123123

Mycat、HAProxy、Keepalived整合高可用(有点问题)

如果Mycat主备机均已启动,则可以看到如下图

Mycat、HAProxy、Keepalived整合高可用(有点问题)

4、验证负载均衡,通过HAProxy访问Mycat

mysql -umycat -p123456 -h 192.168.140.126 -P 48066 

说明:

-u 表示 用户名

-p 表示 密码

-h 表示host,主机,IP地址

-P 表示端口号

3、配置 Keepalived

3.1、 安装 Keepalived

1、准备好Keepalived安装包,传到/opt目录下

keepalived-1.4.2.tar.gz

2、解压到 /usr/local/src 目录下

tar -zxvf keepalived-1.4.2.tar.gz -C /usr/local/src

3、安装依赖插件

yum install -y gcc openssl-devel popt-devel

4、进入解压后的目录, 进行配置, 进行编译

cd /usr/local/src/keepalived-1.4.2
./configure --prefix=/usr/local/keepalived

5、进行编译, 完成后进行安装

make && make install

6、运行前配置

cp  /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived  /etc/init.d/

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/
cp /usr/local/src/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

3.2、keepalived.conf 配置文件

7、修改配置文件

vim /etc/keepalived/keepalived.conf

修改内容如下:(MASTER,BACKUP 略有不同)

! Configuration File for keepalived
global_defs {
    notification_email {
    	aaa@qq.com
    }
    notification_email_from aaa@qq.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    vrrp_skip_check_adv_addr
    vrrp_garp_interval 0
    vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    #主机配MASTER,备机配BACKUP
    state MASTER
    
    #所在机器网卡
    interface ens33
    virtual_router_id 51
    
    #数值越大优先级越高
    priority 100
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    virtual_ipaddress {
        #虚拟IP
        192.168.140.200
    }
}

virtual_server 192.168.140.200 48066 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    
    real_server 192.168.140.125 48066 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.140.126 48600 {
        weight 1
        
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

interface 是什么?

配置文件中 interface ens33 到底是什么?,如图所示:

Mycat、HAProxy、Keepalived整合高可用(有点问题)

virtual_ipaddress 虚拟IP的作用?

通过虚拟IP,隐藏真实服务的IP地址,从而保护的服务的安全性。

3.3、 启动验证

1、启动Keepalived

service keepalived start

2、登录验证

mysql -umycat -p123456 -h 192.168.140.200 -P 48066 

4、测试

1、关闭mycat

2、通过虚拟ip查询数据

mysql -umycat -p123456 -h 192.168.140.200 -P 48066 
相关标签: Mycat