1.建立ZK集群
#在ZK目录创建data目录
mkdir data
#创建ID身份文件
echo 1>data/myid
#配置集群信息
cp zoo_sample.conf zoo.cfg
vim zoo.cfg
server.0=47.107.149.134:2888:3888
server.1=118.25.89.150:2888:3888
server.2=129.204.66.135:2888:3888
dataDir=/usr/local/zookpeer/zookeeper-3.4.9/data
#启动ZK
./zkServer.sh start
2.初始化Mycat配置到ZK集群,配置Mycat支持ZK启动
#修改mycat从ZK配置启动
vim /conf/myid.properties
#通过ZK读取配置文件
loadZk=true
#ZK集群地址
zkURL=129.204.66.135:2181,118.25.89.150:2181,47.107.149.134:2181
#ZK的ID
clusterId=mycat-cluster-1
#id唯一
myid=mycat_02
#mycat的个数
clusterSize=2
clusterNodes=mycat_01,mycat_02
#server booster ; booster install on db same server,will reset all minCon to 2
type=server
boosterDataHosts=dataHost1
#启动mycat 完成zk配置启动mycat
/bin/mycat start
#init_zk_data.sh是复制zkConf文件初始化
cp rule.xml schema.xml server.xml sequence_conf.properties zkconf/
#初始化配置
./init_zk_data.sh
#启动zk
./zkCli.sh
#查看ZK信息
ls /mycat/mycat-cluster-1
#获取位置信息
get /mycat/mycat-cluster-1/schema
#退出zk命令
quit
#Ha安装
yum install haproxy -y
#Ha默认配置文件地址
vim /etc/haproxy/haproxy.cfg
#启动HA
haproxy -f/etc/haproxy/haproxy.cfg
haproxy.cfg的配置
global
chroot /var/lib/haproxy ##执行目录
pidfile /var/run/haproxy.pid
maxconn 4000 ##最大连接数
user haproxy ##用户信息
group haproxy
daemon ## 通过后台运用
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option abortonclose
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
balance roundrobin
log 127.0.0.1 local0 info
listen admin_status ##监控HA状态
bind *:48800 ##VIP 127.168.1.10::48880
stats uri /admin-status ##统计页面
stats auth admin:admin ##通过该账号监听HA状态
listen allmycat_service
bind *:8096 ##转发到mycat的应用端口,即mycat的服务端口 127.168.1.10::8096
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www ##标准HTTP请求
balance roundrobin ##轮训负载均衡
server mycat_132 10.102.13.2:8066 check port 48700 inter 5s rise 2 fall 3 ##通过 48700端口监听 mycat_132 服务状态 5秒检查一次,失败后检测3次
server mycat_135 10.102.13.5:8066 check port 48700 inter 5s rise 2 fall 3
listen allmycat_admin ##监控8097端口服务
bind *:8097 ##转发到mycat的管理端口,及mycat的管理控制台端口
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www ##标准HTTP请求
balance roundrobin
server mycat_132 10.102.13.2:9066 check port 48700 inter 5s rise 2 fall 3
server mycat_135 10.102.13.5:9066 check port 48700 inter 5s rise 2 fall 3
#是一个网络守护进程服务,简单实现一个网络服务,达到启动端口目的
yum install xinted -y
#新建mycatchk信息
vim /etc/xinetd.d/mycatchk
#查看48700HA监听的mycat状态 service增加myucatchk服务
#添加 mycatchk 48700/tcp #mycatchk
vim /etc/services
#启动服务
service xinted restart
#查看端口信息
netstat -nltp
#新建检查mycat状态脚本
vim /usr/local/bin/mycat_status
#设置脚本执行权限
chmod a+x /usr/local/bin/mycat_status 对mycat进行访问
mycat_status配置文件
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# This script checks if a mycat server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
#
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`/usr/local/mycat/bin/mycat status |grep 'not running'| wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -en "HTTP/1.1 200 OK\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 40\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is synced.\r\n"
exit 0
else
/bin/echo -en "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 44\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is not synced.\r\n"
exit 1
fi
mycatchk配置文件
# default: on
# description: monitor for mycat
service mycatchk
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = root
server =/usr/local/bin/mycat_status
log_on_failure += USERID
disable = no
per_source = UNLIMITED
}
#安装kp
yum install keepalived -y
cd /etc/keepalived
#新建配置文件
vim /etc/keepalived/keepalived.conf
#新建HA状态监控脚本
vim /etc/keepalived/check_haproxy.sh
chmod a+x check_haproxy.sh
/etc/init.d/keepalived start
#设置虚ip kp可能需要
ifconfig eth0:1 192.168.1.22 broadcast 192.168.1.2 netmask 255.255.255.0 up
keepalived.conf配置文件
! Configuration Fileforkeepalived
##定义一个监控脚本
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2 ##间隔时间
weight 2
}
vrrp_instance VI_1 {
state MASTER ##SLAVE 主备状态
interface eth0 ##网卡
virtual_router_id 51 ##多个节点是一样的,同一组
priority 150 ##优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
10.102.13.248 dev eth0 scope global #haproxy的VIP
}
}
check_haproxy.sh配置文件
#!/bin/bash
STARTHAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg"
#STOPKEEPALIVED="/etc/init.d/keepalived stop" centOS6
STOPKEEPALIVED="/usr/bin/systemctl stop keepalived"
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "[check_haproxy status]" >> $LOGFILE
A=`ps -C haproxy --no-header |wc -l`
echo "[check_haproxy status]" >> $LOGFILE
date >> $LOGFILE
if [ $A -eq 0 ];then
echo $STARTHAPROXY >> $LOGFILE
$STARTHAPROXY >> $LOGFILE 2>&1
sleep 5
fi
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
exit 0
else
exit 1
fi
配置各个节点的Mysql主从复制
配置Mycat对后端DB进行读写分离
滚动应用Mycat配置
<dataHost balance="3" maxCon="1000" minCon="10" name="mysqlM1" writeType="0" switchType="1" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="129.204.66.6:3306" password="qqqwww" user="im_mycat"/>
<readHost host="hostS1" url="47.107.218.8:3306" password="qqqwww" user="im_mycat"/>
</writeHost>
<writeHost host="hostS1" url="47.107.218.8:3306" password="qqqwww" user="im_mycat"/>
</dataHost>
注意: shell脚本执行后出现错误提示" /bin/bash^M: bad interpreter: No such file or dire"的解决办法
方法1.sed -i 's/\r$//' filename #flename即shell脚本文件名
方法2.apt install dos2unix ;dos2unix xxx.sh;
如果文章对您有帮助,欢迎移至上方按钮打赏,非常感谢你的支持!
全部评论