综述:

浏览器访问虚拟IP: 192.168.1.57, 该虚拟IP被Keepalived接管,两个Keepalived进程分别运行在物理IP为192.168.1.56和192.168.1.59服务器上,这两个服务器上都运行着Nginx server。Nginx server都监听虚拟IP 192.168.1.57. Nginx背后有一些web app集群,已经配置成upstream.

为了防止Nginx自己成为单点瓶颈,这里采用了双Nginx server的方式。每个Server都是Ubuntu 12.04.

假定我的第一台Ubuntu server物理IP地址是192.168.1.56,已经安装了Nginx server,现在安装Keepalived,后面称这台为master server.

  1. apt-get install keepalived

另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。

现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是

  1. CONFIG=/etc/keepalived/keepalived.conf

但是该文件现在还不存在。所以我创建一个,内容如下:

  1. # Settings for notifications
  2. global_defs {
  3. notification_email {
  4. csfreebird@gmail.com     # Email address for notifications
  5. }
  6. notification_email_from keepalived@your_company.com  # The from address for the notifications
  7. smtp_server 127.0.0.1
  8. smtp_connect_timeout 15
  9. }
  10. # Define the script used to check if haproxy is still working
  11. vrrp_script chk_http_port {
  12. script "/etc/keepalived/check_nginx.sh" # check Nginx is alive or not
  13. interval 2 #
  14. weight 2
  15. }
  16. # Configuation for the virtual interface
  17. vrrp_instance VI_1 {
  18. interface eth0
  19. state MASTER        # set this to BACKUP on the other machine
  20. priority 101        # set this to 100 on the other machine
  21. virtual_router_id 51
  22. smtp_alert          # Activate email notifications
  23. authentication {
  24. auth_type PASS
  25. auth_pass 1111      # Set this to some secret phrase
  26. }
  27. # The virtual ip address shared between the two loadbalancers
  28. virtual_ipaddress {
  29. 192.168.1.57
  30. }
  31. # Use the script above to check if we should fail over
  32. track_script {
  33. chk_http_port
  34. }
  35. }

说明:

1. smtp_server必须要用127.0.0.1

2. 自己要创建一个检查nginx进程的脚本

  1. /etc/keepalived/check_nginx.sh

内容如下:

  1. !/bin/bash
  2. # try to start nginx if nginx process is dead
  3. # shutdonw keepalived process if start nginx failed
  4. pid=`ps -C nginx --no-header |wc -l`
  5. if [ $pid -eq 0 ];then
  6. service nginx start
  7. sleep 3
  8. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  9. service keepalived stop
  10. fi
  11. fi

3. 修改nginx的所有server配置,将server_name都改为虚拟IP

  1. server_name  192.168.1.57;

4. 注意,这里的虚拟IP不是用修改/etc/network/interfaces的方式,而是在keepalived配置文件中直接设置,要想判断是否成功,直接ping 就行了,ifconfig是看不到的。

  1. ping 192.168.1.57
  2. PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.
  3. 64 bytes from 192.168.1.57: icmp_req=1 ttl=64 time=0.024 ms
  4. 64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms

