標籤:linux
keepalived + haproxy + mysql 構建高可用
1、keepalived 的高可用是主備,有一台作為備用
2、keepalived + haproxy 搭建的高可用是可以兩台都會調度的高可用
拓撲圖:
keepalived:負責搶佔虛擬ip,使用vrrp協議
haproxy:負責做訪問調度,減輕單點壓力,單獨監聽一個連接埠,這裡用23306
1.安裝mysql
分別在兩台機器上面搭建mysql,並做主從配置,這裡不做介紹
2.搭建haproxy
1、download 源碼包,:http://www.haproxy.org/#down
2、在81.128和81.129解壓縮安裝
tar xf haproxy-1.8.4.tar.gz
cd haproxy-1.8.4
yum install -y gcc
make TARGET=linux310 ARCH=x86_64 # uname -a查看主機資訊填寫
make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/
3、提供啟動指令碼
#!/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 0exec="/usr/sbin/haproxy"prog=$(basename $exec)[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$progcfgfile=/etc/haproxy/haproxy.cfgpidfile=/var/run/haproxy.pidlockfile=/var/lock/subsys/haproxycheck() { $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 2esac
4、提供設定檔
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
vim /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/statsdefaults 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 600listen 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 mysqlbackend mysql balance leastconn server m1 192.168.81.128:3306 check port 3306 maxconn 300 server m2 192.168.81.129:3306 check port 3306 maxconn 300
5、修改日誌系統
###Provides UDP syslog reception //去掉下面兩行注釋,開啟UDP監聽
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log //添加此行
service rsyslog restart
6、啟動測試haproxy
service haproxy start
chkconfig --add haproxy
chkconfig haproxy on
netstat -tnlp
mysql -P23306 -uroot -p123456 -h192.168.81.129 # 查看server_id,判斷是否成功
3.搭建keepalived
1、download 源碼包,:http://www.keepalived.org/download.html
2、在81.128和81.129解壓縮安裝
tar xf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/
make && make install
chkconfig --add keepalived
chkconfig keepalived on
3、提供設定檔
vim /etc/keepalived/keepalived.conf # 兩個機器設定檔不同
! Configuration File for keepalivedglobal_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"}
4、建立check檔案
vim /etc/keepalived/chk.sh
#!/bin/bashif [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then /etc/init.d/keepalived stopfi
chmod +x /etc/keepalived/chk.sh
service keepalived start
5、測試
ip addr # 查看是否綁定了虛ip
tcpdump -nn -i ens33 vrrp # 抓包查看
http://192.168.81.128:6677/haproxyadmin?stats # 通過haproxy查看狀態
keepalived + haproxy + mysql 構建高可用資料庫