Nginx教程以及配置keepalived

xiangjai發表於2017-02-21

Nginx教程

1.  課程目標


1.1.  瞭解反向代理和負載均衡的概念

1.2.  掌握Nginx的安裝和使用

1.3.  利用Nginx實現負載均衡


2.  Nginx相關概念


2.1.  反向代理

反向代理(Reverse Proxy)方式是指以代理伺服器來接受internet上的連線請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連線的客戶端,此時代理伺服器對外就表現為一個伺服器。

2.2.  負載均衡

負載均衡,英文名稱為Load Balance,是指建立在現有網路結構之上,並提供了一種廉價有效透明的方法擴充套件網路裝置和伺服器的頻寬、增加吞吐量、加強網路資料處理能力、提高網路的靈活性和可用性。其原理就是資料流量分攤到多個伺服器上執行,減輕每臺伺服器的壓力,多臺伺服器共同完成工作任務,從而提高了資料的吞吐量。


3.  Nginx的安裝


3.1.  下載nginx

官網:http://nginx.org/

3.2.  上傳並解壓nginx

tar -zxvf nginx-1.8.1.tar.gz -C/usr/local/src


3.3.  編譯nginx

#進入到nginx原始碼目錄

cd /usr/local/src/nginx-1.8.1


#檢查安裝環境,並指定將來要安裝的路徑

./configure --prefix=/usr/local/nginx

 

#缺包報錯 ./configure: error: C compiler cc is not found

 

#使用YUM安裝缺少的包

yum-y install gcc pcre-devel openssl openssl-devel


#編譯安裝

make&& make install

 

安裝完後測試是否正常:

/usr/loca/nginx/sbin/nginx

檢視埠是否有ngnix程式監聽

netstat-ntlp | grep 80


4.  配置nginx

4.1.  配置反向代理

1.修改nginx配置檔案

server {

    listen       80;

    server_name  nginx-01.itcast.cn;    #nginx所在伺服器的主機名

#反向代理的配置

location / {             #攔截所有請求

    root html;

        proxy_pass http://192.168.0.21:8080;   #這裡是代理走向的目標伺服器:tomcat

    }

}

2.啟動tomcat-01上的tomcat

 

3.啟動nginx-01上的nginx

./nginx


 

