搜尋
首頁運維Nginxkeepalived+nginx高可用怎麼實現

keepalived+nginx高可用怎麼實現

May 11, 2023 pm 09:28 PM
nginxkeepalived

1.keepalived介紹

keepalived最初是專為lvs負載平衡軟體設計的,用來管理和監控lvs叢集系統中各個服務節點的狀態,後來又加入了實現高可用的vrrp功能。 keepalived除了能夠管理lvs軟體外,還能支援其他服務的高可用解決方案。

keepalived透過vrrp協定實現高可用功能的。 vrrp(virtual router redundancy protocol)虛擬路由冗餘協定。 vrrp出現的目的就是為了解決靜態路由單點故障問題,它能保證當個別節點宕機時,整個網路可以不間斷地運作。

2.keepalived高可用故障轉移原理

keepalived高可用服務之間的故障轉移,是透過vrrp來實現的。在keepalived服務工作時,主master節點會不斷地向備節點發送(多播的方式)心跳訊息,用來告訴備backup節點自己還活著。

  當主節點發生故障時,無法給備節點發送心跳訊息,如果備節點無法繼續偵測到來自主節點的心跳。就會呼叫自身的接管程序,接管主節點的ip資源和服務。當主節點恢復時,備節點又會釋放主節點故障時自身接管的ip資源和服務,恢復到原來的備用角色

##3.安裝nginx

3.1.主節點(192.168.80.22)

3.1.1.安裝編譯工具與函式庫檔案

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

3.1.2.安裝pcre

#进入目录
cd /usr/local/develop/anginx

#上传安装文件并解压
tar -zxvf pcre-8.38.tar.gz

#进入安装目录
cd pcre-8.38

#检查配置
./configure

#编译、安装
make && make install

#查看pcre版本
pcre-config --version

3.1.3.安裝nginx

#进入目录
cd /usr/local/develop/anginx

#上传安装文件,并解压
tar -zxvf nginx-1.8.1.tar.gz

#进入安装目录
cd nginx-1.8.1

#检查配置
./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38

#编译安装
make && make install

#查看nginx版本
 /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

#配置nginx(检查)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t

#nginx管理命令
/usr/local/develop/anginx/webserver/nginx/sbin/nginx       # 启动 nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop       # 停止 nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload      # 重新载入配置文件
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen      # 重启 nginx

3.1.4.nginx基礎設定

vi nginx.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid    logs/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    mime.types;
  default_type application/octet-stream;

  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;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  #gzip on;
  
  #添加tomcat列表,真实应用服务器都放在这
  upstream tomcat_pool{
    #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
    server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
    server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s;
    
  }

  server {
    listen    80;
    server_name tomcat_pool;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
      #root  html;
      #index index.html index.htm;
      proxy_pass http://tomcat_pool;  #转向tomcat处理
      proxy_set_header  host       $host;
      proxy_set_header  x-real-ip    $remote_addr;
      proxy_set_header  x-forwarded-for $proxy_add_x_forwarded_for;
    }

    #error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }


}

3.2.備節點(192.168.80.21)

說明:安裝方式同nginx主節點。

4.安裝keepalived

4.1.主節點(192.168.80.22)

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------
! configuration file for keepalived

global_defs {
  notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
  }
  notification_email_from alexandre.cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb01
}

vrrp_instance vi_1 {
  state master
  interface eth1
  virtual_router_id 55
  priority 150
  advert_int 1
  authentication {
    auth_type pass
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
...........................................................

關於設定說明:

  •  【router_id】 是一個路由標識,在一個區域網路裡面應該是唯一的

    • 【vrrp_instance vi_1】{...}這是一個vrrp實例,裡面定義了keepalived的主備狀態、介面、優先權、認證和ip資訊

    • 【state】 定義了vrrp的角色

    • 【interface】定義使用的接口,這裡我的伺服器用的網卡都是eth1

    • 【virtual_router_id】是虛擬路由id標識,一組的keepalived配置中主備都是設定一致

    • 【priority】是優先權,數字越大,優先權越大,

    • 【auth_type】是認證方式

    • 【auth_pass】是認證的密碼

  • 【virtual_ipaddress】 {...}定義虛擬ip位址,可以配置多個ip位址,這裡我定義為192.168.80.100,綁定了eth1的網路接口,虛擬接口eth1:1

##4.2.備節點(192.168.80.21 )

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------------
! configuration file for keepalived

global_defs {
  notification_email {
   acassen@firewall.loc
   failover@firewall.loc
   sysadmin@firewall.loc
  }
  notification_email_from alexandre.cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id lb02
}

vrrp_instance vi_1 {
  state backup
  interface eth1
  virtual_router_id 55
  priority 100
  advert_int 1
  authentication {
    auth_type pass
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
.............................................................

5.測試

#5.1.啟動主備節點的keepalived服務

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived start
-------------------------------------
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15788   1 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15790 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15791 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15807 15622 0 01:33 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#


#在节点二执行(192.168.80.21)
/etc/init.d/keepalived start
---------------------------------------
[root@hadoop01 ~]# ps -ef |grep keepalived
root   11542   1 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   11544 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   11545 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   11550 11512 0 01:33 pts/1  00:00:00 grep keepalived
[root@hadoop01 ~]#

#5.2.透過虛擬ip存取服務

http://192.168.80.100/session-redis-demo/

keepalived+nginx高可用怎麼實現5.3.停止主節點keepalived服務

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived stop

#观察备节点变化
ip addr
-------------------------------------------
[root@hadoop01 ~]# ip addr
1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host 
    valid_lft forever preferred_lft forever
2: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000
  link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 ~]#

5.4.繼續透過虛擬ip存取服務

http://192.168.80.100/session-redis-demo/

keepalived+nginx高可用怎麼實現

6.keepalived nginx整合

#說明:編寫nginx守護腳本,如果nginx服務出現故障,則停止目前節點的keepalived服務。自動切換到備用節點。

6.1.寫nginx守護腳本

vi nginx_check.sh

--------------------------------------
#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
  /etc/init.d/keepalived stop
fi
sleep 2
done

#给脚本授权
chmod u+x nginx_check.sh

#执行脚本
nohup /usr/local/develop/anginx/shell/nginx_check.sh &

6.2.停止主節點nginx服務

#停止主节点nginx服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop

#查找进程
[root@hadoop02 ~]# ps -ef |grep nginx
root   15915   1 0 01:51 ?    00:00:00 /bin/bash /usr/local/develop/anginx/shell/nginx_check.sh
root   16516 15753 0 01:54 pts/5  00:00:00 grep nginx
[root@hadoop02 ~]#

#观察备用节点变化【服务正常】
ip addr
--------------------------------------
[root@hadoop01 shell]# ip addr
1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host 
    valid_lft forever preferred_lft forever
2: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000
  link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 shell]#

#再次重新启动主节点nginx和keepalived服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx

/etc/init.d/keepalived start

以上是keepalived+nginx高可用怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
NGINX的優點:速度,效率和控制NGINX的優點:速度,效率和控制May 12, 2025 am 12:13 AM

NGINX受歡迎的原因是其在速度、效率和控制方面的優勢。 1)速度:採用異步、非阻塞處理,支持高並發連接,靜態文件服務能力強。 2)效率:內存使用低,負載均衡功能強大。 3)控制:通過靈活的配置文件管理行為,模塊化設計便於擴展。

