1. Nginx安装

(1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2

(2) 下载官网最新稳定版,地址:https://nginx.org/en/download.html,本教程使用的是1.14.2版

(3) 安装Nginx的依赖包

a. 源码编译使用gcc: yum -y install ggc-c++

b. 正则表达式解析时使用pcre:yum -y install pcre pcre-devel

c. 压缩时使用的zlib:yum -y install zlib zlib-devel

d. openssl: yum -y install openssl openssl-devel

(4) 解压:tar –zxvf nginx-1.14.2.tar.gz; cd nginx-1.14.2

(5) 配置并编译:./configure –prefix=/usr/local/nginx && make && make install

(6) 启动nginx: cd /usr/local/nginx/sbin/; ./nginx

(7) 关闭nginx: ./nginx -s stop

(8) 重新加载配置文件:./nginx -s reload

(9) 设置开机自启:在/etc/rc.local中增加一行"/usr/local/nginx/sbin/nginx",并增加执行权限 chmod 755 /etc/rc.local

2. Keepalived安装配置

(1) 环境准备:安装服务器与nginx相同,vip分别为192.138.86.3和192.138.86.4

(2) 安装依赖:yum -y install libnl libnl-devel libnfnetlink-devel

(3) 官网下载稳定版的Keepalived,地址:http://www.keepalived.org/download.html,本文下载的是2.0.10

(4) 解压缩:tar -zxvf keepalived-2.0.10.tar.gz

(5) 配置并编译:./configure –prefix=/usr/local/keepalived && make && make install

(6) keepalived注册为系统服务:

a. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d/

b. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

c. cp /usr/local/src/keepalived-2.0.10/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

(7) keepalived启动:service keepalvied start|stop|restart

(8) 86.1上的配置文件/etc/keepalived/keepalived.conf的内容如下:

! Configuration File for keepalived

global_defs {
notification_email {
test@example.com
}
notification_email_from research@nagios3.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id NGINX_DEVEL
} vrrp_script chk_http_port {
script "/usr/local/nginx/monitor_nginx.sh"
interval
weight -
fall
rise
} vrrp_instance VI_1 {
state MASTER
interface em1
virtual_router_id
priority
advert_int
mcast_src_ip 192.138.86.1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.138.86.3
}
track_interface {
em1
}
track_script {
chk_http_port
}
} vrrp_instance VI_2 {
state BACKUP
interface em1
virtual_router_id
priority
advert_int
mcast_src_ip 192.138.86.1
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.138.86.4
}
track_interface {
em1
}
track_script {
chk_http_port
}
}

(9) 86.2上的配置文件/etc/keepalived/keepalived.conf的内容如下:

! Configuration File for keepalived

global_defs {
notification_email {
test@example.com
}
notification_email_from research@nagios3.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id NGINX_DEVEL
} vrrp_script chk_http_port {
script "/usr/local/nginx/monitor_nginx.sh"
interval
weight -
fall
rise
} vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id
priority
advert_int
mcast_src_ip 192.138.86.2
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.138.86.3
}
track_interface {
em1
}
track_script {
chk_http_port
}
} vrrp_instance VI_2 {
state MASTER
interface em1
virtual_router_id
priority
advert_int
mcast_src_ip 192.138.86.2
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.138.86.4
}
track_interface {
em1
}
track_script {
chk_http_port
}
}

说明:

a. 1和2上分别有虚拟ip3和4

b. 1和2上的keepalived基于vip互为主从

c. vrrp_script中的内容为nginx的心跳检测,其内容为:

 #!/bin/bash

 DATE=$(date)
LogName=$(date +"%Y%m%d")
A=`ps -C nginx --no-header |wc -l`
logs=/usr/local/nginx/logs/nginx_check.$LogName.logs if [ $A -eq 0 ];then
cd /usr/local/nginx
echo "$DATE keepalived monitor start nginx" >> $logs
sudo ./sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
echo "$DATE killall keepalived" >> $logs
sudo killall keepalived
fi
else
echo "$DATE keepalived monitornginx is ok" >> $logs
f

(10) 重启keepalived,然后尝试关闭主keepalived和nginx验证配置是否生效

