基于keepalived實現高可用nginx服務
環境及配置前提說明
主機1,ip:192.168.25.140 運行web服務
主機2,ip:192.168.25.141 運行web服務
主機3,ip:192.168.25.138 運行nginx服務和keepalived服務
主機4,ip:192.168.25.139 運行nginx服務和keepalived服務
1兩臺nginx主機編譯安裝nginx服務
# 下載源碼包 ]# yum install -y make ]# yum install -y gcc ]# yum -y groupinstall "Development Tools" "Server Platfrom Development" ]# yum install pcre-devel openssl-devel zlib-devel -y 手動解決最重要的依賴關系包 ]# useradd -r nginx 增加一個系統用戶 ]# tar -xf nginx-1.10.0.tar.gz ]# cd nginx-1.10.0/ nginx-1.10.0]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/va/log/nginx.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio ]# make && make install ]# vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin:$PATH ]# . /etc/profile.d/nginx.sh ]# nginx -t 檢測錯誤 ]# nginx -h 可查看命令使用幫助 啟動服務,檢測端口是否已經啟用 ]# /usr/local/nginx/sbin/nginx 啟動服務 ]# ss -tnlp 查看80端口是否已經啟用,注意,如果httpd服務已經啟用了話 這里會啟動失敗
2兩臺nginx主機安裝keepalived服務
]# yum install keepalived -y
2.1為兩臺keepalived主機編輯郵件通知腳本
]# vim notify.sh #!/bin/bash # contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
2.2將腳本移動至/etc/keepalived/目錄下,并且給腳本執行權限
]# mv notify.sh /etc/keepalived/ ]# chmod +x notify.sh
3配置nginx的反代配置文件
]# vim /etc/nginx/nginx.conf 編輯主配置文件,在http上下文編輯upstream server,如下;
4配置兩臺keepalived節點主機配置文件
]# vim keepalived.conf,詳細配置如下;
4.1節點一配置
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 172.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_script chk_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -5 } vrrp_script chk_nginx vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass cb77b8da } virtual_ipaddress { 192.168.25.142/32 dev eth0 #nginx外網地址 } track_script { chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
4.2節點二配置
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 172.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_script chk_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -5 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 98 advert_int 1 authentication { auth_type PASS auth_pass cb77b8da } virtual_ipaddress { 192.168.25.142/32 dev eth0 #nginx外網地址 } track_script { chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
5為兩臺RS web服務器編輯主頁以便進行測試
]# vim /var/www/html/index.html <h1>RS 1</h1> #RS1的主頁 <h1>RS 2</h1> #RS2的主頁
6進行訪問測試
原創文章,作者:M20-1馬星,如若轉載,請注明出處:http://www.www58058.com/58159