综述:

浏览器访问虚拟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. python基础一 day15 作业

    3.处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕def check_file(filename,aim): with open(filename,encoding= ...

  2. c#中的里氏转换和Java中强制类型转换在多态中的应用

    在c#中: 注意: 子类并没有继承父类的构造函数,而是会默认调用父类那个无参数的构造函数. 如果一个子类继承了一个父类,那么这个子类除了可以使用自己的成员外,还可以使用从父类那里继承过来的成员.但是父 ...

  3. Apache超时配置

    Apache超时配置 1. KeepAliveTimeout 语法 KeepAliveTimeout seconds 默认 5 上下文 server config, virtual host 说明 服 ...

  4. k8s的pod生命周期

    pod的生命周期: 1.init container 2.main contianer (1) post start hook :容器启动后做什么操作(可以命令查看kubectl explain po ...

  5. Linux基础学习-网络管理

    Linux系统网络管理NetworkManager 1 启动网络管理服务和开机自启动 在rhel7中网路管理相关命令nmcli,nmtui,nmtui-edit,nm-connection-edito ...

  6. charles 模拟手机弱网、修改请求参数、修改返回值

    1.charles模拟弱网(断网) 2.charles修改请求参数 (1)先访问一次需要改的请求,在charles上找到相应的请求地址 (2)然后在需要打断点的请求上右键,勾选[Breakpoints ...

  7. python入门:输出1-10的所有数(自写)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数(自写) """ 导入time库,给kaishi赋值为数字 ...

  8. 数字内置方法详解(int/long/float/complex)

    一.常用方法 1.1.int 以下是Python2.7的int内置函数: 序号 函数名 作用 举例 1 int.bit_length() 二进制存储这个整数至少需要多少bit(位). >> ...

  9. 20181207(sys,shelve,logging)

    一.logging模块 logging专门用来记录日志 日志的级别分为五级,可以用数字表示,从低到高分别为: import  logginglogging.info('info')   #10logg ...

  10. unix cc编译过程

    1.编译并链接一个完全包含与一个源文件的C程序:                cc program.c     这条命令产生一个称为a.out的可执行程序.中间会产生一个名为program.o的目标 ...