重啟:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`


參考網址:http://www.cnblogs.com/jianxie/p/3990377.html

4.2.  動靜分離

#動態資源 index.jsp

location ~ .*\.(jsp|do|action)$ {

    proxy_pass http://tomcat-01.itcast.cn:8080;

}

 

#靜態資源

location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {

    expires 3d;

}

 

4.3.  負載均衡

在http這個節下面配置一個叫upstream的,後面的名字可以隨意取,但是要和location下的proxy_pass http://後的保持一致。

http {

    是在http裡面的, 已有http, 不是在server裡,在server外面

    upstream tomcats {

        server shizhan02:8080 weight=1;#weight表示多少個

        server shizhan03:8080 weight=1;

        server shizhan04:8080 weight=1;

}

#解除安裝server裡

location ~ .*\.(jsp|do|action) {

    proxy_pass http://tomcats;        #tomcats是後面的tomcat伺服器組的邏輯組號

}

}

5.  利用keepalived實現高可靠(HA)

5.1.  高可靠概念

HA(High Available), 高可用性叢集,是保證業務連續性的有效解決方案,一般有兩個或兩個以上的節點,且分為活動節點及備用節點。

5.2.  高可靠軟體keepalived

keepalive是一款可以實現高可靠的軟體,通常部署在2臺伺服器上,分為一主一備。Keepalived可以對本機上的程式進行檢測,一旦Master檢測出某個程式出現問題,將自己切換成Backup狀態,然後通知另外一個節點切換成Master狀態。

5.3.  keepalived安裝

下載keepalived官網:http://keepalived.org

 

將keepalived解壓到/usr/local/src目錄下

tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src


 

進入到/usr/local/src/keepalived-1.2.19目錄

cd /usr/local/src/keepalived-1.2.19


 

開始configure

./configure--prefix=/usr/local/keepalived


 

#編譯並安裝

make && make install


 

5.4.  將keepalived新增到系統服務中

拷貝執行檔案

cp /usr/local/keepalived/sbin/keepalived/usr/sbin/


將init.d檔案拷貝到etc下,加入開機啟動項

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/keepalived


將keepalived檔案拷貝到etc下

cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/


建立keepalived資料夾

mkdir -p/etc/keepalived


將keepalived配置檔案拷貝到etc下

cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf


新增可執行許可權

chmod+x/etc/init.d/keepalived

 

##以上所有命令一次性執行:

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir -p /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

chmod +x /etc/init.d/keepalived

chkconfig --add keepalived      

chkconfig keepalived on

 

新增keepalived到開機啟動

chkconfig --add keepalived      
chkconfig keepalived on


 

 

 

 

5.5.  配置keepalived虛擬IP

修改配置檔案: /etc/keepalived/keepalived.conf

#MASTER節點

global_defs {

}

vrrp_instance VI_1 {

    state MASTER   #指定A節點為主節點 備用節點上設定為BACKUP即可

    interface eth0    #繫結虛擬IP的網路介面

    virtual_router_id 51   #VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP組

    priority 100   #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低

    advert_int 1  #組播資訊傳送間隔,兩個節點設定必須一樣

    authentication {    #設定驗證資訊,兩個節點必須一致

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {    #指定虛擬IP, 兩個節點設定必須一樣

        192.168.33.60/24    #如果兩個nginxip分別是192.168.33.61,,...62,則此處的虛擬ip跟它倆同一個網段即可

    }

}

 

#BACKUP節點

global_defs {

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.33.60/24

    }

}

 

#分別啟動兩臺機器上的keepalived

service keepalived start


測試:

殺掉master上的keepalived程式,你會發現,在slave機器上的eth0網路卡多了一個ip地址

檢視ip地址的命令:  ip addr

 

5.6.  配置keepalived心跳檢查

原理:

Keepalived並不跟nginx耦合,它倆完全不是一家人

但是keepalived提供一個機制:讓使用者自定義一個shell指令碼去檢測使用者自己的程式,返回狀態給keepalived就可以了

 

 

#MASTER節點

global_defs {

}

 

vrrp_script chk_health {

    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"

    interval 1    #每隔1秒執行上述的指令碼,去檢查使用者的程式ngnix

    weight -2

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 1

    priority 100

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

    track_script {

        chk_health

    }

 

    virtual_ipaddress {

        10.0.0.10/24

    }

 

    notify_master "/usr/local/keepalived/sbin/notify.sh master"

    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"

    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"

}

 

#新增切換通知指令碼

vi /usr/local/keepalived/sbin/notify.sh

#!/bin/bash

 

case "$1" in

    master)

        /usr/local/nginx/sbin/nginx

        exit 0

    ;;

backup)

        /usr/local/nginx/sbin/nginx -s stop

        /usr/local/nginx/sbin/nginx

        exit 0

    ;;

    fault)

        /usr/local/nginx/sbin/nginx -s stop

        exit 0

    ;;

    *)

        echo 'Usage: notify.sh {master|backup|fault}'

        exit 1

    ;;

esac

 

#新增執行許可權

chmod +x/usr/local/keepalived/sbin/notify.sh

global_defs {

}

 

vrrp_script chk_health {

    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"

    interval 1

    weight -2

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 1

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

    track_script {

        chk_health

    }

 

    virtual_ipaddress {

        10.0.0.10/24

    }

        

    notify_master "/usr/local/keepalived/sbin/notify.sh master"

    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"

    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"

}

 

#在第二臺機器上新增notify.sh指令碼

#分別在兩臺機器上啟動keepalived

service keepalived start
chkconfig keepalived on


相關文章