應用場景
對于一個大型網站來說,負載均衡是永恒的話題,顧名思義,負載均衡即是将負載分攤到不同的服務單元,既保證服務的可用性,又保證響應足夠快,給使用者很好的體驗。随着硬體技術的迅猛發展,越來越多的負載均衡硬體裝置湧現出來,如F5 BIG-IP、Citrix NetScaler、Radware等等,雖然可以解決問題,但其高昂的價格卻往往令人望而卻步,是以負載均衡軟體仍然是大部分公司的不二之選。Nginx作為webserver的後起之秀,其優秀的反向代理功能和靈活的負載均衡政策受到了業界廣泛的關注。
Nginx程序基于Master + Slave(worker)多程序模型,自身具有非常穩定的子程序管理功能。在Master程序配置設定模式下,Master程序永遠不進行業務處理,隻是進行任務分發,進而達到Master程序的存活高可靠性。
Keepalived是Linux下面實作VRRP 備份路由的高可靠性運作件。基于Keepalived設計的服務模式能夠真正做到主伺服器和備份伺服器故障時IP瞬間無縫交接。二者結合,可以構架出比較穩定的軟體負載均衡方案。
操作步驟
1. Nginx安裝配置
1.1 系統基礎配置
關閉selinux
# vi /etc/selinux/config
……
SELINUX=disabled
……
關閉防火牆,在Centos6.5中
# service iptables stop
# chkconfig iptables off
關閉防火牆,在Centos7中
# systemctl stop firewalld
# systemctl disable firewalld
修改系統檔案打開限制數量,增加在配置檔案最後
# vi /etc/security/limits.conf
* soft noproc 65535
* hard noproc 65535
* soft nofile 409600
* hard nofile 409600
重新開機機器
# reboot
1.2 設定時間同步
安裝ntpdate , ntpd
# yum install -y ntpdate ntp pcre-devel pcre
複制時區至本地時區
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
時間同步,ip請改成可用的時間伺服器的ip位址,并寫入時間戳
# ntpdate ip
# hwclock -w
開啟ntpd服務,在Centos6中
# service start ntpd
# service enable ntpd
開啟ntpd服務,在Centos7中
# systemctl start ntpd
# systemctl enable ntpd
1.3 安裝軟體包
安裝包下載下傳位址
http://pan.baidu.com/s/1hrUHlOw
安裝依賴元件
# yum install zlib zlib-devel
# yum install pcre-devel
将安裝包中的檔案拷貝到伺服器,并解壓縮安裝包
# tar -zxvf nginx-1.8.1.tar.gz
然後進入目錄進行安裝
# cd nginx-1.8.1
#./configure --prefix=/opt/nginx1.8.1
# make
# make install
# ln -sf /opt/nginx1.8.1 /usr/local/nginx
# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile && source /etc/profile
1.4 配置服務
建立啟動腳本
将啟動腳本檔案夾中的nginx檔案發送到伺服器中并
# cp nginx /etc/init.d/
# chmod +x /etc/init.d/nginx
修改使用者權限
# useradd -r -M nginx
# mkdir -p /var/log/nginx
# chown nginx -R /var/log/nginx
注冊啟動服務
# chmod a+x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
1.5 啟動服務
# service nginx start
2. 配置高可用
注:在主備兩台Nginx伺服器上都要安裝Keepalived。
2.1 安裝Keepalived
安裝依賴包,将keepalived6(Centos6環境中) 或 keepalived7(Centos7中)傳送到伺服器
# tar zxvf keepalived6.tar.gz 或tar zxvf keepalived7.tar.gz
# yum localinstall keepalived/*.rpm -y
将軟體源中的keepalived-1.2.16.tar.gz傳送到伺服器并解壓
# tar zxvf keepalived-1.2.16.tar.gz
編譯安裝
# cd keepalived-1.2.16
# ./configure
# make
# make install
配置服務
# cp /usr/local/sbin/keepalived /usr/sbin/
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# chmod +x /etc/init.d/keepalived
# chkconfig --add keepalived
# chkconfig keepalived on
# mkdir /etc/keepalived
2.2 配置主伺服器
建立配置檔案
# vi /etc/keepalived/keepalived.conf
修改配置檔案并儲存
! Configuration File for keepalived
global_defs {
notification_email {
#[email protected]
}
#notification_email_from [email protected]
#smtp_server smtp.example.com
#smtp_connect_timeout 30
router_id nginx_master
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80" #監控本地Nginx端口
interval 1
weight -10
}
vrrp_instance VI_1 {
state MASTER #主伺服器
interface eth0 #通信網卡,根據實際配置
virtual_router_id 51 #路由辨別,同網段内不可沖突且需與備用伺服器一緻
priority 100 #優先級,0-254
advert_int 1
authentication {
auth_type PASS
auth_pass qwe123!@#
}
virtual_ipaddress {
192.168.202.200 #虛拟IP,可定義多個
}
track_script {
chk_http_port
}
}
啟動服務
# service keepalived start
2.3 配置備用伺服器
建立配置檔案
# vi /etc/keepalived/keepalived.conf
修改配置檔案并儲存
! Configuration File for keepalived
global_defs {
notification_email {
#[email protected]
}
#notification_email_from [email protected]
#smtp_server smtp.example.com
#smtp_connect_timeout 30
router_id nginx_backup
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80" #監控本地Nginx端口
interval 1 #執行間隔
weight -10 #執行失敗,服務優先級-10
}
vrrp_instance VI_1 {
state BACKUP #備用伺服器
interface eth0 #通信網卡,根據實際配置
virtual_router_id 51 #路由辨別,需與主伺服器一緻,同網段内不可沖突
priority 99 #優先級,比主伺服器要低,0-254
advert_int 1
authentication {
auth_type PASS
auth_pass qwe123!@#
}
virtual_ipaddress {
192.168.202.200 #虛拟IP,可定義多個
}
track_script {
chk_http_port
}
}
啟動服務
# service keepalived start
2.4 安裝驗證
浏覽器通路http://ip出現以下提示說明nginx正常運作
檢視keepalived綁定虛拟ip的情況
# ip a