keepalived架设简单高可用的nginx的web服务器 ----那些你不知道的秘密

简介:

keepalived架设简单高可用的nginx的web服务器----那些你不知道的秘密

如果負載均衡軟件不使用LVS的話,那麼keepalived的配置是相當的簡單的,只需要配置好MASTER和SLAVE的vrrp相關配置就可以了,後端realServer的偵測就可以交給負載均衡軟件去做,比如我使用的負載均衡軟件是haproxy,個人認為比LVS更高效、更省資源、配置更簡單、條理更清晰。

下面來說說我在配置keepalived和nginx過程中遇到的一些問題,之所以會出現很多問題,關鍵就在於keepalived沒有配置文件查錯的機制,不管你的配置文件寫成了啥樣,你照樣可以成功啟動keepalived,它不會給你任務錯誤提示,就像它沒有出錯一樣,你查看進程時也不會覺得它異常,和普通的正常的進程一模一樣,因此一旦配置文件配置錯誤就很要命了。

nginx的配置就沒啥好說的了,重點講講keepalived。

1,环境

test2为主ngin服务器,test3为辅的nginx服务器

1
2
3
4
test2 eth2: 192.168 . 46.132
192.168 . 46.132
test3 eth2: 192.168 . 46.133
192.168 . 46.133


2、安裝:1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
如果你使用LVS作為負載均衡器的話,需要讓編譯程序能找到你的linux內核所在目錄,要帶 -- with -kernel-dir=/usr/src/linux參數編譯,因此你需要先安裝內核開發包:
with -kernel-dir=/usr/src/linux參數編譯,因此你需要先安裝內核開發包:
yum install kernel-devel
這是CENTOS的安裝方法。
接著為內核源文件目錄做一個軟鏈接:
ln -s /usr/src/kernels/`uname -r` /usr/src/linux
現在可以開始編譯了:
./configure --prefix=/usr/local/keepalived -- with -kernel-dir=/usr/src/linux
with -kernel-dir=/usr/src/linux
如果不使用LVS,就不需要帶-- with -kernel-dir參數,我用的是nginx,因此沒有指定此參數。
with -kernel-dir參數,我用的是nginx,因此沒有指定此參數。
configure結束,開始make時,出现了以下錯誤:
/usr/ include /stdint.h: 41 : error: conflicting types  for ‘int64_t’
include /stdint.h: 41 : error: conflicting types  for
include /stdint.h: 41 : error: conflicting types  for ‘int64_t’
/usr/src/linux/ include /linux/types.h: 126 : error: previous declaration of ‘int64_t’ was here
include /linux/types.h: 126 : error: previous declaration of ‘int64_t’ was here
/usr/ include /stdint.h: 56 : error: conflicting types  for ‘uint64_t’
include /stdint.h: 56 : error: conflicting types  for
include /stdint.h: 56 : error: conflicting types  for ‘uint64_t’
/usr/src/linux/ include /linux/types.h: 124 : error: previous declaration of ‘uint64_t’ was here
include /linux/types.h: 124 : error: previous declaration of ‘uint64_t’ was here
In file included from /usr/ include /stdlib.h: 438 ,
include /stdlib.h: 438 ,
在源目錄裡找到keepalived/libipvs- 2.6 /ip_vs.h文件,編輯它,把 # include <linux/types.h> 移动到 # include <sys/types.h> 的下面即可,再次make正常,make install安裝完畢。
2.6 /ip_vs.h文件,編輯它,把 # include
<linux/types.h> 移动到 # include
2.6 /ip_vs.h文件,編輯它,把 # include <linux/types.h> 移动到 # include <sys/types.h> 的下面即可,再次make正常,make install安裝完畢。


3、配置

3.1主服务器test2的keepalived的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server  127.0 . 0.1
127.0 . 0.1
smtp_connect_timeout  30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script  "/root/nginx_pid.sh"
interval  2
weight  2
}
vrrp_instance VI_1 {
state MASTER
interfaceeth2
virtual_router_id  51
mcast_src_ip  192.168 . 46.132
192.168 . 46.132
priority  100
advert_int  1
authentication {
auth_type PASS
auth_pass  1111
}
virtual_ipaddress {
192.168 . 46.200 / 24
}
track_script {
chk_http_port
}
}

3.2辅助nginx服务器keepalived的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@test3 init.d]# vim /etc/keepalived/keepalived.conf
!Configuration File forkeepalived
forkeepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server  127.0 . 0.1
127.0 . 0.1
smtp_connect_timeout  30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script  "/root/nginx_pid.sh"
interval  2
weight  2
}
vrrp_instance VI_1 {
state BACKUP
interfaceeth2
virtual_router_id  51
mcast_src_ip  192.168 . 46.133
192.168 . 46.133
priority  99
advert_int  1
authentication {
auth_type PASS
auth_pass  1111
}
virtual_ipaddress {
192.168 . 46.200 / 24
}
track_script {
chk_http_port
}
}
~


