综述:

浏览器访问虚拟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. Twisted 综述

    Twisted 框架概况 Twisted 是一个有着10多年历史的开源事件驱动框架.Twisted 支持很多协议,包括传输层的TCP.UDP.TLS,以及应用层的HTTP.FTP等.对所有这些协议,T ...

  2. superset docker 部署

    公众号原文有更多效果图哦 一.使用自己的数据库 1. 拉取项目 // 创建目录用于存放项目 mkdir -p /mnt/superset cd /mnt/superset git clone http ...

  3. 【转】PCA for opencv

    对于PCA,一直都是有个概念,没有实际使用过,今天终于实际使用了一把,发现PCA还是挺神奇的. 在OPENCV中使用PCA非常简单,只要几条语句就可以了. 1.初始化数据 //每一行表示一个样本 Cv ...

  4. An Intuitive Explanation of Convolutional Neural Networks

    https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ An Intuitive Explanation of Convolu ...

  5. Protobuf有没有比JSON快5倍?用代码来击破pb性能神话

    转 http://www.sohu.com/a/136487507_505779 2017-04-26 07:58 程序设计 /58 /技术 导读:Google 的 Protocol Buffers ...

  6. 初学redis,redis基本数据类型

    String: 1. set key value 2. get key 3. del key 4. strlen key 5. getset key value 修改键值对   6. getrange ...

  7. Linux-Mysql8.0

    Mysql8.0.12 基本操作 解释 命令 安装服务端 yum install mysql-community-server 启动 service mysqld start/restart 停止 s ...

  8. centos7.4进入单用户模式

    centos7.4进入单用户模式 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/b ...

  9. vue_music:封装scroll.vue组件

    在项目中经常用到滚动,结合Better-scroll封装了sroll.vue组件参考链接:https://ustbhuangyi.github.io...http://www.imooc.com/ar ...

  10. jQuery 淡入淡出有png图的时候 ie8下有黑色边框

    jQuery fadeTo 时ie8 png图片有黑色边框 往带有png图的样式里加 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader ...