【Linux集群系列教程】keepalived高可用集群

一、keepalived介绍

keepalived基于vrrp(虚拟冗余路由)协议开发

keep01

二、案例1:keepalived高可用集群MySQL

上一期教程:HAProxy负载均衡集群

keep02

192.168.140.10作为Nginx服务器,反向代理到后台MySQL,并且作为调度器
192.168.140.11作为Nginx服务器,反向代理到后台MySQL,并且作为备用调度器
192.168.140.12作为MySQL数据库,配置双主环境
192.168.140.13作为MySQL数据库,配置双主环境

1.关闭防火墙和SElinux、配置时间同步(重要)

务必同步时间!否则会出现脑裂现象!

2.配置MySQL双主复制环境

MySQL5.7安装教程:https://www.wsjj.top/archives/65
MySQL5.7配置双主复制环境教程:https://www.wsjj.top/archives/76

3.查看MySQL数据库双主复制状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.140.13
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 640
               Relay_Log_File: node1-relay-bin.000004
                Relay_Log_Pos: 445
        Relay_Master_Log_File: master.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 640
              Relay_Log_Space: 1187
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 13
                  Master_UUID: bda75efb-e4ac-11ed-b6fd-000c29d2b7fd
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: bda75efb-e4ac-11ed-b6fd-000c29d2b7fd:1-2
            Executed_Gtid_Set: 95a397fa-e4ac-11ed-8f67-000c290f55d2:1-4,
bda75efb-e4ac-11ed-b6fd-000c29d2b7fd:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.140.12
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 1038
               Relay_Log_File: node2-relay-bin.000004
                Relay_Log_Pos: 745
        Relay_Master_Log_File: master.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1038
              Relay_Log_Space: 1743
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 12
                  Master_UUID: 95a397fa-e4ac-11ed-8f67-000c290f55d2
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 95a397fa-e4ac-11ed-8f67-000c290f55d2:1-4
            Executed_Gtid_Set: 95a397fa-e4ac-11ed-8f67-000c290f55d2:1-4,
bda75efb-e4ac-11ed-b6fd-000c29d2b7fd:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

4.配置Nginx

A.安装Nginx

Nginx的安装教程地址:https://www.wsjj.top/archives/83

B.修改Nginx配置文件

[root@master01 ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}
stream {
   upstream dbserver {
        server 192.168.140.12:3306;
        server 192.168.140.13:3306;
   }
   server {
        listen 3306;
        proxy_pass dbserver;
   }
}
[root@master02 ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}
stream {
   upstream dbserver {
        server 192.168.140.12:3306;
        server 192.168.140.13:3306;
   }
   server {
        listen 3306;
        proxy_pass dbserver;
   }
}

C.启动Nginx

[root@master01 ~]# /usr/local/nginx/sbin/nginx
[root@master02 ~]# /usr/local/nginx/sbin/nginx

D.回到数据库创建授权用户

因为我们配置了双主环境,所以任意一台上创建即可

mysql> grant all on *.* to 'admin'@'192.168.140.%' identified by 'WWW.1.com'; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

E.Windows连接测试

keep03

keep04

keep05

5.配置keeplived

A.安装keeplived

[root@master01 ~]# yum install -y keepalived
[root@master02 ~]# yum install -y keepalived

B.编写脚本用于检测Nginx提供的3306端口

[root@master01 ~]# vim /root/nginx_mysql.sh
#! /bin/bash

netstat -tunlp | grep 3306 &> /dev/null

if [ $? -ne 0 ]; then
    systemctl stop keepalived
fi

C.给予执行权限

[root@master01 ~]# chmod 777 /root/nginx_mysql.sh

D.将脚本拷贝给备用调度器

[root@master01 ~]# scp  /root/nginx_mysql.sh root@192.168.140.11:/root
给予执行权限
[root@master02 ~]# chmod 777 /root/nginx_mysql.sh

E.回到调度器,编辑keepalived配置文件

[root@master01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id master_nginx
}

vrrp_script check_mysql {	#引用外部脚本,并且定义名字
    script "/root/nginx_mysql.sh"	#外部脚本路径
    interval 1	#每1秒执行一次脚本
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mysql		#调用外部脚本,名字是我们上面自己起的
    }
}

F.配置备用调度器

[root@master02 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id slave_nginx
}

vrrp_script check_mysql {
    script "/root/nginx_mysql.sh"
    interval 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mysql
    }
}

G.启动keepalived服务

[root@master01 ~]# systemctl start keepalived.service
[root@master02 ~]# systemctl start keepalived.service

H.Windows连接测试

keep06

keep07

I.模拟主调度故障

[root@master01 ~]# /usr/local/nginx/sbin/nginx -s stop

这时主调度的keepalived服务已经被脚本自动关闭了
可以看到我们在配置文件内定义的192.168.140.100的IP跑到了备用节点上

[root@master02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group defaultqlen 1000
    link/ether 00:0c:29:37:97:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.11/24 brd 192.168.140.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.140.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe37:97c7/64 scope link 
       valid_lft forever preferred_lft forever

J.Windows测试连接

模拟Nginx故障

keep06

keep07

K.恢复主调度运行

[root@master01 ~]# /usr/local/nginx/sbin/nginx
[root@master01 ~]# systemctl start keepalived.service

可以看到我们的192.168.140.100这个IP回到了调度上

[root@master01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8e:07:d7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.10/24 brd 192.168.140.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.140.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8e:7d7/64 scope link 
       valid_lft forever preferred_lft forever
[root@master02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:37:97:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.11/24 brd 192.168.140.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe37:97c7/64 scope link 
       valid_lft forever preferred_lft forever

6.keepalived心跳管理

keepalived心跳地址默认使用224.0.0.1是个组播地址

A.修改组播地址为单播地址

[root@master01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id master_nginx
}

vrrp_script check_mysql {
    script "/root/nginx_mysql.sh"
    interval 1
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mysql
    }
    unicast_src_ip 192.168.140.10	#指定源地址
    unicast_peer {
        192.168.140.11		#指定目标地址
    }
}
[root@master02 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id slave_nginx
}

vrrp_script check_mysql {
    script "/root/nginx_mysql.sh"
    interval 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mysql
    }
    unicast_src_ip 192.168.140.11	#源地址(本机地址)
    unicast_peer {
        192.168.140.10	#目标地址
    }
}

B.查看心跳

如果没有tcpdump命令,请安装yum install -y tcpdump

[root@master01 ~]# tcpdump -i ens33 -nn vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
15:15:52.856323 IP 192.168.140.10 > 192.168.140.11: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:15:53.857127 IP 192.168.140.10 > 192.168.140.11: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:15:54.857973 IP 192.168.140.10 > 192.168.140.11: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
[root@master02 ~]# tcpdump -i ens33 -nn vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
15:13:32.830104 IP 192.168.140.11 > 192.168.140.10: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:13:33.830501 IP 192.168.140.11 > 192.168.140.10: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:13:34.830965 IP 192.168.140.11 > 192.168.140.10: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

7.配置keepalived不抢占VIP

keepalived默认模式下,当调度器挂了以后,备用调度器拿到VIP,而调度器恢复以后,VIP会被调度器抢占回来。
只在优先级高的修改此配置!

[root@master01 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id master_nginx
}

vrrp_script check_mysql {
    script "/root/nginx_mysql.sh"
    interval 1
}

vrrp_instance VI_1 {
    state BACKUP	#修改MASTER为BACKUP
    nopreempt	#新增此条
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mysql
    }
    unicast_src_ip 192.168.140.10
    unicast_peer {
        192.168.140.11
    }
}

三、案例2:配置MyCAT集群

keep08

1.配置MySQL主从环境

MySQL5.7安装教程:https://www.wsjj.top/archives/65
MySQL5.7配置主从环境教程:https://www.wsjj.top/archives/76
从库要开启只读模式!切记!

A.查看从库状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.140.14
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 738
               Relay_Log_File: slave_mysql-relay-bin.000006
                Relay_Log_Pos: 445
        Relay_Master_Log_File: master.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 738
              Relay_Log_Space: 698
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 14
                  Master_UUID: 15a4ce3b-e4da-11ed-801c-000c299a65ec
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 15a4ce3b-e4da-11ed-801c-000c299a65ec:1-3
            Executed_Gtid_Set: 15a4ce3b-e4da-11ed-801c-000c299a65ec:1-3,
45da0776-e4da-11ed-8218-000c29572e76:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

B.配置从库为只读

[root@slave_mysql ~]# vim /etc/my.cnf
#配置文件并不完整,仅展示修改和添加的部分
server_id=15
log_bin=master
gtid_mode=on
read_only=1		#开启只读
enforce_gtid_consistency=true

2.配置MyCat

MyCat部署教程:https://www.wsjj.top/archives/97
192.168.140.12部署MyCat
192.168.140.13部署MyCat

A.添加主机名解析

