一,部署前准备
1.1软件版本
1 操作系统: centOS-7.0(ubuntu 16.4)
2 jdk :1.8
3 haproxy: 1.2.27
4 keepalived: 1.3.9 (keepalived-1.3.9)
5 mycat: 1.6
6 mysql: 5.7
1.2 服务器分布
192.168.1.100 (vip 在192.168.1.1的主机上创建)
192.168.1.1 server001 (mycat,haproxy,keepalive) master
192.168.1.2 server002 (mycat,haproxy,keeaplive) slave
192.168.1.3 server003 (mysql master)
192.168.1.4 server004 (mysql slave)
1.3 软件安装目录总览
haproxy:/opt/haproxy
mycat: /opt/mycat
keepalived: /opt/keepalived
二,Mycat的监听服务
使用部署好的mycat,编写mycat的监听服务(xinetd),和mycat在在同一台服务器
2.1 安装xinetd(没有安装的话)
## centos 下
yum install xinetd -y
# ubuntu 下
sudo apt-get install xinetd
2.2 检查xinetd文件安装目录是否完整性
## 检查/etc/xinetd.conf 文件的末尾是否有:includedir /etc/xinetd.d
vim xinetd.conf
添加末尾:includedir /etc/xinetd.d
## 检查 /etc/xinetd.d文件夹是否存在,不存在也加上
mkdir /etc/xinetd.d
2.3 创建 mycat存活监听脚本
#1,创建脚本(目录:/user/local/bin/)
touch mycat_status
---------------------------------
# 2,编写脚本
mycat=`/opt/mycat/bin/mycat status | grep 'not running' | wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -e "HTTP/1.1 200 OK\r\n"
else
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
fi
注释: /opt/mycat/bin/mycat 为mycat 启动脚本的目录
2.4 在xinetd中创建文件
# 1,创建配置文件
cd /etc/xinetd.d
touch mycat_status
## 2.编辑配置文件
service mycat_status
{
flags = REUSE
socket_type = stream
port = 48700 #绑定端口
wait = no
user = root
server =/usr/local/bin/mycat_status #脚本目录
log_on_failure += USERID
disable = no
}
2.5 写进系统服务(这是采用tcp,之后haproxy得使用tcp的方式来监听)
## 1.打开系统服务的配置文件
vim /etc/services
## 2.编辑service
在末尾加入: mycat_status 48700/tcp # mycat_status
## 3.重启xinetd服务
service xinetd restart
2.6 验证mycat_status服务是否开启
netstat -antup|grep 48700
## 如果成功会展示如下内容
root@localhost log]# netstat -antup|grep 48700
root@localhost log]# netstat -antup|grep 48700
tcp 0 0 :::48700 :::* LISTEN 12609/xinetd
三,安装配置Haproxy
3.1 安装haproxy
## 1 下载并解压
tar haproxy-1.4.27.tar.gz
## 2 编译
cd haproxy-1.4.27
make TARGET=linux2628 ARCH=x86_64 PREFIX=/opt/haproxy
make install PREFIX=/opt/haproxy
-------------------------------------------------------------
##$ 参数说明
#TARGET 内核版本 使用uname -r查看内核
#如:2.6.18-371.el5,此时该参数就为 TARGET=linux26;kernel 大于2.6.28的用:TARGET=linux2628
TARGET=linux2628 #内核版本
ARCH=x86_64 ##系统位数
PREFIX= ## 安装路径
3.2 配置文件
### 创建配置文件
cd /opt/haproxy
touch haproxy.cfg
-------------------------------------------------------------
global
log 127.0.0.1 local0 ##记日志的功能
maxconn 4096
chroot /opt/haproxy
user root
group root
daemon
defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen admin_status 192.168.1.100:48800 ## haproxy的监视页面
stats uri /admin-status
stats auth admin:admin
stats admin if TRUE
stats refresh 5s ##刷新时间
mode http
option httplog
listen all_mycat_service 192.168.1.100:8096
mode tcp ##使用tcp链接
option tcplog ##记录tcp链接日志
#option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www (官方文档里面的坑,这是针对http链接方式的验证方式)
balance roundrobin
##rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
server mycat_1 192.168.1.1:8066 check port 48700 inter 5s rise 2 fall 3
server mycat_2 192.168.1.2:8066 check port 48700 inter 5s rise 2 fall 3
srvtimeout 20000 (断开服务链接的超时时间)
listen all_mycat_admin 192.168.1.100:8097
mode tcp ##使用tcp链接
option tcplog
#option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www (官方文档里面的坑,这是针对http链接方式的验证方式)
balance roundrobin
server mycat_1 192.168.1.1:9066 check port 48700 inter 5s rise 2 fall 3
server mycat_2 192.168.1.2:9066 check port 48700 inter 5s rise 2 fall 3
srvtimeout 20000 (断开服务链接的超时时间)
3.3 Haproxy启动和停止脚本
1.启动脚本
#!/bin/sh
nohup /opt/haproxy/sbin/haproxy -f /opt/haproxy/haproxy.cfg &
2.停止脚本
#!/bin/sh
ps -ef | grep opt/haproxy/sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9
3.4 Haproxy的日志记录
haproxy是不记录日志的,为了记录日志还需要配置syslog模块,在linux下是rsyslogd服务,yum –y install rsyslog先安装
#cd /etc/rsyslog.d/
如果没有这个目录,新建
#cd /etc
#mkdir rsyslog.d
#cd /etc/rsyslog.d/
#touch haproxy.conf
#vi /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log
--------------------------------------------------------
#vi /etc/rsyslog.conf
1、在#### RULES ####上面一行的地方加入以下内容:
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
#### RULES ####
2、在local7.* /var/log/boot.log的下面加入以下内容(增加后的效果如下):
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/haproxy.log
3.5Haproxy的启动
1 启动异常
启动haproxy异常情况
如果报以下错误:
[root@localhost bin]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
[ALERT] 183/115915 (12890) : Starting proxy admin_status: cannot bind socket
[ALERT] 183/115915 (12890) : Starting proxy allmycat_service: cannot bind socket
[ALERT] 183/115915 (12890) : Starting proxy allmycat_admin: cannot bind socket
#原因为:该机器没有抢占到vip,得先开启keepalived 才行
2 访问Haproxy管理页面
#在本案例中配置的是192.168.1.100:48800 上
访问: http://192.168.1.100:48800/admin-status
四,Keepalived的安装
4.1 keepalived 的安装
tar zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure--prefix=/usr/local/keepalived
make
make install
cp /opt/keepalived/sbin/keepalived /usr/sbin/
cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /opt/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
mkdir /etc/keepalived
cd /etc/keepalived/
cp /opt/keepalived/etc/keepalived/keepalived.conf/etc/keepalived
mkdir -p /opt/keepalived/var/log
4.2 keepalived的配置
### 建立脚本存放的目录
mkdir /etc/keepalived/scripts
cd /etc/keepalived/scripts
-------------------------------------------------
### 设置配置文件 keepalived.conf (master 和slave 是一致的,只是priority不同)
vrrp_script chk_http_port {
script "/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #192.168.1.1上改为Master
interface eno1 #对外提供服务的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 150 #数值愈大,优先级越高,172.17.1.2上改为120
advert_int 1 #同步通知间隔
authentication {
#包含验证类型和验证密码。类型主要有PASS、AH两种,通常使用的类型为PASS,据说AH使用时有问题
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #调用脚本check_haproxy.sh检查haproxy是否存活
}
virtual_ipaddress { #vip地址,这个ip必须与我们在lvs客户端设定的vip相一致
192.168.1.100 dev eno1 scope globa
}
notify_master /etc/keepalived/scripts/haproxy_master.sh
notify_backup /etc/keepalived/scripts/haproxy_backup.sh
notify_fault /etc/keepalived/scripts/haproxy_fault.sh
notify_stop /etc/keepalived/scripts/haproxy_stop.sh
}
4.3 创建keepalied所需的脚本
4.3.1 check_haproxy.sh
#!/bin/bash
STARTHAPROXY="/opt/haproxy/sbin/haproxy -f /opt/haproxy/haproxy.cfg"
STOPKEEPALIVED="/etc/init.d/keepalived stop"
LOGFILE="/opt/keepalived/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
sleep5
fi
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
exit 0
else
exit 1
fi
4.3.2 haproxy_master.sh
#!/bin/bash
STARTHAPROXY=`/opt/haproxy/sbin/haproxy -f /opt/haproxy/haproxy.cfg`
STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9`
LOGFILE="/opt/keepalived/var/log/keepalived-haproxy-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "stop haproxy...." >> $LOGFILE 2>&1
$STOPHAPROXY >> $LOGFILE 2>&1
echo "start haproxy...." >> $LOGFILE 2>&1
$STARTHAPROXY >> $LOGFILE 2>&1
echo "haproxy stared ..." >> $LOGFILE
4.3.3 haproxy_backup.sh
#!/bin/bash
STARTHAPROXY=`/opt/haproxy/sbin/haproxy -f /opt/haproxy/haproxy.cfg`
STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9`
LOGFILE="/opt/keepalived/var/log/keepalived-haproxy-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being backup...." >> $LOGFILE 2>&1
echo "stop haproxy...." >> $LOGFILE 2>&1
$STOPHAPROXY >> $LOGFILE 2>&1
echo "start haproxy...." >> $LOGFILE 2>&1
$STARTHAPROXY >> $LOGFILE 2>&1
echo "haproxy stared ..." >> $LOGFILE
4.3.4 haproxy_fault.sh
#!/bin/bash
LOGFILE=/opt/keepalived/var/log/keepalived-haproxy-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
4.3.5 haproxy_stop.sh
#!/bin/bash
LOGFILE=opt/keepalived/var/log/keepalived-haproxy-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
5.启动Keepalived服务
service keepalived start
五, 知识点补充
5.1 创建虚拟vip
# 1 查看但前活动网卡 eth0
ifconfig -a
# 2 添加vip
ifconfig eno1:0 192.168.1.100 netmask 255.255.255.0 up
# 3 设置开机自启
编辑:/etc/rc.local