Nginx+Keepalived配置的更多相关文章

  1. nginx:负载均衡实战(四)nginx+keepalived配置双机热备

    1.下载安装 下载keepalived地址:http://www.keepalived.org/download.html 解压安装: tar -zxvf keepalived-.tar.gz 安装o ...

  2. nginx;keepalived配置出现主主的解决方法(脑裂问题)

    1.查看日志 tail -f /var/log/messages 发现master和backup机都是mastaer模式启动的 通过查看别人的经历,发现VRRP基于报文实现的.master设置一定时间 ...

  3. Nginx+Keepalived 主备高可用 安装与配置

    环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10. ...

  4. Nginx +keepalived

    Nginx +keepalived   配置高可用的Nginx 准备环境: 节点node17,node18   lftp 172.16.0.1:/pub/Sources/6.x86_64/nginx ...

  5. Keepalived+Nginx+Tomcat配置高可用负载均衡系统示例

    前言 此示例为keepalived+nginx+tomcat的基础配置示例,某些特定配置此例中不会出现,在示例中会用到三个虚拟机:两个纯命令行用于模拟服务端配置,一个带桌面环境的用于模拟客户端访问,这 ...

  6. nginx+keepalived+tomcat之具体配置档

    前沿知识点: nginx负责负载均衡(反向代理) msm(memcached session manager)负责缓存会话信息,从而实现会话保持 所需包: nginx和memcached采用最新稳定版 ...

  7. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...

  8. 使用Keepalived配置主从热备实现Nginx高可用(HA)

    Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务 ...

  9. centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课

    centos   LB负载均衡集群 三种模式区别 LVS/NAT 配置  LVS/DR 配置  LVS/DR + keepalived配置  nginx ip_hash 实现长连接  LVS是四层LB ...

随机推荐

  1. 【TJOI2017】异或和

    题目描述 在加里敦中学的小明最近爱上了数学竞赛,很多数学竞赛的题目都是与序列的连续和相关的.所以对于一个序列,求出它们所有的连续和来说,小明觉得十分的简单.但今天小明遇到了一个序列和的难题,这个题目不 ...

  2. CPU 的工作原理

    内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1]控制 ...

  3. P2561 [AHOI2002]黑白瓷砖

    $ \color{#0066ff}{ 题目描述 }$ \(\color{#0066ff}{输入格式}\) 文件中以一行的形式存放一个正整数 n , n ≤ 20 . \(\color{#0066ff} ...

  4. 【离散数学】 SDUT OJ 谁是作案嫌疑人?

    谁是作案嫌疑人? Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 刑侦大队对涉及六个嫌 ...

  5. 剩下的树 THU 机试

    链接:https://www.nowcoder.com/questionTerminal/f5787c69f5cf41499ba4706bc93700a2来源:牛客网 有一个长度为整数L(1<= ...

  6. JS 为任意元素添加任意事件的兼容代码

    为元素绑定事件(DOM):有两种 addEventListener 和 attachEvent:   相同点: 都可以为元素绑定事件 不同点: 1.方法名不一样 2.参数个数不一样addEventLi ...

  7. 教你搭建SpringSecurity3框架(附源码)

    源码下载地址:http://pan.baidu.com/s/1qWsgIg0 一.web.xml <?xml version="1.0" encoding="UTF ...

  8. Logistic Regression-Cost Fuction

    1. 二分类问题 样本:  ,训练样本包含  个: 其中  ,表示样本 包含 个特征:  ,目标值属于0.1分类: 训练数据:  输入神经网络时样本数据的形状: 目标数据的形状: 2. logisti ...

  9. HDU - 1300 简单DP

    题意:买珠子的方案有两种,要么单独买,价钱为该种类数量+10乘上相应价格,要么多个种类的数量相加再+10乘上相应最高贵的价格买 坑点:排序会WA,喵喵喵? 为什么连续取就是dp的可行方案?我猜的.. ...

  10. i2c设备驱动之设备地址

    第一步:查找设备的数据手册可得到设备的从机地址.读写地址. 很不巧的是我在这里又卡了近一天,由于自己的硬件知识学得相当的那啥,哎,没办法,怨不得别人. 今天终于开窍了!!!!! 在开始条件(S)后,发 ...