mycat + haproxy + keepalived build a high-availability load balancing cluster mysql

 

keepalived-2.0.5
haproxy-1.8.12
Mycat-server-1.6.5

Host 1: ip192.168.1.165 installation keepalived, haproxy, Mycat
host 2: ip192.168.1.111 installation keepalived, haproxy, Mycat
VIP: 192.168.1.180
keepalived haproxy be installed on the same machine

Operation of understanding: After the host will seize vip 1 for the server and keepalived on 2 host, to seize vip, access to the host can be accessed via the original ip (ip192.168.1.165 or 192.168.1.111), or directly through VIP (192.168.1.180) access, when one of them is down, vip drift to another machine, as we will haproxy access requests according to the distribution configuration 192.168.1.180 load balancing on two myCat, and haproxy detects mycat is alive.

A, haproxy installation

1, the installation haproxy

Domestic Mirror Address: http://pkgs.fedoraproject.org/repo/pkgs/haproxy/
core edition

uname -r
 
image.png

Select the kernel version compiled in accordance with the parameters:


 
image.png

Here is a selection linux2628 3.10 kernel, installation haproxy

useradd -r haproxy
wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.12.tar.gz/sha512/2b782a54988cc88d1af0e5f011af062910e8fac28eab13db7e05a58d0d23961f827da47e3871e8d081f5a2d222588480d81dec2e9f14ec9f54a1c3cb5bf3d56a/haproxy-1.8.12.tar.gz
tar -zxvf haproxy-1.8.12.tar.gz
cd  haproxy-1.8.12
make TARGET=linux2628 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy chown -R haproxy.haproxy * 

After successful installation, check the version


 
image.png
2, the configuration haproxy
cd /usr/local/haproxy
touch haproxy.cfg

vim /usr/local/haproxy/haproxy.cfg
write about the content:

global
#设置日志
    log 127.0.0.1   local0
    chroot /usr/local/haproxy
#用户与用户组
    user haproxy
    group haproxy
#定义每个haproxy进程的最大连接数 ,由于每个连接包括一个客户端和一个服务器端,所以单个进程的TCP会话最大数目将是该值的两倍。 maxconn 4096 # 以守护进程的方式运行 daemon defaults log global #日志中不记录空连接 option dontlognull # 定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端服务器标记为不可用 retries 3 option redispatch # 设置成功连接到一台服务器的最长等待时间,默认单位是毫秒 timeout connect 5000 # 设置连接客户端发送数据时的成功连接最长等待时间,默认单位是毫秒 timeout client 50000 # 设置服务器端回应客户度数据发送的最长等待时间,默认单位是毫秒 timeout server 50000 #统计页面 listen admin_stats bind 192.168.1.180:48800 mode http #采用http日志格式 option httplog #统计页面自动刷新时间 stats refresh 30s #统计页面url stats uri /admin_stats #统计页面密码框上提示文本 stats realm Haproxy Manager #统计页面用户名和密码设置 stats auth admin:admin #隐藏统计页面上HAProxy的版本信息 stats hide-version listen mycat_service # 绑定192.168.1.180:8067端口访问mycat8066端口 bind 192.168.1.180:8067 # 定义为tcp模式 mode tcp #采用http日志格式 option tcplog # 开启对后端服务器的健康检测 option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www # 设置haproxy的调度算法 balance roundrobin #根据调度分配到真实的后台地址,参数解释:port 48700:检测端口48700, inter 5s:5秒检测一次,rise 2:检测成功2次表示服务器可用,fall 3:检测失败3次后表示服务器不可用 server mycat_111 192.168.1.111:8066 check port 48700 inter 5s rise 2 fall 3 server mycat_165 192.168.1.165:8066 check port 48700 inter 5s rise 2 fall 3 #设置服务器端回应客户度数据发送的最长等待时间,默认单位是毫秒 timeout server 20000 listen mycat_admin #绑定192.168.1.180:9067端口访问mycat9066端口 bind 192.168.1.180:9067 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin server mycat_111 192.168.1.111:9066 cookie 1 check port 48700 inter 5s rise 2 fall 3 server mycat_165 192.168.1.165:9066 cookie 2 check port 48700 inter 5s rise 2 fall 3 timeout server 20000 

Start and stop commands haproxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg #启动
killall haproxy #停止
3, the configuration logging function haproxy

Haproxy default is not logging, logging to syslog also need to configure the module under linux is rsyslogd service, you need to install rsyslog

yum –y install rsyslog
mkdir /etc/rsyslog.d
cd /etc/rsyslog.d/
touch haproxy.conf

vim haproxy.conf
write about the content

$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log

vim /etc/rsyslog.conf
modify two: 1 was added the following in the top line #### RULES ####

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
 
image.png

Second in local7. * Increase /var/log/boot.log below

local0.*                                                /var/log/haproxy.log
 
image.png

Restart rsyslog service

systemctl restart rsyslog.service

The rsyslog added automatically start the service

systemctl enable rsyslog.service

Second, configure the listener mycat is alive

