综述:

浏览器访问虚拟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. ajax的序列化表单提交

    通过传统的 form 表单提交的方式上传文件 ? 1 2 3 4 <form id="uploadForm" action="" method=" ...

  2. checkbox点击选中,再点击取消,并显示在文本框中

    function checkItem(e,itemId) { var item = document.getElementById(itemId); var $items = $(item); if ...

  3. css3中的nth-child和nth-of-type的区别

    实例: 首先创建一个HTML结构 <div class="post"> <p>我是文章的第一段落</p> <p>我是文章的第二段落& ...

  4. 好久没写了,总结一下lnux常用的命令(基础)

    Linux 1.init 0 关机 2.init 6  重启 3.ls 列出当前目录下的文件 4.cd  切换目录  cd -  切换最近使用的两次目录 5.pwd 查看当前所在的路径 (“-”为用户 ...

  5. PHP CURL错误: error:140943FC

    使用PHP访问https网站的时候,间歇性会报error:140943FC错误.google之,通过如下方案可处理: 1.服务器ssl版本较高 curl_setopt($this->curl, ...

  6. python中字典的‘增、删、改、查’

    python 字典的'增.删.改.查' 用 ipython 运行情况如下: #新建字典 In [1]: infos = {'name':'dongge','age':18} In [2]: infos ...

  7. 关于host,nslookup,dig 的安装

    host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...

  8. POJ:2632-Crashing Robots

    Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Description In a modernized warehouse, robot ...

  9. hdu 4565

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  10. Python中的属性访问与描述符

    Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...