1. 程式人生 > >hadoop03---nginx+keepalived

hadoop03---nginx+keepalived

configure 官網 進制 not found 代理服務器 tar.gz listen 優先 通知

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

技術分享圖片

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

技術分享圖片

1
.Nginx的安裝 1.1.下載nginx 官網:http://nginx.org/ 1.2.上傳並解壓nginx tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/src
1.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源)
yum -y install gcc pcre-devel openssl openssl-devel

#編譯安裝(make是編譯。完成之後make install是安裝)
make 
&& make install 安裝完後測試是否正常: /usr/loca/nginx/sbin/nginx 查看端口是否有ngnix進程監聽 netstat -ntlp | grep 80
1.配置nginx
1.1.配置反向代理
1.修改nginx配置文件
server {
    listen       80;
    server_name  nginx-01.itcast.cn;    #nginx所在服務器的主機名,本機的主機名
#反向代理的配置
location / {             #攔截所有請求/開頭,
    root html;
        proxy_pass http:
//192.168.0.21:8080; # proxy_pass是走過通過,這裏是代理走向的目標服務器:tomcat(http://192.168.0.21:8080)。外面用戶訪問nginx的80端口都會走到tomcat的80端口。 } } 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

技術分享圖片

1.1.動靜分離(正則表達式)
#動態資源轉發給tomcat :  index.jsp
location ~ .*\.(jsp|do|action)$ {
    proxy_pass  http://tomcat-01.itcast.cn:8080;
}

#靜態資源nginx自己的服務器返回。
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
    expires 3d;
}
1.2.負載均衡(連接多臺tomcat)
在http這個節下面配置一個叫upstream的,後面的名字可以隨意取,但是要和location下的proxy_pass http://後的保持一致。
http {
    是在http裏面的, 已有http, 不是在server裏,在server外面
    upstream tomcats { 
        server 127.0.0.1:8080 weight=1;  #weight權重
        server 192.168.88.34:8080 weight=1;
        server shizhan04:8080 weight=1;
}
#卸載server裏,~表示大小寫敏感,.表示任意非回車字符,*表示任意,\.表示字符點,以jsp/do/action結尾。
location ~ .*\.(jsp|do|action) {
    proxy_pass http://tomcats;        #tomcats是後面的tomcat服務器組的邏輯組號
}
}
2.利用keepalived實現高可靠(多臺nginx)
2.1.高可靠概念
HA(High Available), 高可用性集群,是保證業務連續性的有效解決方案,一般有兩個或兩個以上的節點,且分為活動節點及備用節點。每臺nginx有一個keepalived。

技術分享圖片

2.2.高可靠軟件keepalived
keepalive是一款可以實現高可靠的軟件,通常部署在2臺服務器上,分為一主一備。Keepalived可以對本機上的進程進行檢測,一旦Master檢測出某個進程出現問題,將自己切換成Backup狀態,然後通知另外一個節點切換成Master狀態。
2.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
2.4.將keepalived添加到系統服務中(用service來啟動)
拷貝執行文件(keepalived是啟動腳本,拷貝到系統的sbin下面)
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
2.5.配置keepalived虛擬IP(2個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組,同一個組搶一個ip。
    priority 100   #主節點的優先級(1-254之間),備用節點必須比主節點優先級低
    advert_int 1  #組播信息發送間隔,兩個節點設置必須一樣,心跳,
    authentication {    #設置驗證信息,兩個節點必須一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {    #指定虛擬IP, 兩個節點設置必須一樣,2個nginx在哪個網段虛禮ip就在哪個網段,虛禮ip是對外的ip地址,24是子網掩碼255.255.255是3個8位的1就是24。
        192.168.33.60/24    #如果兩個nginx的ip分別是192.168.33.61和192.168.33.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 (ifconfig看的是第一個地址,多個地址用ip addr查看)。
Nginx死了那麽keepalived也要切換主備。
2.6.配置keepalived心跳檢查
原理:
Keepalived並不跟nginx耦合,它倆完全不是一家人
但是keepalived提供一個機制:讓用戶自定義一個shell腳本去檢測任何程序,腳本返回要檢測程序的進程狀態給keepalived就可以了。keepalived提供的就是換ip的功能。


#MASTER節點
global_defs {
}

vrrp_script chk_health {
    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"    //ps -ef | grep nginx是查找nginx進程,但是會出來grep進程自己本身,所以 grep -v grep去掉grep進程本身得到nginx進程, wc -l是統計有幾行,就是有幾個進程,至少2個進程, -ge 2是大於等於2,成功返回0不存在返回1.
    interval 1    #每隔1秒執行上述的腳本,去檢查用戶的程序ngnix
    weight -2    #如果檢測到本機的nginx死了,就自己權重-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"   //對方死了,我要變成master
    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"   //對方要我變成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    //把nginx起起來,即使是起來的再起一次也沒問題
        exit 0
    ;;
backup)
        /usr/local/nginx/sbin/nginx -s stop    //把自己nginx停掉
        /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

hadoop03---nginx+keepalived