1. 程式人生 > >nginx+keepalived部署與安裝

nginx+keepalived部署與安裝

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
  1. 配置nginx
    4.1. 配置反向代理
    1.修改nginx配置檔案
    server {
        listen       80;
        server_name  nginx-01.xiaoniu.cn;
    #172.16.203.101/hello.html
    location / {
        root html;
            proxy_pass http://192.168.0.21:8080;
        }
    }
    2.啟動tomcat-01上的tomcat

3.啟動nginx-01上的nginx

./nginx

4.2. 動靜分離
#動態資源 index.jsp

location ~ .*\.(jsp|do|action)$ {
    proxy_pass http://tomcat-01.xiaoniu.cn:8080;
}

#靜態資源

location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
    expires 3d;
}

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

http {
    upstream tomcats { 
        server 172.16.203.20:8080 weight=1;
        server tomcat-02.xiaoniu.cn:8080 weight=1;
        server tomcat-02.xiaoniu.cn:8080 weight=1;
}
location ~ .*\.(jsp|do|action) {
    proxy_pass http://tomcats;
}
}

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.3.6.tar.gz -C /usr/local/src

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

cd /usr/local/src/keepalived-1.3.6

開始configure

./configure

#編譯並安裝

make && make install

5.4. 將keepalived新增到系統服務中
拷貝執行檔案

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

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

cp /usr/local/src/keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

將keepalived檔案拷貝到etc下

cp /usr/local/src/keepalived-1.3.6/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 

建立keepalived資料夾

mkdir -p /etc/keepalived

將keepalived配置檔案拷貝到etc下

cp /usr/local/src/keepalived-1.3.6/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

新增可執行許可權

chmod +x /etc/init.d/keepalived

新增keepalived到開機啟動

chkconfig --add keepalived	
chkconfig keepalived on

5.5. 配置keepalived虛擬IP

#MASTER節點

global_defs {
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.10/24
    }
}

#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 {
        10.0.0.10/24
    }
}

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

service keepalived start

5.6. 配置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
    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