|
As in the production environment uses mysqlcluster, the need to achieve high availability load balancing, there is provided keepalived + haproxy to achieve.
keepalived main function is to achieve real machine failover and fault isolation between the load balancer can be exchanged at 3,4,5 layer through VRRPv2 (Virtual Router Redundancy Protocol) stack to achieve.
Layer3:. Keepalived regularly to the server farm server to send packets of an ICP (as we usually use Ping program), if you find the IP address of a service is not activated, Keepalived will report this server fails, and it removed from the server farm, a typical example of this situation is that a server has been shut down illegal. Layer3 way server based on IP address is valid as a server working properly or not standards.
Layer4: mainly in the state of TCP port to determine the server is working properly or not. Services such as web server port is usually 80, if Keepalived detected port 80 is not started, Keepalived these servers will be removed from the server farm.
Layer5: on the network bandwidth should be larger. Keepalived according to the normal operation of the server program set by the user to check if the setting and the user does not match, then the server will Keepalived removed from the server farm.
Recommended reading:
Haproxy + Keepalived build a high-availability load balancing cluster http://www.linuxidc.com/Linux/2013-09/89732.htm Weblogic
Keepalived + HAProxy configure high-availability load balancing http://www.linuxidc.com/Linux/2012-03/56748.htm
CentOS 6.3 under Haproxy + Keepalived + Apache configuration notes http://www.linuxidc.com/Linux/2013-06/85598.htm
Haproxy + KeepAlived achieve WEB cluster on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm
Haproxy + Keepalived build high-availability load balancing http://www.linuxidc.com/Linux/2012-03/55880.htm
Software Design
A single process will start after keepalived
8352? Ss 0:00 / usr / sbin / keepalived
8353? S 0:00 _ / usr / sbin / keepalived
8356? S 0:01 _ / usr / sbin / keepalived
The parent process: memory management, process management, and more child
Child: VRRP child process
Child: Healthchecking child process
Examples
2 mysqlcluster 10.1.6.203 master 10.1.6.205 backup
vip 10.1.6.173
Purpose of the visit 10.1.6.173 3366 ports are polled and forwarded to 10.1.6.203 3306 10.1.6.205 3306 by haproxy
root@10.1.6.203: ~ # apt-get install keepalived
root@10.1.6.203: ~ # cat /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
script "killall -0 haproxy" # verify the pid existance
interval 2 # check every 2 seconds
weight -2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
interface eth1 # interface to monitor
state MASTER
virtual_router_id 51 # Assign one ID for this route
priority 101 # 101 on master, 100 on backup
nopreempt
debug
virtual_ipaddress {
10.1.6.173
}
track_script {
chk_haproxy
}
notify_master /etc/keepalived/scripts/start_haproxy.sh # indicates that the script when switching to a master state, to be executed
Script execution failure notify_fault /etc/keepalived/scripts/stop_keepalived.sh #
notify_stop /etc/keepalived/scripts/stop_haproxy.sh # keepalived stop running before running notify_stop specified script}
VRRPD configuration includes three categories:
VRRP sync group (synchroization group)
Examples VRRP (VRRP Instance)
VRRP script
As used herein, the VRRP instance, VRRP script
Note Configuration options:
stat: Specifies the instance (Initial) initial state, that is in configured, the initial state of this server is specified here, but not specified here, still have to compete to be determined by the priority in here if is set to master, but should his priority is less than the other one, then this when sending notices will send its own priorities, find another one as good as their high priority, then he will seize on the back for the master
interface: the bound NIC, because you must be added to configure virtual IP when on the existing card
priority 101: The node set priority high priority for the master
debug: debug level
nopreempt: set the preemption
vrrp_script chk_haproxy {
script "killall -0 haproxy" # verify the pid existance
interval 2 # check every 2 seconds script execution interval
Priority changing weight -2 # add 2 points of prio if OK result of the script: 2 indicates the priority of +2; -2 -2 indicates the priority
}
Then in the example (vrrp_instance) inside references, somewhat similar to a script function references inside the same: to define, after a reference to the function name
track_script {
chk_haproxy
}
Note: VRRP script (vrrp_script) and VRRP instance (vrrp_instance) belong to the same level
root@10.1.6.203: scripts # cat start_haproxy.sh
#! / Bin / bash
sleep 5
get = `ip addr | grep 10.1.6.173 | wc -l`
echo $ get >> /etc/keepalived/scripts/start_ha.log
if [$ get -eq 1]
then
echo "` date +% c` success to get vip ">> /etc/keepalived/scripts/start_ha.log
/ Usr / local / sbin / haproxy -f /etc/haproxy/haproxy.cfg
else
echo "` date +% c` can not get vip ">> /etc/keepalived/scripts/start_ha.log
fi
root@10.1.6.203: scripts # cat stop_keepalived.sh
#! / Bin / bash
pid = `pidof keepalived`
if [$ pid == ""]
then
echo "` date +% c` no keepalived process id ">> /etc/keepalived/scripts/stop_keep.log
else
echo "` date +% c` will stop keepalived ">> /etc/keepalived/scripts/stop_keep.log
/etc/init.d/keepalived stop
fi
/etc/init.d/keepalived stop
root@10.1.6.203: scripts # cat stop_haproxy.sh
#! / Bin / bash
pid = `pidof haproxy`
echo "` date +% c` stop haproxy ">> /etc/keepalived/scripts/stop_ha.log
kill -9 $ pid
Similarly configured 10.1.6.205
root@10.1.6.205: ~ # cat /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
script "killall -0 haproxy" # verify the pid existance
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}
vrrp_instance VI_1 {
interface eth1 # interface to monitor
state BACKUP
virtual_router_id 51 # Assign one ID for this route
priority 100 # 101 on master, 100 on backup
virtual_ipaddress {
10.1.6.173
}
track_script {
chk_haproxy
}
notify_master /etc/keepalived/scripts/start_haproxy.sh
notify_fault /etc/keepalived/scripts/stop_keepalived.sh
notify_stop /etc/keepalived/scripts/stop_haproxy.sh
}
Here again introduced haproxy
HAProxy is a based on TCP (fourth layer) and HTTP (seventh layer) application of agent software, it can also be used as a load balancer can support thousands of concurrent connections, while the server can be protected without being exposed to the network through port mapping. it also comes with the monitor server status page.
Installation haproxy
wget -O / tmp / haproxy-1.4.22.tar.gz http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
tar xvfz /tmp/haproxy-1.4.22.tar.gz -C / tmp /
cd /tmp/haproxy-1.4.22
make TARGET = linux26
make install
haproxy required for each server health checks mysqlcluster
1. In the two hosts are configured haproxy.cfg
root@10.1.6.203: scripts # cat /etc/haproxy/haproxy.cfg
global
maxconn 51200 # default maximum number of connections & nbsp;
#uid 99
#gid 99
After running the table in the form of daemon # haproxy
#quiet
nbproc 1 # number of processes (multiple processes may be provided to improve performance) & nbsp;
pidfile /etc/haproxy/haproxy.pid #haproxy the pid storage path, the user starts the process must have permission to access this document & nbsp;
defaults
Category mode tcp # handled by & nbsp; (# 7 Layer & nbsp; http; 4 layer tcp & nbsp; & nbsp;) & nbsp;
After the option redispatch #serverId corresponding server hang, forcing directed to other healthy servers & nbsp;
option abortonclose # when high server load time, automatic off the end of the current queue processing relatively long connection & nbsp;
timeout connect 5000s & nbsp; # connection timeout
timeout client 50000s # client timeout
server timeout timeout server 50000s #
log 127.0.0.1 local0 # error logging
balance roundrobin & nbsp; # default load balancing mode, polling mode & nbsp;
listen proxy
bind 10.1.6.173:3366 # listening port & nbsp;
mode tcp #http seven-layer model
option httpchk # heartbeat file
server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3 # server definitions, check & nbsp; inter & nbsp; 12000 is to detect heart rate rise & nbsp; 3 is three times correct that the server is available, & nbsp; fall & nbsp; 3 is 3 times failure think server is unavailable, weight representatives weight & nbsp;
server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
listen haproxy_stats
mode http
bind 10.1.6.173:8888
option httplog
stats refresh 5s
stats uri / status # health testing site URL, for detecting whether HAProxy management site can be used, normally 200 return, abnormal return 503 & nbsp;
stats realm Haproxy Manager
stats auth admin: p @ a1SZs24 # account password
root@10.1.6.205: ~ $ cat /etc/haproxy/haproxy.cfg
global
maxconn 51200
#uid 99
#gid 99
daemon
#quiet
nbproc 1
pidfile /etc/haproxy/haproxy.pid
defaults
mode tcp
option redispatch
option abortonclose
timeout connect 5000s
timeout client 50000s
timeout server 50000s
log 127.0.0.1 local0
balance roundrobin
listen proxy
bind 10.1.6.173:3366
mode tcp
option httpchk
server db1 10.1.6.203:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
server db2 10.1.6.205:3306 weight 1 check port 9222 inter 12000 rise 3 fall 3
listen haproxy_stats
mode http
bind 10.1.6.173:8888
option httplog
stats refresh 5s
stats uri / status
stats realm Haproxy Manager
stats auth admin: p @ a1SZs24
2. Install xinetd
root@10.1.6.203: ~ # apt-get install xinetd
3. Add xinetd service script and the port number at each node mysqlchk
root@10.1.6.203: ~ # vim /etc/xinetd.d/mysqlchk
# Default: on
# Description: mysqlchk
service mysqlchk # define needs servive
{
flags = REUSE
socket_type = stream
port = 9222
wait = no
user = nobody
server = / opt / mysqlchk
log_on_failure + = USERID
disable = no
per_source = UNLIMITED
bind = 10.1.6.173
}
root@10.1.6.203: ~ # vim / etc / services
mysqlchk 9222 / tcp # mysqlchk
4. Write a script mysqlchk Monitoring Service
root@10.1.6.203: ~ # ls -l / opt / mysqlchk
-rwxr - r-- 1 nobody root 1994 2013-09-17 11:27 / opt / mysqlchk
root@10.1.6.203: ~ # cat / opt / mysqlchk
#! / Bin / bash
#
# This script checks if a mysql server is healthy running on localhost. It will
# Return:
# "HTTP / 1.x 200 OK r" (if mysql is running smoothly)
# - OR -
# "HTTP / 1.x 500 Internal Server Error r" (else)
#
# The purpose of this script is make haproxy capable of monitoring mysql properly
#
MYSQL_HOST = "localhost"
MYSQL_SOCKET = "/ var / run / mysqld / mysqld.sock"
MYSQL_USERNAME = "mysqlchkusr"
MYSQL_PASSWORD = "secret"
MYSQL_OPTS = "- N -q -A"
TMP_FILE = "/ dev / shm / mysqlchk. $$. Out"
ERR_FILE = "/ dev / shm / mysqlchk. $$. Err"
FORCE_FAIL = "/ dev / shm / proxyoff"
MYSQL_BIN = "/ opt / mysqlcluster / mysql-cluster-gpl-7.2.6-linux2.6-x86_64 / bin / mysql"
CHECK_QUERY = "select 1"
preflight_check ()
{
for I in "$ TMP_FILE" "$ ERR_FILE"; do
if [-f "$ I"]; then
if [-w $ I!]; then
echo -e "HTTP / 1.1 503 Service Unavailable r n"
echo -e "Content-Type: Content-Type: text / plain r n"
echo -e " r n"
echo -e "Can not write to $ I r n"
echo -e " r n"
exit 1
fi
fi
done
}
return_ok ()
{
echo -e "HTTP / 1.1 200 OK r n"
echo -e "Content-Type: text / html r n"
echo -e "Content-Length: 43 r n"
echo -e " r n"
echo -e "< html > < body > MySQL is running. < / body > < / html > r n"
echo -e " r n"
rm $ ERR_FILE $ TMP_FILE
exit 0
}
return_fail ()
{
echo -e "HTTP / 1.1 503 Service Unavailable r n"
echo -e "Content-Type: text / html r n"
echo -e "Content-Length: 42 r n"
echo -e " r n"
echo -e "< html > < body > MySQL is * down *. < / body > < / html > r n"
sed -e 's / n $ / r n /' $ ERR_FILE
echo -e " r n"
rm $ ERR_FILE $ TMP_FILE
exit 1
}
preflight_check
if [-f "$ FORCE_FAIL"]; then
echo "$ FORCE_FAIL found"> $ ERR_FILE
return_fail;
fi
$ MYSQL_BIN $ MYSQL_OPTS --host = $ MYSQL_HOST --socket = $ MYSQL_SOCKET --user = $ MYSQL_USERNAME --password = $ MYSQL_PASSWORD -e "$ CHECK_QUERY"> $ TMP_FILE 2> $ ERR_FILE
if [$ -ne 0?]; then
return_fail;
fi
return_ok;
test
2 nodes open keepalived (master node will get vip, automatic pull haproxy), xinetd
root@10.1.6.203: ~ # ip add
1: lo: < LOOPBACK, UP, LOWER_UP > mtu 16436 qdisc noqueue state UNKNOWN
link / loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
inet 127.0.0.1/8 scope host lo
2: eth0: < BROADCAST, MULTICAST > mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link / ether 00: 26: b9: 36: 0f: 81 brd ff: ff: ff: ff: ff: ff
inet 211.151.105.186/26 brd 211.151.105.191 scope global eth0
3: eth1: < BROADCAST, MULTICAST, UP, LOWER_UP > mtu 1500 qdisc pfifo_fast state UP qlen 1000
link / ether 00: 26: b9: 36: 0f: 83 brd ff: ff: ff: ff: ff: ff
inet 10.1.6.203/24 brd 10.1.6.255 scope global eth1
inet 10.1.6.173/32 scope global eth1
4: eth2: < BROADCAST, MULTICAST > mtu 1500 qdisc noop state DOWN qlen 1000
link / ether 00: 26: b9: 36: 0f: 85 brd ff: ff: ff: ff: ff: ff
5: eth3: < BROADCAST, MULTICAST > mtu 1500 qdisc noop state DOWN qlen 1000
link / ether 00: 26: b9: 36: 0f: 87 brd ff: ff: ff: ff: ff: ff
root@10.1.6.203: ~ # netstat -tunlp | grep ha
tcp 0 0 10.1.6.173:3366 0.0.0.0:* LISTEN 1042 / haproxy
tcp 0 0 10.1.6.203:8888 0.0.0.0:* LISTEN 1042 / haproxy
udp 0 0 0.0.0.0:56562 0.0.0.0:* 1042 / haproxy
root@10.1.6.203: ~ # netstat -tunlp | grep xine
tcp 0 0 10.1.6.203:9222 0.0.0.0:* LISTEN 30897 / xinetd
root@10.1.6.203: ~ # ps -ef | grep haproxy
root 1042 1 0 Sep17? 00:00:00 / usr / local / sbin / haproxy -f /etc/haproxy/haproxy.cfg
test:
vip10.1.6.173 3366 cluster database access through (note the account dave permissions need to add three ip10.1.6.203,10.1.6.205,10.1.6.173)
root@10.1.6.203: mgm # mysql -udave -p -h 10.1.6.173 -P 3366
Enter password:
Welcome to the MySQL monitor Commands end with;. Or g.
Your MySQL connection id is 1344316
Server version: 5.5.22-ndb-7.2.6-gpl-log MySQL Cluster Community Server (GPL)
Type 'help;' or ' h' for help Type ' c' to clear the buffer..
mysql> show databases;
+ -------------------- +
| Database |
+ -------------------- +
| Information_schema |
| Dave |
| Test |
+ -------------------- +
3 rows in set (0.01 sec)
mysql>
Manually respectively keepalive, haproxy, the database will automatically drift to hang .vip10.1.6.173 from 10.1.6.205, does not affect the vip access
By vip, haproxy view the status of each node
http://10.1.6.173:8888/status |
|
|
|