4,监控nginx的脚本

chk_http_port腳本內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test2 init.d]# vim /root/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq  0 ];then
[ $A -eq  0
if [ $A -eq  0 ];then
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
sleep  3
if [ `ps -C nginx --no-header |wc -l` -eq  0 ];then
[ `ps -C nginx --no-header |wc -l` -eq  0
if [ `ps -C nginx --no-header |wc -l` -eq  0 ];then
killall keepalived
fi
fi


5,注意事項:

1、第一行!開頭表示是一行註釋,keepalived配置文件的註釋行都是以!或#開頭的。
2、vrrp_scriptchk_http_port是指定一個vrrp檢測腳本,當切換到MASTER時,自動執行腳本中指定的程序, 要特別注意的是vrrp_scriptchk_http_port與後面跟著的{之間要至少留有一個空格,否則腳本不會執行,我為了這個問題糾結了2小時!keepalived的配置文件中所有的{都要與之前的字符至少間隔一個空格位置。就因為keepalived不檢測任何配置問題,因此如果不知道的話就相當的杯具了。。
3、track_script{,這是執行vrrp_script指令所指定的腳本,要注意的是它必須放在virtual_ipaddress配置的后面,意思就是只有當VIP正常啟動生效後才能執行track_script中指定的腳本,其原因是我的負載均衡器使用的是haproxy,在nginx配置文件中設置的綁定IP就是這個VIP,而我的檢測腳本chk_http_port就是檢測nginx狀態並根據條件啟動它,因此如果track_script放到前面了就會發生綁定的VIP還沒有生效就試圖啟動nginx,這當然是不可能成功的了,再看chk_http_port內容--當找不到nginx進程時,就停止keepalived,這樣就導致了nginx啟動不起來,連累了keepalived也被停掉,MASTER被SLAVE接管,要命的是SLAVE上的配置和MASTER是一樣的,結果就是不管MASTER還是SLAVE,VIP都無法啟動。這個問題也讓我糾結了4個多小時。。根據這個經驗, 一切檢測任務的配置都最好寫在VIP啟動配置的後面,而不要寫到前面!
4、從機keepalived進程啟動時,chk_http_port腳本會執行失敗,這是因為主機佔用了VIP,從機上的nginx得不到這個IP而啟動失敗,當從機接管MASTER時,雖然VIP飄過來了,但chk_http_port是不會執行的,因此nginx不會啟動,這個原因我想應該是vrrp_script所定義的腳本只是在keepalived啟動時才執行的吧,雖然名字是以vrrp開頭。因此最好先讓從機接管MASTER,將VIP飄過來,再手動啟動nginx,這樣不管MASTER飄到主機還是從機,都能正常執行負載均衡任務了。


本文转自陈仲阳0 51CTO博客,原文链接:http://blog.51cto.com/wolfword/1304037
相关文章
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
26 0
|
1月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
164 0
|
30天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
50 0
|
8天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
1月前
|
消息中间件 存储 Kafka
Kafka【环境搭建 02】kafka_2.11-2.4.1 基于 zookeeper 搭建高可用伪集群(一台服务器实现三个节点的 Kafka 集群)
【2月更文挑战第19天】Kafka【环境搭建 02】kafka_2.11-2.4.1 基于 zookeeper 搭建高可用伪集群(一台服务器实现三个节点的 Kafka 集群)
140 1
|
1月前
|
弹性计算 算法 应用服务中间件
倚天使用|Nginx性能高27%,性价比1.5倍,基于阿里云倚天ECS的Web server实践
倚天710构建的ECS产品,基于云原生独立物理核、大cache,结合CIPU新架构,倚天ECS在Nginx场景下,具备强大的性能优势。相对典型x86,Http长连接场景性能收益27%,开启gzip压缩时性能收益达到74%。 同时阿里云G8y实例售价比G7实例低23%,是Web Server最佳选择。
|
1月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
57 2
|
1月前
|
机器学习/深度学习 Python
Python基础:构建一个简单的Web服务器
Python基础:构建一个简单的Web服务器
57 1
|
1月前
|
应用服务中间件 Apache nginx
web后端-web服务器对比
web后端-web服务器对比
|
1月前
|
SQL JSON 应用服务中间件
web后端-win-phpMySql-nginx-web发布
web后端-win-phpMySql-nginx-web发布