haproxy实现mycat高可用负载均衡
1.实验架构图:http://i2.运维网.com/images/blog/201801/02/39dfbcb1e57ee808fa9a7495f01d65fb.png
2.实验基础信息记录:
实验环境vmware虚机6台。
其中2台安装mysql,mysql做主从。
其中2台安装mycat,用来做mycat的高可用。
其中2台安装haproxy和keepalived。
http://i2.运维网.com/images/blog/201801/02/27c15b0bd5fa1ee73ce273cb43ae78d0.png
主机名和IP对应如下:
mycat_ha1 192.168.211.135
mycat_ha2 192.168.211.143
mycat1 192.168.211.145
mycat2 192.168.211.144
mycat_mysql1 192.168.211.147
mycat_mysql2 192.168.211.146
主机修改名称命令:
hostnamectl set-hostname主机名
http://i2.运维网.com/images/blog/201801/02/7653db81b9b0383f2d86bd4a50f6da65.png
具体部署过程概述:
从零开始。首先准备6个最小化安装的虚机。
然后根据自己的命名规则,命名虚机。
两台虚机安装mysql,且配置主从。这里两台虚机是mycat_mysql1和mycat_mysql2。
两台虚机安装mycat,配置。这里两台虚机是mycat1和mycat2。
两台虚机安装haproxy和keepalived,配置mycat高可用和haproxy高可用。
配置全部完成后,测试。
系统环境和软件版本:
centos 7
mycat 1.6
mysql 5.7.20
注意事项:
实验环境,selinux和firewalld全部关闭状态进行的。
3.安装mysql,初始化数据库,配置连接mycat的用户和做mysql主从
以mycat_mysql1主机做示例,两台mysql虚机的安装过程是一样的,如下:
3.1
配置mysql源
我这里用mysql5.7,centos默认没有mysql5.7。需要安装mysql源。
##下载源安装包
#wgethttps://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
http://i2.运维网.com/images/blog/201801/02/4a4dd006ae6de2a6f7f317ec051757a4.png
# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
warning: mysql57-community-release-el7-11.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... #################################
Updating / installing...
1:mysql57-community-release-el7-11 #################################
##安装完成后,见如下,多了mysql的源
http://i2.运维网.com/images/blog/201801/02/c8be3e0dc1764a5dafe66fcd17391c49.png
3.2
用mysql源安装mysql
注意:网络不是很快,耐心点,没法子。
# yum -y install mysql-server mysql-devel
3.3
mysql基础配置,设置密码配置开机启动
systemctl start mysqld.service ##启动
systemctl enable mysqld.service ##设置开机启动
systemctl status mysqld.service ##查看状态
http://i2.运维网.com/images/blog/201801/02/1e3b6dc788f643014cce1b099e68fe9a.png
可以看到已经启动了。
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个临时的默认密码。
# cat /var/log/mysqld.log |grep password
2018-01-02T07:46:53.390748Z 1 A temporary password is generated for root@localhost: Oe5eta)p&Zab
#
临时密码登录mysql后,必须先修改root密码,而且密码有规则,必须包含大小写字母数字还有符号,必须8位以上。
mysql> alter user 'root'@'localhost' identified by 'Passw0rd!';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
3.4
创建一个账号,后面用来mycat连接。
mysql> grant all privileges on . to 'root'@'%' identified by 'Passw0rd!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show grants for 'root'@'%';
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON . TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)
3.5
配置mysql的主从
这里是mycat_mysql1为主,mycat_mysql2为从
只做最基础的配置,优化之类的暂时不弄。
一些说明:
配置主从,需要打开binlog,配置server-id,设置主从间的读取日志的用户。
首先开启binlog和配置server-id
# pwd
/var/lib/mysql
# mkdir logs
# chown mysql:mysql logs
#
创建个文件夹,来存放binlog,注意这个文件夹的权限必须是mysql:mysql。
在/etc/my.cnf 下面添加,如下:
log-bin=/var/lib/mysql/logs/mysql-bin
server-id=6
http://i2.运维网.com/images/blog/201801/02/528fd6ba2faa79d1c8167f60e660527d.png
注意:server-id 主从的不能一样。
设置好了重启下服务,看看binlog启动了没。
http://i2.运维网.com/images/blog/201801/02/480e306daa47bda3d4f479b2e46d6ccc.png
启动了。
创建主从复制的账号 #只需要在mycat_mysql1上创建,这是主
mysql> grant replication slave,replication client on .to repl@'%' identified by 'Passw0rd!';
Query OK, 0 rows affected, 1 warning (0.01 sec)
记录binlog position 记录主上面的
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
接下来在从上面启动复制,这里是mycat_mysql2
mysql> change master to master_host='192.168.211.147',
-> master_user='repl',
-> master_password='Passw0rd!',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.11 sec)
启动
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
查看状态
http://i2.运维网.com/images/blog/201801/02/af2e9d2fc304769147ed17237e8556f2.png
启动成功:
主上面会启动binlog dump线程
http://i2.运维网.com/images/blog/201801/02/7ac6b04af90b5c6b8a90d2ec57e30aca.png
从上面会启动 IOSQL线程
http://i2.运维网.com/images/blog/201801/02/c96568dffe78595053e9cc81cb4da176.png
测试下,在主上面创建个数据库:
mysql> create database test1 character set utf8;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| logs |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
去从哪里看看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| logs |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
成功。
到这一步,最基础的mysql配置已经全部完成。
4.配置mycat
4.1 软件准备
只是做最简单的配置,实验环境。服务起来能达到基本架构需求即可。
mycat安装需要jdk9以上,可以去java官网下载,下载好了上传到安装mycat的虚机。如下是我下载的版本:
# ls
jdk-9.0.1_linux-x64_bin.tar.gzMycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
#
4.2 解压缩到目录
# tar -zxvf jdk-9.0.1_linux-x64_bin.tar.gz -C /usr/local
# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz-C /usr/local/
# pwd
/usr/local
# ls
binetcgamesincludejdk-9.0.1liblib64libexecmycatsbinsharesrc
#
4.3 设置java和mycat环境变量
在/etc/profile文件里最后面加入下面的配置:
export JAVA_HOME=/usr/local/jdk-9.0.1
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
export MYCAT_HOME=/usr/local/mycat
然后执行:
# source /etc/profile
# java --version
java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
#
版本显示出来了,java就正常了。
# java --version
java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
#
4.3 创建mycat用户和群组用来运行mycat服务
# groupadd mycat
# useradd mycat -M -g mycat
# chown -R mycat:mycat /usr/local/mycat/
#
4.4 修改/etc/hosts文件,使得可以解析mysql节点的名称
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.211.147 mycat_mysql1
192.168.211.146 mycat_mysql2
4.5 mycat配置文件介绍
看看mycat的配置文件
# pwd
/usr/local/mycat
# ls
bincatletconfliblogsversion.txt
#
简单介绍:bin 执行文件放这里conf 配置文件放这里logs 日志文件放这里
基本配置只需要修改conf文件夹里的配置文件,如下:
# ls
autopartition-long.txt index_to_charset.propertiespartition-range-mod.txt sequence_distributed_conf.propertieszkconf
auto-sharding-long.txt log4j2.xml rule.xml sequence_time_conf.properties zkdownload
auto-sharding-rang-mod.txtmigrateTables.properties schema.xml server.xml
cacheservice.properties myid.properties sequence_conf.properties sharding-by-enum.txt
ehcache.xml partition-hash-int.txt sequence_db_conf.propertieswrapper.conf
简单介绍:
schema.xml ##数据库表格
server.xml ##账号密码
rule.xml ##规则
log4j2.xml ##日志
这几个是要用的主配置文件。
等下会修改,稳妥为上,先把这几个初始文件备份一下,这个自行决定可忽略。
# mv schema.xml.bk schema.xml
# cp schema.xml schema.xml.bk
# cp server.xml server.xml.bk
# cp rule.xml rule.xml.bk
# cp log4j2.xml log4j2.xml.bk
4.6 配置schema.xml
我是做简单的配置,如果要配置分库分表,参考前面几篇。
我的配置文件,参考如下:
select user()
4.7 配置下server.xml
把下面选项里的默认数据库名字改成自己的就可以了
4.8 启动服务,登录看看
# ../bin/mycat start
Starting Mycat-server...
看看启动了没有
# ps -ef |grep mycat
root 2827 10 08:46 ? 00:00:00 /usr/local/mycat/bin/./wrapper-linux-x86-64 /usr/local/mycat/conf/wrapper.conf wrapper.syslog.ident=mycat wrapper.pidfile=/usr/local/mycat/logs/mycat.pid wrapper.daemonize=TRUE wrapper.lockfile=/var/lock/subsys/mycat
root 282928276 08:46 ? 00:00:00 java -DMYCAT_HOME=. -server -XX:MaxPermSize=64M -XX:+AggressiveOpts -XX:MaxDirectMemorySize=2G -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1984 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xmx4G -Xms1G -Djava.library.path=lib -classpath lib/wrapper.jar:conf:lib/zookeeper-3.4.6.jar:lib/jline-0.9.94.jar:lib/ehcache-core-2.6.11.jar:lib/log4j-1.2.17.jar:lib/fastjson-1.2.12.jar:lib/curator-client-2.11.0.jar:lib/joda-time-2.9.3.jar:lib/log4j-slf4j-impl-2.5.jar:lib/libwrapper-linux-x86-32.so:lib/netty-3.7.0.Final.jar:lib/druid-1.0.26.jar:lib/log4j-api-2.5.jar:lib/mapdb-1.0.7.jar:lib/slf4j-api-1.6.1.jar:lib/univocity-parsers-2.2.1.jar:lib/hamcrest-core-1.3.jar:lib/Mycat-server-1.6-RELEASE.jar:lib/objenesis-1.2.jar:lib/leveldb-api-0.7.jar:lib/hamcrest-library-1.3.jar:lib/wrapper.jar:lib/commons-lang-2.6.jar:lib/reflectasm-1.03.jar:lib/mongo-java-driver-2.11.4.jar:lib/guava-19.0.jar:lib/curator-recipes-2.11.0.jar:lib/curator-framework-2.11.0.jar:lib/libwrapper-linux-ppc-64.so:lib/log4j-core-2.5.jar:lib/leveldb-0.7.jar:lib/sequoiadb-driver-1.12.jar:lib/mysql-binlog-connector-java-0.4.1.jar:lib/kryo-2.10.jar:lib/jsr305-2.0.3.jar:lib/commons-collections-3.2.1.jar:lib/disruptor-3.3.4.jar:lib/log4j-1.2-api-2.5.jar:lib/velocity-1.7.jar:lib/libwrapper-linux-x86-64.so:lib/dom4j-1.6.1.jar:lib/minlog-1.2.jar:lib/asm-4.0.jar -Dwrapper.key=iYNPXI1FIFvTaSnP -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=2827 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper -Dwrapper.service=TRUE -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1 org.tanukisoftware.wrapper.WrapperSimpleApp io.mycat.MycatStartup start
root 284121020 08:46 pts/0 00:00:00 grep --color=auto mycat
登录看看
# mysql -uroot -p123456 -P8066 -h127.0.0.1
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
进来了。
注意mycat有两个口,8066和9066,9066是管理口。
登录管理口看看
# mysql -uroot -p123456 -P9066 -h127.0.0.1
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (monitor)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
可以执行命令看看,节点起来了吗
MySQL [(none)]> show @@database;
+----------+
| DATABASE |
+----------+
| test |
+----------+
1 row in set (0.00 sec)
MySQL [(none)]> show @@datanode;
+------+------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST | INDEX | TYPE| ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1| mycat_mysql1/db1 | 0 | mysql | 0 | 0 | 1000 | 0 | 0 | 0 | 0 | -1 |
+------+------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
1 row in set (0.01 sec)
MySQL [(none)]>
到这一步。基础mycat配置完成。
注意两台mycat虚机做一样的配置,后面用haproxy做这两台mycat的高可用。
[*]配置haproxy,做mycat的高可用和负载均衡
5.1 haproxy的安装
直接用源安装
# yum -y installhaproxy
主要配置文件
# rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/lib/systemd/system/haproxy.service
/usr/sbin/haproxy
/usr/sbin/haproxy-systemd-wrapper
/etc/haproxy/haproxy.cfg 这是配置文件
5.2 动手配置haproxy
先备份下配置文件吧。养成好习惯。
# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bk
haproxy的配置文件由两部分组成:全局设定和代理设定。分为五段:global,defaults,frontend,backend,listen.
不考虑优化,做些简单配置,global和default配置大部分可以使用默认即可。
注意:
执行haproxy服务的用户是haproxy,需要修改下文件的用户和群主。
# chown -Rhaproxy:haproxy /etc/haproxy/
# ll /etc |grep haproxy
drwxr-xr-x 2 haproxy haproxy 47 Jan2 09:35 haproxy
#
配置下haproxy的日志
修改:/etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
这两项的注释去掉
设置下haproxy.log的路径
local2.* /var/log/haproxy.log
修改:/etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-r -m 0 -c 2"
然后重启下rsyslog服务。
# systemctl restart rsyslog.service
日志配置还有问题回头测试。
我的配置文件,参考如下:
# cat /etc/haproxy/haproxy.cfg |grep -v "^$" |grep -v "^#" |sed '/#/d'
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 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
frontend mycat
bind 0.0.0.0:8066
bind 0.0.0.0:9066
mode tcp
log global
default_backend mycat_server
backend mycat_server
balance roundrobin
servermycat1 192.168.211.145:8066 check inter 5s rise 2 fall 3
servermycat2 192.168.211.144:8066 check inter 5s rise 2 fall 3
servermycatadmin1 192.168.211.145:9066 check inter 5s rise 2 fall 3
servermycatadmin2 192.168.211.144:9066 check inter 5s rise 2 fall 3
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
5.3 启动haproxy
# systemctl start haproxy.service
看看启动日志
Jan2 11:34:13 mycat_ha2 systemd: Started HAProxy Load Balancer.
Jan2 11:34:13 mycat_ha2 systemd: Starting HAProxy Load Balancer...
Jan2 11:34:13 localhost haproxy: Proxy mycat started.
Jan2 11:34:13 localhost haproxy: Proxy mycat started.
Jan2 11:34:13 localhost haproxy: Proxy mycat_server started.
Jan2 11:34:13 localhost haproxy: Proxy stats started.
通过:
http://192.168.211.143:1080/haproxyadmin?stats
访问监控页面,正常
找一台安装了mysql客户端的虚机通过haproxy虚机的地址连接测试看看:
连管理口:
http://i2.运维网.com/images/blog/201801/03/2c40277b78edd147cd0ec8555913cf29.png
连数据口
http://i2.运维网.com/images/blog/201801/03/7028178aa39b216c15001d40ed2feaff.png
看看日志:
Jan2 11:38:10 localhost haproxy: 192.168.211.147:60024 mycat mycat_server/mycat1 1/0/49239 198 -- 0/0/0/0/0 0/0
Jan2 11:38:10 localhost haproxy: 192.168.211.147:60024 mycat mycat_server/mycat1 1/0/49239 198 -- 0/0/0/0/0 0/0
Jan2 11:39:14 localhost haproxy: 192.168.211.147:60322 mycat mycat_server/mycat2 1/1/60013 198 cD 0/0/0/0/0 0/0
Jan2 11:39:14 localhost haproxy: 192.168.211.147:60322 mycat mycat_server/mycat2 1/1/60013 198 cD 0/0/0/0/0 0/0
Jan2 11:40:43 localhost haproxy: 192.168.211.147:60324 mycat mycat_server/mycatadmin1 1/1/16608 193 -- 0/0/0/0/0 0/0
Jan2 11:40:43 localhost haproxy: 192.168.211.147:60324 mycat mycat_server/mycatadmin1 1/1/16608 193 -- 0/0/0/0/0 0/0
到这一步,基本需求已经实现。
另外台安装haproxy的虚机做同样配置。
5.4 测试mycat的ha和负载均衡
haproxy解决了mycat的ha问题,后端的mycat服务,只要有一个是正常状态,服务就不会中断。
mycat负载均衡测试:
通过haproxy地址登录mycat,多登录几次,查看日志对比,参考如下:
Jan3 02:14:25 localhost haproxy: 192.168.211.145:33894 mycat mycat_server/mycat2 1/0/60014 198 cD 1/1/1/0/0 0/0
Jan3 02:14:25 localhost haproxy: 192.168.211.145:33894 mycat mycat_server/mycat2 1/0/60014 198 cD 1/1/1/0/0 0/0
Jan3 02:14:58 localhost haproxy: 192.168.211.144:47170 mycat mycat_server/mycatadmin2 1/1/60016 193 cD 0/0/0/0/0 0/0
Jan3 02:14:58 localhost haproxy: 192.168.211.144:47170 mycat mycat_server/mycatadmin2 1/1/60016 193 cD 0/0/0/0/0 0/0
Jan3 02:16:02 localhost haproxy: 192.168.211.144:47292 mycat mycat_server/mycat1 1/0/62495 429 cD 0/0/0/0/0 0/0
Jan3 02:16:02 localhost haproxy: 192.168.211.144:47292 mycat mycat_server/mycat1 1/0/62495 429 cD 0/0/0/0/0 0/0
可以看到,已经实现了负载的要求。
6.安装配置keepalived,解决haproxy的单点故障
6.1 用源安装keepalived
# yum -y install keepalived
6.2 keepalived的配置
keepalived的基础知识
keepalived模块化设计,不同模块负责不同的功能,主要模块如下:
core keepalived的核心,负责进程的启动和维护,全局配置文件的加载解析等
check 负责healthchecker(健康检查),包含各种健康检查方式,以及对应的配置的解析
vrrp vrrpd的子进程,vrrpd子进程就是实现vrrpd协议的
libipfwc iptables库,配置lvs会用到
libipvs* 配置lvs会用到
注意:
keepalived启动后会有三个进程
父进程:内存管理,子进程管理等等
子进程:VRRP子进程
子进程:healthchecker子进程
keepalived配置文件解释
keepalived有三类配置区域
全局配置
vrrpd配置
lvs配置
全局配置包括两个子配置
全局定义
静态路由配置
全局配置解析
global_defs全局配置标识,表面这个区域{}是全局配置
静态路由配置
这个区域一般不用
vrrpd配置
vrrpd配置又分为vrrp同步组配置和vrrp实例配置和vrrp脚本
vrrp同步组
VRRP同步组的作用是:当VRRP路由器上接2个网段时。只要当其中的一个网段出现问题,都会导致keepalived发生切换时事件。
如果不使用同步组的话,一旦发生故障,VRRP路由器仍然认为自己是健康的,因此不会发生切换事件。从而会导致问题。
我这边用的是同一网段,不用配置这个。
vrrp实例vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
state state指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,
但这里指定的不算,还是得要通过竞选通过优先级来确定,如果这里设置为master,但如若他的优先级不及另外一台,
那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为master
interface 实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
virtual_router_id 设置vrid,主备的必须一致
priority 设置本节点的优先级,数值越大优先级越高,优先级高的的为master
advert int检查间隔,默认为1秒
authentication这里设置认证
auth type 认证方式,可以是PASS或AH两种认证方式
auth pass 认证密码
virtual_ipaddress VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,
当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
vrrp脚本
vrrp脚本很重要,用来监控做HA的服务的状态,当然服务停止了,就停止keepalived的服务,实现切换vip地址的功能。
6.3 我的配置文件,如下:
虚机mycat_ha1
global_defs {
router_id NodeA
}
vrrp_script chk_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state MASTER #设置为主服务器
interface ens33#监测网络接口
virtual_router_id 51#主、备必须一样
priority 100 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
advert_int 1 #VRRP Multicast广播周期秒数
track_script {
chk_haproxy
}
authentication {
auth_type PASS#VRRP认证方式,主备必须一致
auth_pass 1111 #(密码)
}
virtual_ipaddress {
192.168.211.180/24#VRRP HA虚拟地址
}
}
虚机mycat_ha2:
global_defs {
router_id NodeB
}
vrrp_script chk_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state MASTER #设置为主服务器
interface ens33#监测网络接口
virtual_router_id 51#主、备必须一样
priority 90 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
advert_int 1 #VRRP Multicast广播周期秒数
track_script {
chk_haproxy
}
authentication {
auth_type PASS#VRRP认证方式,主备必须一致
auth_pass 1111 #(密码)
}
virtual_ipaddress {
192.168.211.180/24#VRRP HA虚拟地址
}
}
脚本两个主机都要有,配置一样
# cat check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl start haproxy.service
fi
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
pkill keepalived
fi
6.4 启动服务测试
# systemctl start keepalived.service
# systemctl enable keepalived.service
看看设置权限更大的那台虚机的ip,我这里是mycat_ha1
# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:52:a9:5b brd ff:ff:ff:ff:ff:ff
inet 192.168.211.135/24 brd 192.168.211.255 scope global dynamic ens33
valid_lft 1574sec preferred_lft 1574sec
inet 192.168.211.180/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::8d56:bf7:da17:8b45/64 scope link
valid_lft forever preferred_lft forever
192.168.211.180就是我们配置的vip,已经起来了。
测试:
关闭现在活得vip的虚机的keepalived服务,看vip会切换吗?
# systemctl stop keepalived.service
Jan3 04:07:25 mycat_ha1 Keepalived: Stopping
Jan3 04:07:25 mycat_ha1 systemd: Stopping LVS and VRRP High Availability Monitor...
Jan3 04:07:25 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) sent 0 priority
Jan3 04:07:25 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Jan3 04:07:25 mycat_ha1 Keepalived_healthcheckers: Stopped
Jan3 04:07:26 mycat_ha1 Keepalived_vrrp: Stopped
Jan3 04:07:26 mycat_ha1 systemd: Stopped LVS and VRRP High Availability Monitor.
Jan3 04:07:26 mycat_ha1 Keepalived: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
看看mycat_ha2 活得vip了吗?
# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:58:bd:ba brd ff:ff:ff:ff:ff:ff
inet 192.168.211.143/24 brd 192.168.211.255 scope global dynamic ens33
valid_lft 1390sec preferred_lft 1390sec
inet 192.168.211.180/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::1075:640f:6448:6b4e/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::bd24:c53:6545:1cc6/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::8d56:bf7:da17:8b45/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
#
正常
再启动mycat_ha1的keepalived服务
Jan3 04:10:05 mycat_ha1 systemd: Started LVS and VRRP High Availability Monitor.
Jan3 04:10:05 mycat_ha1 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: Registering Kernel netlink reflector
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: Registering Kernel netlink command channel
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: Registering gratuitous ARP shared channel
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: VRRP sockpool:
Jan3 04:10:05 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.211.180
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
Jan3 04:10:06 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
#
mycat_ha1 权限更高,重新获取vip
mycat_ha1虚机
停止haproxy服务,测试:
# systemctl start haproxy.service
#
看日志
Jan3 04:20:59 mycat_ha1 Keepalived_vrrp: Sending gratuitous ARP on ens33 for 192.168.211.180
Jan3 04:21:14 mycat_ha1 systemd: Stopping HAProxy Load Balancer...
Jan3 04:21:14 mycat_ha1 systemd: Stopped HAProxy Load Balancer.
Jan3 04:21:14 mycat_ha1 haproxy-systemd-wrapper: haproxy-systemd-wrapper: exit, haproxy RC=0
Jan3 04:21:15 mycat_ha1 Keepalived: Stopping
Jan3 04:21:15 mycat_ha1 Keepalived_healthcheckers: Stopped
Jan3 04:21:15 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) sent 0 priority
Jan3 04:21:15 mycat_ha1 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Jan3 04:21:15 mycat_ha1 Keepalived_vrrp: Stopped
Jan3 04:21:15 mycat_ha1 Keepalived: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
脚本执行成功,vip已经切换成功。
keepalived已经部署成功。
[*]通过vip地址访问mycat
# mysql -uroot -p123456 -P8066 -h 192.168.211.180
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL > show tables;
+----------------+
| Tables in test |
+----------------+
| t1 |
| t2 |
| t3 |
+----------------+
3 rows in set (0.00 sec)
MySQL > desc t1;
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
MySQL > desc t2;
ERROR 1146 (42S02): Table 'db1.t2' doesn't exist
MySQL > create table t1(id int);
Query OK, 0 rows affected (0.02 sec)
MySQL > desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES| | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)
MySQL >
看看获取vip那台虚机的日志
Jan3 04:26:37 localhost haproxy: 192.168.211.145:60346 mycat mycat_server/mycat1 1/0/120656 1167 cD 0/0/0/0/0 0/0
Jan3 04:26:37 localhost haproxy: 192.168.211.145:60346 mycat mycat_server/mycat1 1/0/120656 1167 cD 0/0/0/0/0 0/0
到这一步。
实验架构图的预设需求已经达到。
页:
[1]