文章目錄
說明:
- 環境爲centos 6.6
- nginx軟件爲 nginx-1.6.2.tar.gz
一、安裝nginx
### 一定要配置好yum
# 1.6.1安裝nginx所需的pcre庫
yum install pcre pcre-devel -y
rpm -qa pcre pcre-devel
# 1.6.2安裝openssl openssl-devel以及zlib程序
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel
# 1.6.3 創建nginx賬號
useradd -s /sbin/nologin -M nginx
# 安裝
tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module && make && make install
echo $?
# 1.6.5指定軟連接
ln -s /application/nginx-1.6.2/ /application/nginx
# 檢查nginx的配置文件的語法
/application/nginx/sbin/nginx -t
# 1.將nginx的路徑加入系統默認的搜索路徑並寫入登錄腳本
echo 'PATH=$PATH:/application/nginx/sbin' >>/etc/profile
# 重新加載系統腳本
. /etc/profile
# 1.7 測試訪問nginx的網站
# 1方法1
# curl -I 127.0.0.1
# 方法2
# wget 127.0.0.1
二、nginx負載均衡配合keepalived服務案例實戰
0. 拓撲
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-TC6CfZUx-1586523244533)(C:\Users\root\AppData\Roaming\Typora\typora-user-images\image-20200410180900979.png)]
角色 | ip地址 | 軟件安裝 |
---|---|---|
lb01 | 192.168.1.11 | keepalived、nginx |
lb02 | 192.168.1.12 | keepalived、nginx |
web01 | 192.168.1.13 | apache |
web02 | 192.168.1.14 | apache |
1. 在192.168.1.11和192.168.1.12上配置nginx的代理服務器功能,配置如下
1.11 nginx配置
cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools {
server 192.168.1.13:80 weight=1;
server 192.168.1.14:80 weight=1;
}
server {
listen 192.168.1.113:80;
server_name www.hdxy.com;
location / {
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
1.12 nginx配置
cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools {
server 192.168.1.13:80 weight=1;
server 192.168.1.14:80 weight=1;
}
server {
listen 192.168.1.113:80;
server_name www.hdxy.com;
location / {
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
2. 在192.168.1.11 和192.168.1.12 上配置keepalived服務
( 首先用 yum -y install keepalived
安裝keepalived)
1.11 keepalived配置
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL01
}
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.113 dev eth0 label eth0:1 # 注意本機網卡
}
}
1.12 keepalived配置
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 # 注意本機網卡
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.113 dev eth0 label eth0:1 # 注意本機網卡
}
}
3. 啓動nginx和keepalived服務
/application/nginx/sbin/nginx
/etc/init.d/keepalived start
4. 解決服務監聽網卡上不存在ip地址問題
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
sysctl -p
5. yum安裝1.13 和 1.14的apache
yum -y install httpd
6. 配置首頁
echo "wo shi 192.168.1.13 " >/var/www/html/index.html
echo "wo shi 192.168.1.14 " >/var/www/html/index.html
7. 啓動httpd服務
/etc/init.d/httpd start
8. 在windows上測試
# 先關閉防火牆 和 setenforce
service iptables stop
setenforce 0
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-85qQqhvc-1586523244535)(C:\Users\root\AppData\Roaming\Typora\typora-user-images\image-20200410203441155.png)]
9. 將主用lb的服務關閉
/etc/init.d/keepalived stop # 停掉 keepalived
ip addr|grep 192.168.1.113 # 查看ip信息
#inet 192.168.1.113/32 scope global eth0:1
發現備用lb可以正常接管
10. 實驗到此結束 !
問題:
正常情況下,keepalived軟件僅僅在對方機器宕機或keepalived停掉的時候纔會接管業務,但在實際中,有一種情況是,nginx反向代理停掉,而keepalived服務還在工作的情況,這個問題會導致用戶訪問的vip無法找到對應的服務,如何解決這個問題呢?
-
可以寫守護程序 當nginx業務有問題的時候,就停掉本地的keepalived服務,實現備用lb的自動接管
#!/bin/bash # file name check_nginx.sh while true do if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ] ;then /etc/init.d/keepalived stop fi sleep 2 done
-
在後臺運行腳本
sh check_nginx.sh &
-
模擬nginx業務掛掉
/application/nginx/sbin/nginx -s stop ip addr|grep 192.168.1.113 #inet 192.168.1.113/32 scope global eth0:1
-
發現ip地址漂移到備用lb了
ived stop
fi
sleep 2
done
- 在後臺運行腳本
```shell
sh check_nginx.sh &
-
模擬nginx業務掛掉
/application/nginx/sbin/nginx -s stop ip addr|grep 192.168.1.113 #inet 192.168.1.113/32 scope global eth0:1
-
發現ip地址漂移到備用lb了