[root@mycat01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.140.12 mycat01
[root@mycat02 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.140.13 mycat02

B.安装jdk环境

如果提示找不到wget命令,请安装yum install -y wget

[root@mycat01 ~]# wget https://mirrors.huaweicloud.com/java/jdk/8u191-b12/jdk-8u191-linux-x64.tar.gz
[root@mycat02 ~]# wget https://mirrors.huaweicloud.com/java/jdk/8u191-b12/jdk-8u191-linux-x64.tar.gz
[root@mycat01 ~]# tar xf jdk-8u191-linux-x64.tar.gz -C /usr/local
[root@mycat02 ~]# tar xf jdk-8u191-linux-x64.tar.gz -C /usr/local
[root@mycat01 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_191
export PATH=$PATH:$JAVA_HOME/bin		#文件后面加上2行内容
#配置文件并不完整,请勿删改其他!

[root@mycat01 ~]# source /etc/profile
[root@mycat02 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_191
export PATH=$PATH:$JAVA_HOME/bin		#文件后面加上2行内容
#配置文件并不完整,请勿删改其他!

[root@mycat02 ~]# source /etc/profile

C.安装MyCat

[root@mycat01 ~]# tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat02 ~]# tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/

D.修改MyCat配置文件

[root@mycat01 ~]# vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<schema name="mysql" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'>
	</schema>
	<dataNode name="dn1" dataHost="localhost1" database="mysql" />
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="192.168.140.14:3306" user="admin"
				   password="WWW.1.com">
		</writeHost>
		<writeHost host="hostS1" url="192.168.140.15:3316" user="admin"
				   password="WWW.1.com" />
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>
</mycat:schema>
[root@mycat01 ~]# vim /usr/local/mycat/conf/server.xml 	#在文件底部做修改
<user name="wsjj">
                <property name="password">redhat</property>
                <property name="schemas">mysql</property>
        </user>
回到数据库创建MyCat登录的用户
mysql> grant all on *.* to 'admin'@'192.168.140.%' identified by 'WWW.1.com';

启动MyCat
如果没有netstat命令,请安装yum install -y net-tools

[root@mycat01 ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat01 ~]# netstat -tunlp | grep java
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      3630/java           
tcp6       0      0 :::41941                :::*                    LISTEN      3630/java           
tcp6       0      0 :::42367                :::*                    LISTEN      3630/java           
tcp6       0      0 :::1984                 :::*                    LISTEN      3630/java           
tcp6       0      0 :::8066                 :::*                    LISTEN      3630/java           
tcp6       0      0 :::9066                 :::*                    LISTEN      3630/java  
Windows测试连接

keep09

keep10

修改MyCat02配置文件
[root@mycat01 ~]# scp /usr/local/mycat/conf/schema.xml root@192.168.140.13:/usr/local/mycat/conf/
[root@mycat01 ~]# scp /usr/local/mycat/conf/server.xml root@192.168.140.13:/usr/local/mycat/conf/
启动MyCat02
[root@mycat02 ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat02 ~]# netstat -tunlp | grep java
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      13146/java          
tcp6       0      0 :::1984                 :::*                    LISTEN      13146/java          
tcp6       0      0 :::8066                 :::*                    LISTEN      13146/java          
tcp6       0      0 :::9066                 :::*                    LISTEN      13146/java          
tcp6       0      0 :::34506                :::*                    LISTEN      13146/java          
tcp6       0      0 :::34384                :::*                    LISTEN      13146/java
测试MyCat02

keep11

keep12

3.配置haproxy主备调度器

安装MySQL

MySQL安装教程:https://www.wsjj.top/archives/65

A.安装haproxy

分别在master01192.168.140.10
master02192.168.140.11安装
关于HAProxy教程:https://www.wsjj.top/archives/99

[root@master01 ~]# yum install -y haproxy
[root@master02 ~]# yum install -y haproxy

B.编辑haproxy配置文件

[root@master01 ~]# 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

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

defaults
    mode                    http
    log                     global
    option                  httplog
    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

listen mycat_service
       bind 0.0.0.0:8066
       mode tcp
       balance source
       server mycat01 192.168.140.12:8066 weight 3 check inter 2000 rise 1 fall 2
       server mycat02 192.168.140.13:8066 weight 3 check inter 2000 rise 1 fall 2

C.启动haproxy

[root@master01 ~]# systemctl enable --now haproxy
[root@master01 ~]# scp /etc/haproxy/haproxy.cfg root@192.168.140.11:/etc/haproxy/
[root@master02 ~]# systemctl enable --now haproxy

D.本地测试连接

keep13

keep14

keep15

4.配置keepalived

A.安装keepalived

[root@master01 ~]# yum install -y keepalived
[root@master02 ~]# yum install -y keepalived

B.编写脚本

[root@master01 ~]# vim /root/check_mycat.sh
#! /bin/bash

netstat -tunlp | grep 8066 &> /dev/null

if [ $? -ne 0 ]; then
    systemctl stop keeplived
fi

[root@master01 ~]# chmod 777 /root/check_mycat.sh
[root@master02 ~]# vim /root/check_mycat.sh
#! /bin/bash

netstat -tunlp | grep 8066 &> /dev/null

if [ $? -ne 0 ]; then
    systemctl stop keeplived
fi

[root@master02 ~]# chmod 777 /root/check_mycat.sh

C.修改配置文件

[root@master01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id master_mycat
}
vrrp_script check_mycat {
    script "/root/check_mycat.sh"
    interval 1
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mycat
    }
}
[root@master02 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id slave_mycat
}
vrrp_script check_mycat {
    script "/root/check_mycat.sh"
    interval 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
    track_script {
        check_mycat
    }
}

D.启动服务

[root@master01 ~]# systemctl start keepalived.service
[root@master02 ~]# systemctl start keepalived.service

E.连接测试

keep16

keep17

F.模拟主调度上的服务挂掉

[root@master01 ~]# systemctl stop haproxy.service

可以看到现在VIP已经跑到了备用调度器上

[root@master02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:37:97:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.11/24 brd 192.168.140.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.140.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe37:97c7/64 scope link 
       valid_lft forever preferred_lft forever

再次连接测试

keep16

keep17