On Mycat server1 Mycat server2 you need to add detection script port 48700, for which the need to use xinetd, xinetd linux system for the basic services, increase the port mapping configuration script file in the following directory xinetd.

yum install xinetd -y
cd /etc/xinetd.d
touch mycat_status

vim /etc/xinetd.d/mycat_status
writes the following

service mycat_status
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = nobody
server =/usr/local/bin/mycat_status
log_on_failure += USERID
disable = no
}

Create a service xinetd start script
vim / usr / local / bin / 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 -e "HTTP/1.1 200 OK\r\n" else /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n" fi 

Modify the script file permissions

chmod 777 /usr/local/bin/mycat_status
chmod 777 /etc/xinetd.d/mycat_status

The startup script to join the service
vim / etc / services
added at the end

mycat_status    48700/tcp               # mycat_status

Restart xinetd service

systemctl restart xinetd.service

Mycat_status verify whether the service starts successfully

netstat -antup|grep 48700
 
image.png

Note: /etc/xinetd.d/mycat_status in the service mycat_status (service name) and port = 48700 (the port) to the / etc / services in the same

Three, keepalived installation

1, mounted reliance
yum install -y openssl-devel openssl libnl libnl-devel libnfnetlink-devel
2, keepalived installation
wget http://www.keepalived.org/software/keepalived-2.0.5.tar.gz
tar -zxvf keepalived-2.0.5.tar.gz
cd keepalived-2.0.5
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp ./keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cd /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived mkdir -p /usr/local/keepalived/var/log 
2, keepalived Configuration

a, View Card

ifconfig
 
image.png

So here I enp0s3 card is configured in the interface is enp0s3, Host 1 and Host 2 is basically the same configuration, different state and priority. b, configure the host keepalived 2
vim /etc/keepalived/keepalived.conf

! Configuration Fileforkeepalived
#VRRP脚本
vrrp_script chk_http_port {
#执行的脚本
script"/etc/keepalived/scripts/check_haproxy.sh"
 #脚本执行间隔
interval 2 #脚本结果导致的优先级变更:2表示优先级+10;-2则表示优先级-2 weight 2 } #vrrp实例 vrrp_instance VI_1 { state BACKUP #本机网卡 interface enp0s3 ##VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组 virtual_router_id 51 #优先级,数值愈大,优先级越高 priority 120 #检查间隔,默认1秒 advert_int 1 #设置认证 authentication { #认证方式 auth_type PASS #认证密码 auth_pass 1111 } track_script { #调用脚本check_haproxy.sh检查haproxy是否存活 chk_http_port } #虚拟IP地址池,可以有多个IP,每个IP占一行 virtual_ipaddress { 192.168.1.180 dev enp0s3 scope global } #指定当切换到master时,执行的脚本 notify_master /etc/keepalived/scripts/haproxy_master.sh #指定当切换到backup时,执行的脚本 notify_backup /etc/keepalived/scripts/haproxy_backup.sh #故障时执行的脚本 notify_fault /etc/keepalived/scripts/haproxy_fault.sh notify_stop /etc/keepalived/scripts/haproxy_stop.sh 

c, the configuration of the host keepalived 1

! Configuration Fileforkeepalived
vrrp_script chk_http_port {
script"/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 192.168.1.180 dev enp0s3 scope global } 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 } 
3, create a profile and scripts
mkdir /etc/keepalived/scripts

a, create a script to check whether haproxy survival
vim /etc/keepalived/scripts/check_haproxy.sh

#!/bin/bash
STARTHAPROXY="/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg"
STOPKEEPALIVED="/etc/init.d/keepalived stop"
LOGFILE="/usr/local/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 #如果没有haproxy进程存在,就启动haproxy,停止keepalived,并写入日志 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 

b, create and switch to MASTER BACKUP script
vim /etc/keepalived/scripts/haproxy_master.sh
vim /etc/keepalived/scripts/haproxy_backup.sh
writes the following in both files

#!/bin/bash
STARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg`
STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep | awk '{print $2}'| xargs kill -s 9` LOGFILE="/usr/local/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 

c, script execution when creating fault
vim /etc/keepalived/scripts/haproxy_fault.sh

#!/bin/bash
LOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.log
echo "[fault]" >> $LOGFILE date >> $LOGFILE 

d, to create a stop vrrp script
vim /etc/keepalived/scripts/haproxy_stop.sh

#!/bin/bash
LOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.log
echo "[stop]" >> $LOGFILE date >> $LOGFILE 
4, giving the script executable permissions
chmod 777 /etc/keepalived/scripts/*
5, start keepalived
systemctl start keepalived.service
6, by adding the boot
systemctl enable keepalived.service

Fourth, the test

Enter the browser http://192.168.1.180:48800/admin_stats

 
image.png

View 8067 and 9067 ports are listening


 
image.png

Connection port 8067
mysql -uroot -p123456 -h192.168.1.180 -P806


 
image.png

Can be connected, this will set up complete


Author: riding an elephant to work
link: https: //www.jianshu.com/p/c7c178e29af9
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/xiaohanlin/p/11057408.html