NGINX與Apache:社區,支持和資源NGINX與Apache:社區,支持和資源May 11, 2025 am 12:19 AM

NGINX和Apache在社區、支持和資源方面的差異如下:1.NGINX的社區雖然規模較小,但活躍度和專業性高,官方支持通過NGINXPlus提供高級功能和專業服務。 2.Apache擁有龐大且活躍的社區,官方支持主要通過豐富的文檔和社區資源提供。

NGINX單元:應用程序服務器簡介NGINX單元:應用程序服務器簡介May 10, 2025 am 12:17 AM

NGINXUnit是一個開源的應用服務器,支持多種編程語言和框架,如Python、PHP、Java、Go等。 1.它支持動態配置,可以在不重啟服務器的情況下調整應用配置。 2.NGINXUnit支持多語言應用,簡化了多語言環境的管理。 3.通過配置文件,可以輕鬆部署和管理應用,如運行Python和PHP應用。 4.它還支持高級配置,如路由和負載均衡,幫助管理和擴展應用。

使用NGINX:優化網站性能和可靠性使用NGINX:優化網站性能和可靠性May 09, 2025 am 12:19 AM

NGINX可通过以下方式提升网站性能和可靠性:1.作为Web服务器处理静态内容;2.作为反向代理服务器转发请求;3.作为负载均衡器分配请求;4.作为缓存服务器减轻后端压力。通过配置优化如启用Gzip压缩和调整连接池,NGINX能显著提高网站性能。

NGINX的目的:服務Web內容等NGINX的目的:服務Web內容等May 08, 2025 am 12:07 AM

nginxserveswebcontentandactsasareverseproxy,loadBalancer和more.1)效率高效的servesstaticContentLikeHtmlandImages.2)itfunctionsasareverseproxybalancer,and andginxenhanceperforfforfforfforfforfforffrenfcaching.4)

NGINX單元:簡化應用程序部署NGINX單元:簡化應用程序部署May 07, 2025 am 12:08 AM

NGINXUnit通過動態配置和多語言支持簡化應用部署。 1)動態配置無需重啟服務器即可修改。 2)支持多種編程語言,如Python、PHP、Java。 3)採用異步非阻塞I/O模型,提升高並發處理性能。

NGINX的影響:Web服務器及其他NGINX的影響:Web服務器及其他May 06, 2025 am 12:05 AM

NGINX起初解決C10K問題,現已發展為處理負載均衡、反向代理和API網關的全能選手。 1)它以事件驅動和非阻塞架構聞名,適合高並發。 2)NGINX可作為HTTP和反向代理服務器,支持IMAP/POP3。3)其工作原理基於事件驅動和異步I/O模型,提升了性能。 4)基本用法包括配置虛擬主機和負載均衡,高級用法涉及復雜負載均衡和緩存策略。 5)常見錯誤包括配置語法錯誤和權限問題,調試技巧包括使用nginx-t命令和stub_status模塊。 6)性能優化建議包括調整worker參數、使用gzip壓縮和

NGINX故障排除:診斷和解決常見錯誤NGINX故障排除:診斷和解決常見錯誤May 05, 2025 am 12:09 AM

Nginx常見錯誤的診斷與解決方法包括:1.查看日誌文件,2.調整配置文件,3.優化性能。通過分析日誌、調整超時設置和優化緩存及負載均衡,可以有效解決404、502、504等錯誤,提高網站穩定性和性能。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具