部署圖:java
集羣部署的理解:linux
、keepalived呾haproxy必項裝在同一臺機器上(如172.17.210.210.83機器上,keepalived呾haproxy都要安裝),keepalived負責爲該服務器搶佔vip(虛擬ip),搶佔vip後,對主機的訪問能夠經過原來的ip(172.17.210.210.83)訪問,也能夠經過vip(172.17.210.210.103)訪問。redis
172.17.210.64上癿keepalived也會去搶佔vip,搶佔vip有優先級,配置keepalived.conf中的(priority 150 #數值愈多,優先級越高,172.17.210.64上改成120,master和slave上該值配置不一樣)決定。可是通常哪臺主機上的keepalived服務先啓動就會搶佔到vip,即便是slave,只要先啓動也能搶到算法
haproxy負責將對vip癿請求分發到mycat上。起到負載均衡的做用,同時haproxy也能檢測到mycat是否存活,haproxy只會將請求轉發到存貨的mycat上。vim
若是一臺朋務器(keepalived+haproxy朋務器)宕機,另一臺上癿keepalived會馬上搶佔vip並接管服務。tomcat
haproxy的安裝和部署:bash
下載haproxy linux 安裝包:haproxy-1.5.8.tar.gz tar zxvf haproxy-1.5.8.tar.gz cd haproxy-1.4.25 make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=x86_64 make install PREFIX=/usr/local/haproxy cd /usr/local/haproxy #chown -R haproxy.haproxy *
haproxy.cfg服務器
global log 127.0.0.1 local0 debug ##記日誌的功能 maxconn 4096 user sa group sa pidfile /opt/app/tomcat/haproxy-1.5.8/haproxy.pid daemon defaults log global option dontlognull retries 3 option redispatch maxconn 2000 timeout connect 5000 timeout client 50000 timeout server 50000 listen admin_status ip:48800 ##VIP stats uri /admin-status ##統計頁面 stats auth admin:admin mode http option httplog listen allmycat_service ip1:8096 ##轉發到mycat的8066端口,即mycat的服務端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin #負載均衡算法 server mycat_31 ip1:8066 check port 48700 inter 5s rise 2 fall 3 server mycat_67 ip2:8066 check port 48700 inter 5s rise 2 fall 3 timeout server 20000 mode http option httplog listen allmycat_admin ip:8097 ##轉發到mycat的9066端口,及mycat的管理控制檯端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin server mycat_31_9066 ip1:9066 check port 48700 inter 5s rise 2 fall 3 server mycat_67_9066 ip2:9066 check port 48700 inter 5s rise 2 fall 3 timeout server 20000
haproxy日誌記錄網絡
默認haproxy是不記錄日誌的 ,爲了記錄日誌須要配置syslog,在linux下是rsyslogd服務, 先安裝app
rsyslog yum –y install rsyslog
配置監聽mycat是否存活
在Mycat server1 Mycat server2上都須要添加檢測端口48700的腳本,爲此須要用到xinetd,xinetd爲linux系統的基礎服務。
首先在xinetd目錄下面增長腳本與端口映射配置文件
若是xinetd沒有安裝,使用以下命令安裝:
yum install xinetd -y
檢查 /etc/xinetd.d文件夾是否存在,不存在加上
cd /etc mkdir xinetd.d
增長 /etc/xinetd.d/mycat_status
vim /etc/xinetd.d/mycat_status
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 }
/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=`ps -ef|grep 'usr/local/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
/etc/services中加入mycat_status服務
加入mycat_status服務,
cd /etc vi services
在末尾加入如下內容: mycat_status 48700/tcp # mycat_status
重啓xinetd朋務 service xinetd restart
驗證mycat_status服務是否啓動成功
netstat -antup|grep 48700
keepalived安裝
tar zxvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./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 /usr/local/keepalived/etc/rc.d/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
keepalived配置
keepalived.conf:
vi /etc/keepalived/keepalived.conf
! 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 eth0 #對外提供朋務癿網絡接口:ipconfig 查看網卡 virtual_router_id 60 #VRRP組名,兩個節點設置必項同樣,指明各個節點屬二同一VRRP組 priority 150 #數值愈多,優先級越高,上改成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.10.10 dev eth0 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 }
注意: 默認爲51,須要修改一下,要不在keepallived會報錯。
interface eth1 和172.17.210.103 dev eth1 scope global中癿eth1指的是網卡,若是是多網卡,可能會有eth0,eth1,eth2…,可使用ifconfig命令查看,確保eth0是本機存在的網卡地址。有的服務器若是隻有一個網卡,但被人爲把eth0改爲eth1了,你在寫eth0就找不到了。
virtual_router_id 60
/etc/keepalived/scripts 檢查腳本:
-rwxr-xr-x 1 root root 528 Apr 14 22:08 check_haproxy.sh -rwxr-xr-x 1 root root 531 Apr 14 22:14 haproxy_backup.sh -rwxr-xr-x 1 root root 104 Apr 14 22:11 haproxy_fault.sh -rwxr-xr-x 1 root root 531 Apr 14 22:10 haproxy_master.sh -rwxr-xr-x 1 root root 103 Apr 14 22:11 haproxy_stop.sh
check_haproxy.sh: #!/bin/bash STARTHAPROXY="/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg" STOPKEEPALIVED="/etc/init.d/keepalived stop" LOGFILE="/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 sleep 5 fi if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then exit 0 else exit 1 fi
haproxy_backup.sh [root@kaifa scripts]# cat haproxy_backup.sh #!/bin/bash STARTHAPROXY=`/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg` STOPHAPROXY=`ps -ef |grep haproxy-1.5.8/haproxy|grep -v grep|awk '{print $2}'|xargs kill -s 9` LOGFILE="/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 [root@kaifa scripts]# cat haproxy_fault.sh #!/bin/bash LOGFILE=var/log/keepalived-haproxy-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE [root@kaifa scripts]# cat haproxy_master.sh #!/bin/bash STARTHAPROXY=`/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg` STOPHAPROXY=`ps -ef |grep haproxy-1.5.8/haproxy|grep -v grep|awk '{print $2}'|xargs kill -s 9` LOGFILE="/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 [root@kaifa scripts]# cat haproxy_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-haproxy-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
啓用keepalived服務
啓用服務 service keepalived start
[root@kaifa scripts]# cat /etc/sysconfig/keepalived # Options for keepalived. See `keepalived --help' output and keepalived(8) and # keepalived.conf(5) man pages for a list of all options. Here are the most # common ones : # # --vrrp -P Only run with VRRP subsystem. # --check -C Only run with Health-checker subsystem. # --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. # --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. # --dump-conf -d Dump the configuration data. # --log-detail -D Detailed log messages. # --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON) # KEEPALIVED_OPTIONS="-D -d -S 0"