nginx+keepalived 高可用主从配置详解

2025-05-26 0 18

本文介绍了nginx+keepalived 高可用主从配置详解,分享给大家,具体如下:

一、系统环境及软件版本

CentOS 6.6 x64
keepalived-1.2.18.tar.gz
nginx-1.6.2.tar.gz

主服务器:192.168.38.64

从服务器:192.168.38.66

VIP :192.168.38.100

二、nginx安装 (主从安装一致)

1.安装依赖环境

复制代码 代码如下:

yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-developenssl openssl-devel
  

2.上传nginx到 opt 目录

3.解压安装

?

1

2

3

4

5
# tar -zxvf nginx-1.6.2.tar.gz

# cd nginx-1.6.2

# ./configure --prefix=/opt/nginx (prefix=/opt/nginx 这个指定的是 nginx目录)

# make && make install

4.修改 nginx监听端口 及 index.html

# vi /opt/nginx/conf/nginx.conf

nginx+keepalived 高可用主从配置详解

vi /opt/nginx/html/index.html

nginx+keepalived 高可用主从配置详解

5. nginx 启动及常用命令

配置测试: /opt/nginx/sbin/nginx -t 出现如下界面说明配置没问题

nginx+keepalived 高可用主从配置详解

启动 : /opt/nginx/sbin/nginx

重启 : /opt/nginx/sbin/nginx -s reload

停止 : /opt/nginx/sbin/nginx -s stop

6.开机启动 nginx

vi /etc/rc.local

加入: /opt/nginx/sbin/nginx

7.修改防火墙开放端口

vi /etc/sysconfig/iptables

添加 : -A INPUT -p tcp -m state –state NEW -m tcp –dport 8888 -j ACCEPT

重启防火墙 : service iptables restart

8.问题

启动 nginx遇到的问题

nginx+keepalived 高可用主从配置详解

vi /etc/ld.so.conf

添加: /opt/nginx/lib/

9.nginx的负载均衡

nginx的负载均衡主要是 由upstream 这一模块完成

修改 nginx的配置文件

vi /data/nginx/conf/nginx.conf

添加如下内容:(web_pools 这个名称可变)

?

1

2

3

4
upstream web_pools {

server 10.0.6.108:7080weight=1;

server 10.0.0.85:8980weight=1;

}

将server节点下的location节点中的proxy_pass配置为:http:// + upstream名称即可

结果如下:

nginx+keepalived 高可用主从配置详解

其中 weight是权重 backup是备用服务器 ,只有其它服务器宕机后,备用服务器才会启动。

三、keepalived 安装

1.keepalived上传到 opt目录下

2.解压 安装  

?

1

2

3

4
tar -zxvf keepalived-1.2.18.tar.gz

cd keepalived-1.2.18

./configure --prefix=/opt/keepalived

make && make install

3.将keepalived 安装成 linux服务

?

1

2

3

4
cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

  cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

  ln -s /opt/sbin/keepalived /usr/sbin/

  ln -s /opt/keepalived/sbin/keepalived /sbin/

4.设置 keepalived 服务开机启动

?

1
chkconfig keepalived on

5.修改 Keepalived 配置文件

vi /etc/keepalived/keepalived.conf

?

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

39

40
! 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 LVS_01 #这个配置要唯一

} 

vrrp_script chk_nginx {

  script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径

  interval 2 ## 检测时间间隔

  weight -20 ## 如果条件成立,权重-20

} 

vrrp_instance VI_1 { #实例 VI_1 名字可以随意 但是不建议修改

  state MASTER # 主服务器MASTER 从服务器 BACKUP

  interface em1 # em1 网卡

  virtual_router_id 51 #virtual_router_id 主备要一致

  priority 100   # 优先级 数字越大 优先级越高 priority 的值 主服务器要大于 从服务器

  advert_int 1  #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒

  authentication { # 主从通信 验证类型及密码

    auth_type PASS  #设置vrrp验证类型,主要有PASS和AH两种

    auth_pass 1111  #设置vrrp验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信

  } 

  ## 将 track_script 块加入 instance 配置块

  track_script {

    chk_nginx ## 执行 Nginx 监控的服务

  }

  virtual_ipaddress {

  192.168.38.100/24 #VRRP HA 虚拟地址 如果有多个VIP,继续换行填写

  }

} 

6.编写 Nginx 状态检测脚本

vi /etc/keepalived/nginx_check.sh

内容如下:

?

1

2

3

4

5

6

7

8

9

10

11
#!/bin/bash

A=`ps -C nginx –no-header |wc -l`

if [ $A -eq 0 ];then

  /opt/nginx/sbin/nginx

  sleep 2

  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

    killall keepalived

  fi

fi

保存后,给脚本赋执行权限:chmod +x/etc/keepalived/nginx_check.sh

7.注意点:Keepalived主从配置文件不同点

  a.router_id 不一致

  b.state 主服务器是MASTER ,从服务器是 BACKUP

  c.priority 主服务器 大于 从服务器

8.keepalived 命令

  启动 : servicekeepalived start

  停止: servicekeepalived stop

  重启: servicekeepalived restart

9.注意的问题

a.vip没绑定成功

解决方案:ip addr 查看 本地ip所在网卡的名称 ,然后修改 配置文件

nginx+keepalived 高可用主从配置详解

vi /etc/keepalived/keepalived.conf

nginx+keepalived 高可用主从配置详解

保存后 servicekeepalived restart 重启 keepalived服务即可

10.测试

启动主从nginx和keepalived 服务

主从服务器分别: ip add | grep 192.168.38.100

在192.168.38.64 可看到

nginx+keepalived 高可用主从配置详解

同时 在192.168.38.66

nginx+keepalived 高可用主从配置详解

当杀死 主服务器上的keepalived 则 从服务器

nginx+keepalived 高可用主从配置详解

当再次启动主服务器上的keepalived则结果主服务器上有结果,从服务器上没结果。

当杀死 nginx后,keepalived则会自动启动 nginx服务

11. keepalived脑裂 (ip add | grep 192.168.38.100 在主从都有结果)

解决方案:防火墙问题

?

1

2

3

4

5
iptables-IINPUT4-pvrrp-jACCEPT  

service iptables save

service iptables restart

nginx+keepalived 高可用主从配置详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/liulangzhizi/p/7050260.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 nginx+keepalived 高可用主从配置详解 https://www.kuaiidc.com/53495.html

相关文章

发表评论
暂无评论