第一章 原理
原理:
VIP 是外網訪問的IP地址,通過keepalived設置,以及VRRP將VIP綁定到主機和備機上,通過權重實現控制。當主機掛掉後,keepalived釋放對主機的控制,備機接管VIP。
Nginx+keepalived單主環境下,其中一臺slave機器處於備份狀態,有些浪費,其實可以利用DNS輪詢來實現雙機負載均衡。
第二章 安裝前準備
1.環境配置
1.Ip配置
操作系統:CentOS 6.5
Nginx+keepalived 1服務器 IP:192.168.1.244
Nginx+keepalived 2服務器 IP:192.168.1.245
訪問VIP1: 192.168.1.248
訪問VIP2: 192.168.1.249
2.需要軟件包
zlib-static-1.2.3-29.el6.x86_64.rpm
keepalived-1.2.12.tar.gz
ngx_cache_purge-2.3.tar.gz
nginx-1.9.7.tar.gz
Nginx nginx啓動腳本
check_http.sh keepalived監控nginx進程的腳本,並自殺
2.安裝前準備
1..關閉防火牆
[root@localhost ~]# service iptables stop
[root@localhost ~]#chkconfig iptables off
改完後確認
[root@localhost ~]# service iptables status
Iptables: 未運行防火牆
2..關閉selinux
修改/etc/selinux/config 文件,將SElINUX=enforcing改爲SElINUX=disabled或SElINUX=permissive
# vi /etc/selinux/config
……
SElINUX=disabled
……
3.重啓
[root@localhost ~]# reboot
4.配置本地yum
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mkdir repo.bak
[root@localhost yum.repos.d]# cp CentOS-* repo.bak/
[root@localhost yum.repos.d]# rm -rf CentOS-*
[root@localhost yum.repos.d]# vi aa.repo
[aa]
name=aa
baseurl=file:///mnt
gpgcheck=1
[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-*
5.掛載
[root@localhost ~]# mount /dev/dvd /mnt
第三章 安裝
1.安裝rpm包
[root@localhost ~]# rpm -ivh zlib-static-1.2.3-29.el6.x86_64.rpm
2.yum安裝包
[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel gcc gcc-c++ make wget zlib-static zlib zlib-devel
3.安裝keepalived
tar xf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure --prefix=/usr/local/keepalived
make
make install
4.拷貝文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
設置開機啓動
chkconfig keepalived on
5.配置keepalived
1.配置244
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_1 運行keepalived機器的一個標識
}
vrrp_script chk_nginx {
script "/tmp/check_http.sh"
interval 2
weight 2
} 故障時執行的腳本
vrrp_instance VI_1 { 監控多個網段的實例
state BACKUP 指定那個爲master,那個爲backup,主備考priority決定
interface eth0 設置實例綁定的網卡
virtual_router_id 51 VPID標記
priority 99 優先級,高優先級競選爲master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
} 注意只有backup配置這三行
virtual_ipaddress {
192.168.1.248 dev eth0 label eth0:1 訪問vip1
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.249 dev eth0 label eth0:2 訪問vip2
}
}
2.配置245
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGIXN_2
}
vrrp_script chk_nginx {
script "/tmp/check_http.sh"
interval 2
weight 2
}
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 {
192.168.1.248 dev eth0 label eth0:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.249 dev eth0 label eth0:2
}
}
6.上傳check_http.sh
將check_http.sh腳本通過ftp傳輸到到文件夾/tmp
賦予腳本執check_http.sh行權限
[root@localhost tmp]# chmod +x check_http.sh
第四章 nginx安裝及緩存配置
1.Nginx安裝
上傳軟件包到/root
[root@localhost~]#mkdir -p /usr/local/nginx
[root@localhost~]#tar zxvf ngx_cache_purge-2.3.tar.gz
[root@localhost~]#tar zxvf nginx-1.9.7.tar.gz
[root@localhost~]#cd nginx-1.9.7
[root@localhost nginx-1.9.7]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --add-module=../ngx_cache_purge-2.3加入緩存模塊
[root@localhost nginx-1.9.7]#make
[root@localhost nginx-1.9.7]#make install
[root@localhost nginx-1.9.7]#cd /usr/local/nginx
啓動nginx
[root@loaclhost nginx]#./sbin/nginx
查看nginx啓動是否成功
[root@localhost nginx]#./sbin/nginx-t
重新載入
[root@localhost nginx]#./sbin/nginx -s reload
查看nginx進程
[root@localhost nginx]#ps -ef | grep nginx
2.Nginx配置文件
cd /usr/local/nginx/
vi conf/nginx.conf
user nobody;
#nginx 運行的用戶
worker_processes auto;
#啓動進程,通常設置成和cpu的總核心數量相等,auto自動檢測
#error_log logs/error.log;
#error_log logs/error.log notice;
error_loglogs/error.log info;
#打開定義錯誤日誌的路徑及其日誌級別
pid logs/nginx.pid;
#nginx 的進程
worker_rlimit_nofile 65535;
#文件句柄數,和系統單進程打開的文件數相同,不必理會進程個數
events{
worker_connections 65535;定義的是單個進程的連接數,該值受系統進程打開文件數限制,需要修改打開的文件句柄數,但是max_client = worker_proxesses X work_connextions
use epoll; #linux最常用支持大併發的事件觸發機制
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http{
include mime.types;
default_type application/octet-stream;
#服務器名字的hash表大小
server_names_hash_bucket_size 128;
#指定來自客戶端請求頭的headerbuffer大小,設置爲32KB
client_header_buffer_size 32k;
#指定客戶端請求中較大的消息頭的緩存最大數量和大小,這裏是4個32KB
large_client_header_buffers 4 32k;
#用來設置允許客戶端請求的最大的單個文件字節數
client_max_body_size 356m;
#客戶端請求主體讀取緩存
client_body_buffer_size 32k;
#緩衝區代理緩衝用戶端請求的最大字節數
proxy_connect_timeout 10;
#nginx跟後端服務器連接超時時間(代理連接超時)
proxy_read_timeout 180;
#連接成功後,後端服務器響應時間(代理接收超時)
proxy_send_timeout 5;
#後端服務器數據回傳時間(代理髮送超時)
proxy_buffer_size 16k;
#設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 4 64k;
#proxy_buffers緩衝區,網頁平均在32k以下的設置
proxy_temp_file_write_size 256k;
#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
proxy_busy_buffers_size 128k;
#高負荷下緩衝大小(proxy_buffers*2)
open_file_cache max=102400 inactive=20s;
#這個將爲打開文件指定緩存,默認是沒有啓用的,max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求後刪除緩存。
open_file_cache_valid 30s;
#這個是指多長時間檢查一次緩存的有效信息。
open_file_cache_min_uses 1;
#open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個文件在inactive時間內一次沒被使用,它將被移除。
#配置緩存目錄
proxy_temp_path /usr/local/nginx/temp;
#proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#設置內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。
#日誌的格式
log_format access.log '$remote_addr-[$time_local]-"$request"-"$http_user_agent"' ;
#log_format main'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#開啓高效模式文件傳輸模式,將tcp_nopush和tcp_nodely兩個指另設置爲on,用於防止網絡阻塞
keepalive_timeout 60;給客戶端分配keep-alive鏈接超時時間。服務器將在這個超時時間過後關閉鏈接。我們將它設置低些可以讓ngnix持續工作的時間更長
server_tokens off;
# 關閉錯誤時的nginx 的版本顯示
#gzip on;
配置負載均衡服務器列表
upstream heren-master{
ip_hash;
server 192.168.1.240:8051 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;
}
#注weight設置的權重,max_fails設置的是健康監測失敗次數,timeout設置超時時間
upstream heren-message{
ip_hash;
server 192.168.1.240:9999 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;
}
upstream heren-report{
ip_hash;
server 192.168.1.240:8081 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;
}
upstream heren-schedule{
ip_hash;
server 192.168.1.240:8061 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.241:8051 weight=1 max_fails=2 fail_timeout=30s;
}
配置主機
server{
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
配置業務訪問url
location /{
root html;
#index index.html index.htm; 增加#
}
配置緩存規則
location ~.*\.(gif|jpg|jpeg|png|bmp|sqf|js|css)$
#設置擴展名以gif .jpg .css 等結尾的靜態文件緩存
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#如果後端的服務器返回502 504 執行超時等錯誤,自動將請求轉發到upstream 負責均衡池中的另外一臺服務器,實現故障轉移
proxy_cache cache_one;
#進行緩存,使用web緩存去cache_one
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#對不同的HTTP 狀態碼設置不同的緩存時間
proxy_set_header Accept-Encoding "none";
# proxy_set_header Accept-Encoding ""; 同上面一項相同
proxy_ignore_headers "Cache-Control" "Expires";
#設定proxy_set_header Accept-Encoding 或者是後臺服務器關閉gzip,該臺機器纔不會緩存被壓縮的文件,造成亂碼#設置proxy_cache 支持後臺設定的expires , 即使支持HTTP頭信息定義的緩存規則
proxy_pass http://heren-master;
expires 1h;
#設置過去的時間
}
#設置js|css在瀏覽器中過期的時間
location ~.*\.(js|css)
{
expires 1h;
}
#設置清除所有的cache
location ~/purge(/.*)
{
allow 127.0.0.1;
allow 192.168.244.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}
}
3.nginx配置靜態頁面
cd /usr/local/html
mkdir heren
上傳4個jar包到主節點和備節點heren文件夾下,解壓
4.上傳nginx腳本.
將nginx啓動腳本通過ftp傳輸到到文件夾/etc/init.d/
賦予nginx腳本執行權限
[root@localhost~]# cd /etc/init.d
[root@localhost init.d]# ll
如若出現上面情況,執行下面命令
[root@localhost init.d]# cp nginx?ű? nginx
[root@localhost init.d]# rm -rf nginx?ű?
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# ll
設置nginx開機啓動
chkconfig nginx on
第五章 測試
1.啓動keepalived和nginx
查看244
[root@localhost ~]# service nginx start
[root@localhost ~]# /etc/init.d/keepalived start
[root@localhost ~]# ip a
查看245
[root@localhost ~]# service nginx start
[root@localhost ~]# /etc/init.d/keepalived start
[root@localhost ~]# ip a
2.停掉244的nginx
查看244
查看245
3.啓動244的nginx和keepalived
查看244
查看245