用keepalived高可用nginx

單主模型

環境四個虛擬機

upstream server:node1:172.16.100.6 ? ? ? ? CeotOS 6

upstream server:node3:172.16.100.69 ? ? ? ? CeotOS 7

節點1:172.16.100.67 ? ? ? ? ? ? ? ? ? ? ? ?CeotOS 7

節點2:node2:172.16.100.68 ? ? ? ? ? ? ? ? ? ? ??CeotOS 7

節1&2

# yum install nginx -y

# cd /etc/nginx

# vim nginx.conf

? ? ? ? ? ? ? ? ? ? ? include /etc/nginx/conf.d/*.conf;
? ? ? ? ? ? ? ? ? ? ? upstream webservers {
  ? ? ? ? ? ? ? ? ? ?  ? ? ? ? ? ? ? ? ? ? server 172.16.100.6:80 weight=1;
  ? ? ? ? ? ? ? ? ? ?  ? ? ? ? ? ? ? ? ? ? server 172.16.100.69:80 weight=1;
 ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ?location ? / ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?proxy_pass ? http://websrvers/;

? ? ? ? ? ? ? ? ? ? ?}

# nginx ? -t ?

# systemctl start nginx.service
# netstat -tnlp
用瀏覽器訪問172.16.100.67
# scp nginx.conf node2:/etc/nginx/

節2
# systemctl start nginx.service
# netstat -tnlp

用瀏覽器訪問172.16.100.68看能否負載均衡

  • 用keepalived監控nginx服務
節1&節2
# vim keepalived.conf 
     vrrp_script chk_nginx {
             script "killall -0 nginx &> /dev/null"
             interval 1 
             weight -10
       }
       vrrp_instance VI_1 {
             track_script {
                   chk_nginx
             }
       }
節1

# systemctl status nginx.service

# systemctl start keepalived.service ssh node2:systemctl start keepalived.service

# systemctl status keepalived.service

# ip addr list

如果沒啟動起來,可能是組播地址和虛擬地址的問題,修改一下即可

宕掉nginx,肯是否會轉到節點2上

#?systemctl stop nginx.service

#?systemctl status nginx

節2

# ip addr list

節1

#?systemctl start nginx.service

瀏覽器訪問172.16.100.88

節1(主節點)只要在線,節2(備節點)就搶不走,除非節1宕掉
# vim notify

 #!/bin/bash

 #


 vip=172.16.100.88

 contact='root@localhost' 


 notify() {

 mailsubject="`hostname` to be $1: $vip floating"??? 

 mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"???

echo $mailbody | mail -s "$mailsubject" $contact

 }


 case "$1" in

           master)

?             notify master
              systemctl restart nginx.service

              exit 0

           ;;

           backup)

               notify backup
               systemctl restart nginx.service

               exit 0

           ;;

           fault)

               notify fault

                exit 0

           ;;

           *)

               echo 'Usage: `basename $0` {master|backup|fault}'

               exit 1

            ;;

  esac

# scp notify.sh node2:/etc/keepalived/

# systemctl status keepalived

# ip addr list

主停掉,備也停掉,會自動啟動nginx

# systemctl stop httpd.server

節2

# systemctl stop nginx.service

# systemctl status keepalived

節1

# systemctl status nginx.service

# systemctl status keepalived

用keepalived高可用nginx 用keepalived高可用nginx

雙主模型

節1
# vim keepalived.conf
            vrrp_instance VI_2 {

                        state BACKUP
                        interface ens33

                        virtual_router_id 61

                        priority 99

                        advert_int 1

                        authentication {

                                 auth_type PASS
                                 auth_pass 1111
                       }

                       virtual_ipaddress {

                                172.16.100.180/16 dev ens33 label ens33:2

                       }
                       track_script {

                                      chk_nginx

                       }

                        notify_master “/etc/keepalived/notify.sh master”   (不手動啟動nginx,而讓監控系統確保nginx在線)

                        notify_backup “/etc/keepalived/notify.sh backup”

                        notify_fault “/etc/keepalived/notify.sh fault”

             }
節1&節2
(不手動啟動nginx,而讓監控系統確保nginx在線)
# vim notify.sh
   注釋掉systemctl restart nginx.service
節2
# vim keepalived.conf
        vrrp_instance VI_2 {

                        state MASTER
                        interface ens33

                        virtual_router_id 61

                        priority 100

                        advert_int 1
                        authentication {

                                 auth_type PASS
                                 auth_pass 1111
                       }

                       virtual_ipaddress {

                                172.16.100.180/16 dev ens33 label ens33:2

                       }
                       track_script {

                                      chk_nginx
                       }

                       notify_master “/etc/keepalived/notify.sh master” 

                       notify_backup “/etc/keepalived/notify.sh backup”

                       notify_fault “/etc/keepalived/notify.sh fault”

         }
節1

# sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service'
# systemctl status keepalived
# ifconfig ens33:1 down
# sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service'
# systemctl status keepalived
用keepalived高可用nginx
節2
# systemctl status keepalived.service
# ip addr list
用瀏覽器訪問兩個地址,看調度是否有問題
節1

# sytemctl restart keepalived.service; sytemctl start httpd.service
# systemctl status keepalived

用keepalived高可用nginx
節2
# ifconfig
節1
# sytemctl stop httpd.service; sytemctl restart nginx.service'
# systemctl status nginx.service
# systemctl status keepalived
用keepalived高可用nginx
# ifconfig     




原創文章,作者:nene,如若轉載,請注明出處:http://www.www58058.com/88155

(0)
nenenene
上一篇 2017-10-15 16:07
下一篇 2017-10-15 22:15

相關推薦

  • btrfs文件系統——更加高級的文件系統

    Btrfs 文件系統: Btrfs ( B-tree ,Butter FS ,Better FS ) GPL ,Oracle公司在2007年研發,支持CoW(對文件快照),為了取代ext3,ext4,xfs 核心特點:          多物理卷支持:btrfs可由多個底層物理卷…

    Linux干貨 2016-08-31
  • 第五周練習

    1.顯示當前系統上root,fedora或user1用戶的默認shell         # cat /etc/passwd | cut -d: -f1,7 | grep -E "(fedora|root|user1)" 2.找出/etc/rc.d/init.…

    Linux干貨 2016-11-26
  • 硬鏈接與軟鏈接的聯系與區別

    硬鏈接與軟鏈接的聯系與區別 我們知道文件都有文件名與數據,這在 Linux 上被分成兩個部分:用戶數據 (user data) 與元數據 (metadata)。用戶數據,即文件數據塊 (data block),數據塊是記錄文件真實內容的地方;而元數據則是文件的附加屬性,如文件大小、創建時間、所有者等信息。在 Linux 中,元數據中的 inode 號(ino…

    Linux干貨 2016-10-23
  • ELK+RabbitMQ架構處理nginx及tomcat日志

    前言       查看日志的傳統方法是:登錄操作系統,使用命令工具如cat、tail、sed、awk、grep等等進行過濾輸出后分析,處理少量日志還好,日志量大處理效率就沒那么高了。而且很多情況下開發人員需要查看并分析日志進行排錯,但他們對Linux命令又不是太熟悉,而且有時候又不能賦予他們服務器權限,更多時…

    Linux干貨 2016-08-02
  • bash 循環語句for、while、until

    循環執行 循環執行     定義:將某段代碼有次數或無次數循環執行多次。     構成要素:退出條件和進入條件     循環的三種語句:for、while、until for循環:      &nbs…

    Linux干貨 2016-08-21
欧美性久久久久