mount –o loop /home/omcr/rhel-server-7.1-x86_64-dvd.iso /mnt/cdromlinux |
[InstallMedia] #表明整個庫的名字 不能有空格web name=Red Hat Enterprise Linux 7.1 #這個庫的說明 沒啥意義redis baseurl=file:///mnt/cdrom #以什麼方式傳輸 路徑在哪裏 方式能夠有file ftp httpc# metadata_expire=-1bash gpgcheck=0 #是否gpg文件來檢查軟件包的簽名 1是 0 否服務器 cost=500網絡 enabled=1 #是否啓用這個更新庫 0表示不啓用app |
使用下面的命令查詢是否安裝,負載均衡
rpm -qa | grep openssl-develfrontend |
yum install -y openssl-devel |
使用下面的命令查詢是否安裝,
rpm -qa | grep libnl-devel |
爲空說明未安裝,執行下面命令
yum -y install libnl-devel |
7.安裝libnfnetlink-devel
將libnfnetlink-devel-1.0.1-4.el7.x86_64.rpm上傳到/home/omcr目錄下,而後執行
rpm –ivh libnfnetlink-devel-1.0.1-4.el7.x86_64.rpm |
安裝前準備工做成功執行完成以後,就能夠開始安裝Keepalived軟件系統,步驟以下:
1.將keepalived-2.0.5.tar.gz源文件上傳到/home/omcr目錄下
2.解壓keepalived-2.0.5.tar.gz源文件
tar -zxvf keepalived-2.0.5.tar.gz |
3.編譯並安裝
cd /home/omcr/ keepalived-2.0.5 |
./configure --prefix=/usr/local/keepalived make && make install |
以上命令執行完成,安裝過程完成。
爲了方便用戶使用系統命令啓停Keepalived服務,經過如下步驟將安裝好的Keepalived軟件註冊爲系統服務
1)
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ |
2)
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ |
在/etc/init.d/下建立keepalived
修改該文件權限爲可執行
chmod +x /etc/init.d/keepalived |
文件內容以下(用於keepalived定時檢測代理的服務狀態):
#!/bin/sh # # Startup script for the Keepalived daemon # # processname: keepalived # pidfile: /var/run/keepalived.pid # config: /etc/keepalived/keepalived.conf # chkconfig: - 21 79 # description: Start and stop Keepalived
# Source function library . /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there) . /etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start() { echo -n $"Starting $prog: " daemon keepalived ${KEEPALIVED_OPTIONS} RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog }
stop() { echo -n $"Stopping $prog: " killproc keepalived RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog }
reload() { echo -n $"Reloading $prog: " killproc keepalived -1 RETVAL=$? echo }
# See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; condrestart) if [ -f /var/lock/subsys/$prog ]; then stop start fi ;; status) status keepalived RETVAL=$? ;; *) echo "Usage: $0 {start|stop|reload|restart|condrestart|status}" RETVAL=1 esac
exit $RETVAL |
將keepalive.conf和 check_haproxy.sh 、send_arp.sh文件分別上傳到主備機/etc/keepalived目錄下,編輯keepalive.config文件以下
|
global_defs { notification_email { #指定Keepalived在發生事情的時候,發送郵件通知,每行一個地址 root@localhost }
notification_email_from keepalived@localhost #指定發件人 smtp_server 127.0.0.1 #發送email的smtp地址 smtp_connect_timeout 30 #超時時間 router_id haproxy #運行Keepalived的機器標識號,主從機必須不一樣 }
vrrp_script chk_haproxy { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 }
vrrp_instance VI_1 { state BACKUP #指定keepalive的角色MASTER表明主服務器BACKUP表示備用機 interface ens1f0 #指定HA監測網絡的接口 virtual_router_id 51 #虛擬路由標識 同一vrrp實例只有一個,即主備機相同 priority 99 #定義優先級,數字越大優先級越高 主機必須大於備機 advert_int 1 #設定主備負載均衡器之間同步檢查的時間間隔單位是秒 unicast_src_ip 192.168.222.137 #本地ip地址 unicast_peer { 192.168.222.136 #對端IP地址 } authentication { #設定驗證類型和密碼 auth_type PASS #設定驗證類型 有PASS和AH兩種 auth_pass 1111 #設置密碼 同一個vrrp_instance實例下主備機必須設置相同才能正常通訊 } track_script { #執行監控的服務 chk_haproxy } virtual_ipaddress { #keepalive的虛擬ip 192.168.52.201 } # notify_master "/etc/keepalived/send_arp.sh ens1f0 192.168.52.201 192.168.50.1" } |
設置開機自啓動
chkconfig keepalived on |
取消開機自啓動
systemctl disable keepalived.service 取消開機自動啓動 |
測試是否綁定成功
1)停掉服務
systemctl stop keepalived 或者 service keepalived stop |
2)重啓
sudo service keepalived restart |
3)運行 ip addr查看是否綁定成功
也可執行
systemctl status keepalived |
keepalived被註冊爲系統服務後能夠經過如下命令啓動、中止服務或者查詢服務的運行狀態。
1)查詢keepalived服務狀態
systemctl status keepalived 或者 service keepalived status |
2)啓動keepalived服務
systemctl start keepalived 或者 service keepalived start |
3)中止keepalived服務
systemctl stop keepalived 或者 service keepalived stop |
配置syslog
vi /etc/rsyslog.conf |
開啓UDP 514 端口
# Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 |
設置haproxy日誌存放目錄
local2.* /var/log/haproxy.log |
再修改/etc/sysconfig/rsyslog
vi /etc/sysconfig/rsyslog |
修改內容爲
SYSLOGD_OPTIONS="-r -m 0" |
說明:-r:打開接受外來日誌消息的功能,其監控514 UDP端口;
-x:關閉自動解析對方日誌服務器的FQDN信息,這能避免DNS不完整所帶來的麻煩;
-m:修改syslog的內部mark消息寫入間隔時間(0爲關閉),例如240爲每隔240分鐘寫入一次"--MARK--"信息;
-h:默認狀況下,syslog不會發送從遠端接受過來的消息到其餘主機,而使用該選項,則把該開關打開,全部
接受到的信息均可根據syslog.conf中定義的@主機轉發過去
重啓rsyslog服務
systemctl restart rsyslog |
安裝前準備工做成功執行完成以後,就能夠開始安裝Haproxy軟件系統,步驟以下:
1.將haproxy-1.5.18.tar.gz源文件上傳到/home/omcr目錄下
2.解壓haproxy-1.5.18.tar.gz源文件
tar -zxvf haproxy-1.5.18.tar.gz |
3.編譯並安裝
cd /home/omcr/haproxy-1.5.18 |
uname -r #查看系統內核版本號 結果3.10.0-229.el7.x86_64 make TARGET=linux3100 #3100表明系統內核版本號 make install PREFIX=/usr/local/haproxy |
以上命令執行完成,安裝過程完成。
爲了方便用戶使用系統命令啓停haproxy服務,經過如下步驟將安裝好的haproxy軟件註冊爲系統服務
在/etc/init.d/下建立haproxy
修改該文件權限爲可執行
chmod +x /etc/init.d/ haproxy |
文件內容以下:
#!/bin/sh #chkconfig: 2345 10 90 # Source function library . /etc/rc.d/init.d/functions
PROGNAME=haproxy PROGDIR=/usr/local/haproxy DAEMON=$PROGDIR/sbin/$PROGNAME CONFIG=$PROGDIR/$PROGNAME.cfg PIDFILE=$PROGDIR/$PROGNAME.pid SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 RETVAL=0
start() { $DAEMON -f $CONFIG RETVAL=$? echo -e "$PROGNAME started" }
stop() { haproxy_pid="$(cat $PIDFILE)" kill -9 $haproxy_pid RETVAL=$? echo -e "$PROGNAME stoped " }
restart() { $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE) RETVAL=$? echo -e "$PROGNAME restarted " }
case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status $PROGNAME RETVAL=$? ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2 RETVAL=1 ;; esac
exit $RETVAL |
將haproxy.cfg文件分別上傳到主備機/usr/local/haproxy目錄下,編輯haproxy.cfg文件將其中的IP地址修改成實際的集羣服務節點IP地址。
#--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #---------------------------------------------------------------------
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2
chroot /usr/local/haproxy pidfile /usr/local/haproxy/haproxy.pid maxconn 4000 user root group root daemon
# turn on stats unix socket stats socket /usr/local/haproxy/stats
#--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode tcp #默認的模式mode{mode|http|health},tcp4層http7層,health只會返回ok log global option tcplog #日誌類別有httplog option dontlognull option http-server-close # option forwardfor except 127.0.0.0/8 option redispatch retries 3 #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 3000
#--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend rabbitmq bind *:5672 default_backend rabbitmq_cluster
frontend redis bind *:6379 default_backend redis_cluster frontend mycat bind *:8066 default_backend mycat_cluster
#--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend rabbitmq_cluster mode tcp balance roundrobin server master 192.168.53.11:5672 check inter 2000 rise 2 fall 3 server slaver 192.168.53.12:5672 check inter 2000 rise 2 fall 3 backend redis_cluster mode tcp balance roundrobin server master 192.168.53.11:6379 check inter 2000 rise 2 fall 3 server slaver 192.168.53.12:6379 check inter 2000 rise 2 fall 3 backend mycat_cluster mode tcp balance roundrobin server master 192.168.52.155:8066 check inter 2000 rise 2 fall 3 server slaver 192.168.53.12:8066 check inter 2000 rise 2 fall 3 listen cluster_monitoring bind *:9098 #監聽端口 mode http option httplog #採用http日誌格式 timeout client 3h timeout server 3h timeout connect 3h stats enable stats uri /stats #統計頁面url stats refresh 5s #統計頁面自動刷新時間 |
|
如需配置服務自啓動需在上面的配置文件(/etc/rc.d/init.d/haproxy)中添加下面兩句到#!/bin/bash以後
#chkconfig: 2345 10 90 #description:haproxy |
----其中2345是默認啓動級別,級別有0-6共7個級別。
----等級0表示:表示關機
----等級1表示:單用戶模式
----等級2表示:無網絡鏈接的多用戶命令行模式
----等級3表示:有網絡鏈接的多用戶命令行模式
----等級4表示:不可用
----等級5表示:帶圖形界面的多用戶模式
----等級6表示:從新啓動
----10是啓動優先級,90是停機優先級,優先級範圍是0-100,數字越大,優先級越低。
再執行
chkconfig --add haproxy |
重啓服務器執行以下命令查看是否配置成功
netstat -ntlp | grep haproxy |
haproxy被註冊爲系統服務後能夠經過如下命令啓動、中止服務或者查詢服務的運行狀態。
1)查詢haproxy服務狀態
systemctl status haproxy |
2)啓動haproxy服務
systemctl start haproxy |
3)中止haproxy服務
systemctl stop haproxy |