根據功能劃分爲兩大類:高可用和負載均衡html
高可用集羣一般爲兩臺服務器,一臺工做,另一臺做爲冗餘,當提供服務的機器宕機,冗餘將接替繼續提供服務
高可用集羣,英文原文爲High Availability Cluster,簡稱HACluster,簡單的說,集羣(cluster)就是一組計算機,它們做爲一個總體向用戶提供一組網絡資源。這些單個的計算機系統 就是集羣的節點(node)
HA(High Available), 高可用性羣集是經過系統的可靠性(reliability)和可維護性(maintainability)來度量的。工程上,一般用平均無端障時間(MTTF)來度量系統的可靠性,用平均維修時間(MTTR)來度量系統的可維護性。因而可用性被定義爲:HA=MTTF/(MTTF+MTTR)*100%
==具體HA衡量標準:
99% 一年宕機時間不超過4天
99.9% 一年宕機時間不超過10小時
99.99% 一年宕機時間不超過1小時
99.999% 一年宕機時間不超過6分鐘==
實現高可用的開源軟件有:heartbeat、keepalivednode負載均衡集羣,須要有一臺服務器做爲分發器,它負責把用戶的請求分發給後端的服務器處理,在這個集羣裏,除了分發器外,就是給用戶提供服務的服務器了,這些服務器數量至少爲2
實現負載均衡的開源軟件有LVS、keepalived、haproxy、nginx,商業的有F五、Netscaler linux
在這裏咱們使用keepalived來實現高可用集羣,由於heartbeat在centos6上有一些問題,影響實驗效果
keepalived經過VRRP(Virtual Router Redundancy Protocl)來實現高可用。
Keepalived要有三個模塊,分別是core、check和vrrp。中core模塊爲keepalived的核心,負責主進程的啓動、維護以及全局配置文件的加載和解析,check模塊負責健康檢查,vrrp模塊是來實現VRRP協議的。nginx
環境:有A[root@chy01 ~],B[root@chy ~]#
兩個服務器,A爲主服務器,B爲備服務器。首先兩臺服務器都須要安裝keepalived。具體配置以下:
配置主:vim
[root@chy01 ~]# yum install -y keepalived (A機器安裝keepalived) [root@chy ~]# yum install -y keepalived (B機器安裝keepalived) [root@chy01 ~]# ps aux |grep nginx root 2485 0.0 0.0 45484 1280 ? Ss 05:15 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2492 0.0 0.2 47972 4172 ? S 05:15 0:00 nginx: worker process nobody 2493 0.0 0.2 47972 3916 ? S 05:15 0:00 nginx: worker process root 3041 0.0 0.0 112664 976 pts/0 R+ 05:28 0:00 grep --color=auto nginx (A機器上安裝過nginx,須要用nginx來作爲負載均衡器) [root@chy ~]# yum install -y nginx (B機器上用yum安裝的nginx,由於以前沒有安裝過) [root@chy01 ~]# >!$ >/etc/keepalived/keepalived.conf (清空master的keepalived的配置文件) [root@chy01 ~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { chy@chy.com } notification_email_from root@chy.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass chylinux>com } virtual_ipaddress { 192.168.212.1 } track_script { chk_nginx } } 如上是主的配置文件:以下是詳解 1,全局定義(global definition)配置範例 global_defs { notification_email { chy@chy.com } notification_email_from root@chy.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL }global_defs全局配置標識,表面這個區域{}是全局配置,表示keepalived在發生諸如切換操做時須要發送email通知,以及email發送給哪些郵件地址,郵件地址能夠多個,每行一個 notification_email_from 表示發送通知郵件時郵件源地址是誰 smtp_server 127.0.0.1 表示發送email時使用的smtp服務器地址,這裏能夠用本地的sendmail來實現 mtp_connect_timeout 30 鏈接smtp鏈接超時時間 router_id node1 機器標識 vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state MASTER state 指定instance(Initial)的初始狀態,就是說在配置好後,這臺服務器的初始狀態就是這裏指定的,但這裏指定的不算,仍是得要經過競選經過優 先級來肯定,裏若是這裏設置爲master,但如若他的優先級不及另一臺,那麼這臺在發送通告時,會發送本身的優先級,另一臺發現優先級不如本身的 高,那麼他會就回搶佔爲master interface ens33 實例綁定的網卡,由於在配置虛擬IP的時候必須是在已有的網卡上添加的 virtual_router_id 51這裏設置VRID,這裏很是重要,相同的VRID爲一個組,他將決定多播的MAC地 priority 100這裏設置VRID,這裏很是重要,相同的VRID爲一個組,他將決定多播的MAC地址 advert_int 1檢查間隔,默認爲1秒 authentication {這裏設置認證 auth_type PASS認證方式,能夠是PASS或AH兩種認證方式 auth_pass chylinux>com } virtual_ipaddress { 192.168.212.1 }這裏設置的就是VIP,也就是虛擬IP地址,他隨着state的變化而增長刪除,當state爲master的時候就添加,當state爲backup的時候刪除,這裏主要是有優先級來決定的,和state設置的值沒有多大關係,這裏能夠設置多個IP地址 track_script { chk_nginx } } [root@chy01 ~]# vim /usr/local/sbin/check_ng.sh (編輯腳本) #!/bin/bash d=` date --date today +%Y%m%d_%H:%M:%S` #計算nginx進程數量 n=`ps -C nginx --no-heading|wc -l` #若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量, #若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi [root@chy01 ~]# chmod 755 /usr/local/sbin/check_ng.sh (增長腳本的權限) [root@chy01 ~]# systemctl start keepalived [root@chy01 ~]# ps aux |grep keep root 3225 0.0 0.0 111708 1308 ? Ss 06:13 0:00 /usr/sbin/keepalived -D root 3226 0.0 0.1 111708 2560 ? S 06:13 0:00 /usr/sbin/keepalived -D root 3227 0.0 0.1 111708 1624 ? S 06:13 0:00 /usr/sbin/keepalived -D root 3254 0.0 0.0 112664 972 pts/0 S+ 06:13 0:00 grep --color=auto keep [root@chy01 ~]# ps aux |grep nginx root 2485 0.0 0.0 45484 1280 ? Ss 05:15 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2492 0.0 0.2 47972 4172 ? S 05:15 0:00 nginx: worker process nobody 2493 0.0 0.2 47972 3916 ? S 05:15 0:00 nginx: worker process root 3274 0.0 0.0 112668 972 pts/0 S+ 06:13 0:00 grep --color=auto nginx (啓動keepalive服務而且查看是否啓動) [root@chy01 ~]# less /var/log/messages (keepalive的log日誌) [root@chy01 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:13:b3:3b brd ff:ff:ff:ff:ff:ff inet 192.168.212.11/24 brd 192.168.212.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.100.1/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::243b:ddac:7a2b:a5b/64 scope link valid_lft forever preferred_lft forever (查看虛擬ip地址 ,須要用ipadd 查看) (以後須要在主服務器,與備服務器上面都查看防火牆是否關閉,兩邊都須要關閉才能夠)
配置從服務器後端
[root@chy ~]# >/etc/keepalived/keepalived.conf (清空備的配置文件) global_defs { notification_email { aming@aminglinux.com notification_email_from root@aminglinux.com smtp_server 127.0.0.1 notification_email { aming@aminglinux.com smtp_server 127.0.0.1 } aming@aminglinux.com } notification_email_from root@aminglinux.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass chylinux>com } virtual_ipaddress { 192.168.212.1 } track_script { chk_nginx } } (如上是備的配置文件) [root@chy ~]# vim /usr/local/sbin/check_ng.sh (編輯備的腳本文件) !/bin/bash 時間變量,用於記錄日誌 d=`date --date today +%Y%m%d_%H:%M:%S` #計算nginx進程數量 n=`ps -C nginx --no-heading|wc -l` #若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量, #若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived if [ $n -eq "0" ]; then systemctl start nginx n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi [root@chy ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@chy ~]# source /usr/local/sbin/check_ng.sh (使腳本生效) [root@chy ~]# systemctl start keepalived [root@chy ~]# ps aux |grep keepalived root 4484 0.0 0.0 111728 1308 ? Ss 06:58 0:00 /usr/sbin/keepalived -D root 4485 0.0 0.1 111728 2560 ? S 06:58 0:00 /usr/sbin/keepalived -D root 4486 0.0 0.1 111728 1644 ? S 06:58 0:00 /usr/sbin/keepalived -D root 4503 0.0 0.0 112664 980 pts/0 R+ 06:58 0:00 grep --color=auto keepalived (啓動keepalived而且查看是否啓動) [root@chy ~]# cat /usr/share/nginx/html/index.html (yum安裝的nginx的默認主機的位置)
測試高可用centos
[root@chy01 ~]# /etc/init.d/nginx stop Stopping nginx (via systemctl): [ 肯定 ] (測試1關閉nginx) [root@chy01 ~]# ps aux |grep nginx root 10942 0.0 0.0 45484 1276 ? Ss 07:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 10944 0.0 0.2 47972 3912 ? S 07:13 0:00 nginx: worker process nobody 10945 0.0 0.2 47972 3912 ? S 07:13 0:00 nginx: worker process root 10961 0.0 0.0 112664 976 pts/0 S+ 07:13 0:00 grep --color=auto nginx (以後查看會自動啓動這個是由於寫的腳本check) 測試2:在master上增長iptabls規則 [root@chy01 ~]# iptables -I OUTPUT -p vrrp -j DROP [root@chy01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 58 packets, 3974 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 36 packets, 3394 bytes) pkts bytes target prot opt in out source destination 30 1200 DROP 112 -- * * 0.0.0.0/0 0.0.0.0/0 (查看已經iptables的規則) 測試2並不能測試出keepvalied的是否能夠速度切換) 測試3:當中止主的keepvalied服務時,到從的服務器上能夠迅速的看到切換的速度。 [root@chy ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:70:77:62 brd ff:ff:ff:ff:ff:ff inet 192.168.212.10/24 brd 192.168.212.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.212.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::338e:589c:fa07:65e5/64 scope link valid_lft forever preferred_lft forever