1.Keepalived+Nginx 高可用集羣 (主從模式)
集羣架構圖:
2.實驗環境
(1)需要兩臺 nginx 服務器
(2)需要 keepalived
(3)需要虛擬 ip
3.配置高可用的準備工作
(1)需要兩臺服務器 192.168.70.128 和 192.168.70.130
(2)在兩臺服務器安裝 nginx
(3)在兩臺服務器安裝 keepalived
3.1.準備兩臺測試服務器
192.168.70.128
192.168.70.130
3.2.在兩臺服務器安裝 nginx
192.168.70.128
192.168.70.130
3.3.在兩臺服務器安裝 keepalived
(1)使用 yum 命令進行安裝
yum install keepalived –y
(2)安裝之後,在etc 裏面生成目錄 keepalived,有文件 keepalived.conf
[root@centos2 init.d]# cd /etc/
[root@centos2 etc]# ls keep*
keepalived.conf
4 .完成高可用配置(主從配置)
(1)修改/etc/keepalived/keepalived.conf
192.168.70.128
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.70.128
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 備份服務器上將 MASTER 改爲 BACKUP
interface ens33 # 網卡
virtual_router_id 51 # 主、備機的 virtual_router_id 必須相同
priority 90 # 主、備機取不同的優先級,主機值較大,備份機值較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.70.135 # VRRP H 虛擬地址
}
}
192.168.70.130
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.70.130
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 備份服務器上將 MASTER 改爲 BACKUP
interface ens33 # 網卡
virtual_router_id 51 # 主、備機的 virtual_router_id 必須相同
priority 80 # 主、備機取不同的優先級,主機值較大,備份機值較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.70.135 # VRRP H 虛擬地址
}
}
(2 )在/usr/local/src添加腳本
192.168.70.128
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/opt/myapp/soft/tengine-2.1.0/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@centos2 src]# ls
nginx_check.sh
[root@centos2 src]# chmod +755 nginx_check.sh
[root@centos2 src]# ls
nginx_check.sh
192.168.70.130
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/opt/myapp/soft/tengine-2.1.0/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@centos3 sbin]# cd /usr/local/src
[root@centos3 src]# ls
[root@centos3 src]# vi nginx_check.sh
[root@centos3 src]# ls
nginx_check.sh
[root@centos3 src]# chmod +755 nginx_check.sh
[root@centos3 src]# ll
total 4
-rwxr-xr-x. 1 root root 194 Apr 13 01:04 nginx_check.sh
(3 )把兩臺服務器上 nginx 和 和 keepalived 啓動
啓動 nginx :
service nginx reload
啓動 keepalived :
systemctl start keepalived.service
5.最終測試
192.168.70.128
192.168.70.130
(1 )在瀏覽器地址欄輸入 虛擬 ip 地址 http://192.168.70.135/
(2 )把主服務器(192.168.70.128 )nginx 和 和 keepalived 停止,再輸入http://192.168.70.135/
[root@centos2 src]# service nginx stop
Stopping nginx (via systemctl): [ OK ]
[root@centos2 src]# systemctl stop keepalived.service
至此爲止,Nginx的高可用搭建完畢!