集群介紹,keepalived介紹,用keepalived配置高可用集群
阿新 • • 發佈:2018-07-04
conf global aux prior chmod multicast gre ns3 生產環境 集群介紹
- 根據功能劃分為兩大類:高可用和負載均衡
- 高可用集群通常為兩臺服務器,一臺工作,另外一臺作為冗余,當提供服務的機器宕機,冗余將接替繼續提供服務
- 業內有個綜合評分比如4個九,甚至5個九六個九來衡量服務的高可用,一般大企業的核心業務都有高可用,如果一個機器故障,一分鐘之內就可以切換到另一臺上
- 實現高可用的開源軟件有:heartbeat、keepalived
- 負載均衡集群,需要有一臺服務器作為分發器,它負責把用戶的請求分發給後端的服務器處理,在這個集群裏,除了分發器外,就是給用戶提供服務的服務器了,這些服務器數量至少為2
- 實現負載均衡的開源軟件有LVS、keepalived、haproxy、nginx,商業的有F5、Netscaler
keepalived介紹
- 在這裏我們使用keepalived來實現高可用集群,因為heartbeat在centos6上有一些問題,影響實驗效果,並更新不及時
- keepalived通過VRRP(Virtual Router Redundancy Protocl即虛擬路由冗余協議)來實現高可用。
- 在這個協議裏會將多臺功能相同的路由器(其實是一臺機器)組成一個小組,這個小組裏會有1個master角色和N(N>=1)個backup角色。
- master會通過組播的形式向各個backup發送VRRP協議的數據包,當backup收不到master發來的VRRP數據包時,就會認為master宕機了。此時就需要根據各個backup的優先級來決定誰成為新的mater。
- Keepalived要有三個模塊,分別是core、check和vrrp。其中core模塊為keepalived的核心,負責主進程的啟動、維護以及全局配置文件的加載和解析,check模塊負責健康檢查,vrrp模塊是來實現VRRP協議的。
用keepalived配置高可用集群
- 準備兩臺機器130和132,130作為master,132作為backup
- 兩臺機器都執行yum install -y keepalived
- 兩臺機器都安裝nginx,其中130上已經編譯安裝過nginx,132上需要yum安裝nginx: yum install -y nginx
- 之所以選擇nginx,在生產環境中,好多企業把它作為負載均衡器,如果它掛掉,影響後面好多web服務器,所以不能出現單點故障
- 編輯130上的keepalived配置文件,
[root@akuilinux01 ~]# > /etc/keepalived/keepalived.conf [root@akuilinux01 ~]# vim !$ vim /etc/keepalived/keepalived.conf global_defs { #這個全局定義參數 notification_email { #出現問題給郵箱發郵件, [email protected] } notification_email_from [email protected] #由哪一個郵件發送 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 # 3秒檢測一次 } vrrp_instance VI_1 { # 定義master相關的 state MASTER #如果是從就是backup interface ens33 #定義網卡 virtual_router_id 51 # 定義路由器的id priority 100 #定義權重 advert_int 1 authentication { #認證相關 auth_type PASS auth_pass aminglinux>com } virtual_ipaddress { #定義vip(公有的ip) 192.168.21.100 } track_script { #加載服務 chk_nginx } }
- 在130上編輯監控腳本
[root@akuilinux01 ~]# 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
- 給腳本755權限,並啟動keepalived服務
[root@akuilinux01 ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@akuilinux01 ~]# systemctl start keepalived [root@akuilinux01 ~]# ps aux |grep keepa root 2895 0.0 0.0 118608 1388 ? Ss 23:20 0:00 /usr/sbin/keepalived -D root 2896 0.1 0.1 127468 3304 ? S 23:20 0:00 /usr/sbin/keepalived -D root 2899 0.3 0.1 127408 2836 ? S 23:20 0:00 /usr/sbin/keepalived -D root 2961 0.0 0.0 112676 980 pts/1 S+ 23:20 0:00 grep --color=auto keepa
- 檢測下nginx可以自動加載不
[root@akuilinux01 ~]# ps aux |grep nginx root 1016 0.0 0.0 46008 1260 ? Ss 20:52 0:00 nginx: master process /usr/localnginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 1017 0.0 0.2 48496 4152 ? S 20:52 0:00 nginx: worker process nobody 1018 0.0 0.2 48496 3896 ? S 20:52 0:00 nginx: worker process root 2982 0.0 0.0 112680 980 pts/1 S+ 23:20 0:00 grep --color=auto nginx [root@akuilinux01 ~]# /etc/init.d/nginx stop Stopping nginx (via systemctl): [ 確定 ] [root@akuilinux01 ~]# ps aux |grep nginx root 3266 1.0 0.0 46008 1252 ? Ss 23:22 0:00 nginx: master process /usr/localnginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 3267 0.0 0.2 48496 3892 ? S 23:22 0:00 nginx: worker process nobody 3268 0.0 0.2 48496 3892 ? S 23:22 0:00 nginx: worker process root 3273 0.0 0.0 112676 984 pts/1 S+ 23:22 0:00 grep --color=auto nginx
- 日誌
[root@akuilinux01 ~]# less /var/log/messages
- 查看vip
[root@akuilinux01 ~]# 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:87:fb:87 brd ff:ff:ff:ff:ff:ff inet 192.168.21.128/24 brd 192.168.21.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.21.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::68fd:e6fa:c781:f5a6/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:87:fb:91 brd ff:ff:ff:ff:ff:ff inet 192.168.110.136/24 brd 192.168.110.255 scope global dynamic ens37 valid_lft 1278sec preferred_lft 1278sec inet6 fe80::c559:4a92:72f1:b448/64 scope link valid_lft forever preferred_lft forever
-
132上編輯配置文件
[root@akuilinux02 ~]# > /etc/keepalived/keepalived.conf [root@akuilinux02 ~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { [email protected] } notification_email_from [email protected] 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 aminglinux>com } virtual_ipaddress { 192.168.21.100 } track_script { chk_nginx } }
- 132上編輯監控腳本
[root@akuilinux02 ~]# vim /usr/local/sbin/check_ng.sh #時間變量,用於記錄日誌 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
- 給腳本755權限並啟動keepalived
[root@akuilinux02 ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@akuilinux02 ~]# systemctl start keepalived
測試
- curl或者瀏覽器打開
[root@akuilinux01 ~]# curl [email protected] master,master. [root@akuilinux01 ~]# curl [email protected] backup,backup. [root@akuilinux01 ~]# curl [email protected] master,master.
- 測試高可用
[root@akuilinux01 ~]# systemctl stop keepalived [root@akuilinux01 ~]# curl [email protected] backup,backup. [root@akuilinux01 ~]# systemctl start keepalived [root@akuilinux01 ~]# curl [email protected] master,master.
擴展
- heartbeat和keepalived比較
- DRBD工作原理和配置
- mysql+keepalived
集群介紹,keepalived介紹,用keepalived配置高可用集群