对192.168.1.59做相同的配置,略有变化的是:

  1. vrrp_instance VI_1 {
  2. interface eth0
  3. state BACKUP  // changed
  4. priority 100  // changed

通过轮流关闭master和backup,证明keepalived已经有效工作了。

参考资料:Keepalived官方文档真是够差的。

http://www.leaseweblabs.com/2011/09/setting-up-keepalived-on-ubuntu-load-balancing-using-haproxy-on-ubuntu-part-2/

http://blog.csdn.net/zmj_88888888/article/details/8825471

用Keepalived搭建双Nginx server集群,防止单点故障的更多相关文章

  1. 基于keepalived搭建MySQL高可用集群

    MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,MMM,Heartbeat+DRBD,PXC,Galera Cluster 比较常用的是keepalived+双主,MHA和 ...

  2. 003.Keepalived搭建LVS高可用集群

    一 基础环境 1.1 IP规划 OS:CentOS 6.8 64位 节点类型 IP规划 主机名 类型 主 Director Server eth0:172.24.8.10 DR1 公共IP eth1: ...

  3. Window平台搭建Redis分布式缓存集群 (一)server搭建及性能測试

    百度定义:Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对很多其它.包含string(字符串).list(链表).set(集合).zset(sort ...

  4. spring boot 2.0.3+spring cloud (Finchley)1、搭建服务注册和发现组件Eureka 以及构建高可用Eureka Server集群

    一 .搭建Eureka 编写Eureka Server 由于有多个spring boot项目,采用maven多module的结构,项目结构如下: 新建一个maven主工程,在主maven的pom文件中 ...

  5. CanalAdmin搭建Canal Server集群

    CanalAdmin搭建Canal Server集群 一.背景 二.机器情况 三.实现步骤 1.下载canal admin 2.配置canalAdmin 3.初始化canal admin数据库 4.启 ...

  6. centos7.4安装高可用(haproxy+keepalived实现)kubernetes1.6.0集群(开启TLS认证)

    目录 目录 前言 集群详情 环境说明 安装前准备 提醒 一.创建TLS证书和秘钥 安装CFSSL 创建 CA (Certificate Authority) 创建 CA 配置文件 创建 CA 证书签名 ...

  7. 使用Kubeadm搭建高可用Kubernetes集群

    1.概述 Kubenetes集群的控制平面节点(即Master节点)由数据库服务(Etcd)+其他组件服务(Apiserver.Controller-manager.Scheduler...)组成. ...

  8. mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离

    数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了 ...

  9. 亿级Web系统搭建——单机到分布式集群

    当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...

随机推荐

  1. epoch,iteration,batch,batch_size

    epoch:训练时,所有训练图像通过网络训练一次​(一次前向传播+一次后向传播):测试时,所有测试图像通过网络一次​(一次前向传播).Caffe不用这个参数. batch_size:1个batch包含 ...

  2. 【转】瓜娃(guava)的API快速熟悉使用

    http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...

  3. mac层到ath9k层,ath9k层到硬件层

    如上图,整个 mac 层分成两个部分——UMAC 和 LMAC.LMAC 分成 MAC 下半部分和硬件抽象层. 硬件抽象层和ath9k层的连接 在hw.h中的函数struct ath_hw_ops() ...

  4. Java中的ArrayList类和LinkedList

    集合的体系: ----------| Collection 单列集合的根接口----------------| List 如果实现了List接口的集合类,具备的特点: 有序,可重复.--------- ...

  5. iOS开发之MVVM在项目中的应用

    今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  6. LaTeX中常用数学符号总结

    博主一些小小的总结,以后会继续更的. 某个传送门. ⎝⎛•‿•⎞⎠⎝⎛•‿•⎞⎠⎝⎛•‿•⎞⎠ 1.左右一个$: 1+1=2 $1+1=2$ ($3$及以后的都需要$) 2.左右两个$: 1+1=2 ...

  7. Spring Security 与 OAuth2(介绍)

    https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 OAuth2(介绍) 林塬 2018.01.23 11:14* 字数 3097 阅读 ...

  8. Redis错误解决:(error) MISCONF Redis is configured to save RDB snapshots

    刚开始学习使用redis数据库,在执行删除命令时,提示了我这么一个错误: 错误提示 (error) MISCONF Redis is configured to save RDB snapshots, ...

  9. Bootstrap 模态框 select2搜索框无法输入

    去掉模态框的div中的 tabindex="-1" 这个属性 <div class="modal fade" role="dialog" ...

  10. 【Python学习之七】面向对象高级编程——__slots__的使用

    1.Python中的属性和方法的绑定 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法. (1)首先,定义一个class:  class Stu ...