Keepalived+Mysql+Haproxy
#dd dd0 配主從
vi /etc/my.cnf
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 1
#dd
grant replication slave on *.* to ‘dd‘@‘192.168.55.%‘ identified by ‘123456‘
show master status;
#dd0
change master to master_host=‘192.168.55.138‘,master_user=‘dd‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=530;
start slave;
#dd dd0
tar haproxy
make TARGET=linux310 ARCH=x86_64
make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/
#dd dd0 配haproxy的啟動腳本
vi /etc/rc.d/init.d/haproxy.sh
#!/bin/sh
#
# haproxy
#
# chkconfig: - 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
chmod +x /etc/rc.d/init.d/haproxy.sh
cd /etc/rc.d/init.d
chkconfig --add haproxy.sh
#dd dd0配haproxy
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
vi /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
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option dontlognull
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 600
listen stats
mode http
bind :6677
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend main
bind *:23306
default_backend mysql
backend mysql
balance leastconn
server m1 192.168.55.138:3306 check port 3306 maxconn 300
server m2 192.168.55.129:3306 check port 3306 maxconn 300
#dd dd0 啟動haproxy並查看狀態
systemctl start haproxy
systemctl status haproxy
#dd0以dd允許的賬戶通過haproxy的23306端口訪問dd的mysql
mysql -udd -p123456 -h192.168.55.138 -P23306
show variables like ‘server_id‘;
判斷id是不是dd
#dd dd0配keepalived
vi /etc/keepalived/keepalived.conf # 兩個機器配置文件不同
! Configuration File for keepalived
global_defs {
notification_email { # 忽略
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh" # 檢查haproxy的腳本
interval 2 # 每兩秒檢查一次
}
vrrp_instance VI_1 {
state BACKUP # 定義為BACKUP節點
nopreempt # 開啟不搶占,另一個不寫
interface ens33
virtual_router_id 51
priority 100 # 開啟了不搶占,所以此處優先級必須高於另一臺,另一個寫99
advert_int 1
authentication {
auth_type PASS
auth_pass abcd
}
virtual_ipaddress {
192.168.81.150 # 配置VIP
}
track_script {
chk_haproxy # 調用檢查腳本
}
notify_backup "/etc/init.d/haproxy restart"
notify_fault "/etc/init.d/haproxy stop"
}
#dd dd0 配chk.sh
vi /etc/keepalived/chk.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/chk.sh
service keepalived start
#測試
ip addr # 查看是否綁定了虛ip
tcpdump -nn -i ens33 vrrp # 抓包查看
http://192.168.55.138:6677/haproxyadmin?stats # 通過haproxy查看狀態
Keepalived+Mysql+Haproxy