1.keepalived介绍

  keepalived最初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了实现高可用的VRRP功能。keepalived除了能够管理LVS软件外,还能支持其他服务的高可用解决方案。

  keepalived通过VRRP协议实现高可用功能的。VRRP(Virtual Router Redundancy Protocol)虚拟路由冗余协议。VRRP出现的目的就是为了解决静态路由单点故障问题,它能保证当个别节点宕机时,整个网络可以不间断地运行。

2.keepalived高可用故障转移原理

  keepalived高可用服务之间的故障转移,是通过VRRP来实现的。在keepalived服务工作时,主Master节点会不断地向备节点发送(多播的方式)心跳消息,用来告诉备Backup节点自己还活着。

  当主节点发生故障时,无法给备节点发送心跳消息,如果备节点无法继续检测到来自主节点的心跳。就会调用自身的接管程序,接管主节点的IP资源和服务。当主节点恢复时,备节点又会释放主节点故障时自身接管的IP资源和服务,恢复到原来的备用角色

3.安装nginx

3.1.主节点(192.168.80.22)

3.1.1.安装编译工具和库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

3.1.2.安装pcre

#进入目录
cd /usr/local/develop/anginx #上传安装文件并解压
tar -zxvf pcre-8.38.tar.gz #进入安装目录
cd pcre-8.38 #检查配置
./configure #编译、安装
make && make install #查看pcre版本
pcre-config --version

3.1.3.安装nginx

#进入目录
cd /usr/local/develop/anginx

#上传安装文件,并解压
tar -zxvf nginx-1.8.1.tar.gz

#进入安装目录
cd nginx-1.8.1

#检查配置
./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38

#编译安装
make && make install

#查看nginx版本
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

#配置nginx(检查)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t

#nginx管理命令
/usr/local/develop/anginx/webserver/nginx/sbin/nginx # 启动 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
 

3.1.4.nginx基础配置

vi nginx.conf

#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on; #添加tomcat列表,真实应用服务器都放在这
upstream tomcat_pool{
#server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s; }

server {
listen 80;
server_name tomcat_pool;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#root html;
#index index.html index.htm;
proxy_pass http://tomcat_pool; #转向tomcat处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}


}

3.2.备节点(192.168.80.21)

说明:安装方式同nginx主节点。

4.安装keepalived

4.1.主节点(192.168.80.22)

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb01
}

vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 55
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass server123
}
virtual_ipaddress {
192.168.80.100 dev eth1 label eth1:1
}
}
...........................................................

关于配置说明

 【router_id】 是路由标识,在一个局域网里面应该是唯一的

 【vrrp_instance VI_1】{...}这是一个VRRP实例,里面定义了keepalived的主备状态、接口、优先级、认证和IP信息
【state】 定义了VRRP的角色
【interface】定义使用的接口,这里我的服务器用的网卡都是eth1
【virtual_router_id】是虚拟路由ID标识,一组的keepalived配置中主备都是设置一致
【priority】是优先级,数字越大,优先级越大,
【auth_type】是认证方式
【auth_pass】是认证的密码 【virtual_ipaddress】 {...}定义虚拟IP地址,可以配置多个IP地址,这里我定义为192.168.80.100,绑定了eth1的网络接口,虚拟接口eth1:1

4.2.备节点(192.168.80.21)

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -D
root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------------
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb02
}

vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass server123
}
virtual_ipaddress {
192.168.80.100 dev eth1 label eth1:1
}
}
.............................................................

5.测试

5.1.启动主备节点的keepalived服务

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived start
-------------------------------------
[root@hadoop02 anginx]# ps -ef |grep keepalived
root 15788 1 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15790 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15791 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -D
root 15807 15622 0 01:33 pts/1 00:00:00 grep keepalived
[root@hadoop02 anginx]#


#在节点二执行(192.168.80.21)
/etc/init.d/keepalived start
---------------------------------------
[root@hadoop01 ~]# ps -ef |grep keepalived
root 11542 1 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11544 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11545 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -D
root 11550 11512 0 01:33 pts/1 00:00:00 grep keepalived
[root@hadoop01 ~]#

5.2.通过虚ip访问服务

http://192.168.80.100/session-redis-demo/

5.3.停止主节点keepalived服务

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived stop

#观察备节点变化
ip addr
-------------------------------------------
[root@hadoop01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
inet 192.168.80.100/32 scope global eth1:1
inet6 fe80::250:56ff:fe38:e546/64 scope link
valid_lft forever preferred_lft forever
[root@hadoop01 ~]#

5.4.继续通过虚ip访问服务

http://192.168.80.100/session-redis-demo/

6.keepalived+nginx整合

说明:编写nginx守护脚本,如果nginx服务出现故障,则停止当前节点的keepalived服务。自动切换到备用节点。

6.1.编写nginx守护脚本

vi nginx_check.sh

--------------------------------------
#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
/etc/init.d/keepalived stop
fi
sleep 2
done

#给脚本授权
chmod u+x nginx_check.sh

#执行脚本
nohup /usr/local/develop/anginx/shell/nginx_check.sh &
 

6.2.停止主节点nginx服务

#停止主节点nginx服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop

#查找进程
[root@hadoop02 ~]# ps -ef |grep nginx
root 15915 1 0 01:51 ? 00:00:00 /bin/bash /usr/local/develop/anginx/shell/nginx_check.sh
root 16516 15753 0 01:54 pts/5 00:00:00 grep nginx
[root@hadoop02 ~]#

#观察备用节点变化【服务正常】
ip addr
--------------------------------------
[root@hadoop01 shell]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff
inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1
inet 192.168.80.100/32 scope global eth1:1
inet6 fe80::250:56ff:fe38:e546/64 scope link
valid_lft forever preferred_lft forever
[root@hadoop01 shell]#

#再次重新启动主节点nginx和keepalived服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx

/etc/init.d/keepalived start


keepalived+nginx高可用实现的更多相关文章

  1. Nginx(四):Keepalived+Nginx 高可用集群

    Keepalived+Nginx 高可用集群 (主从模式) 集群架构图 安装keepalived [root@localhost ~]# yum install -y keepalived 查看状态 ...

  2. Keepalived+Nginx高可用实例

    Keepalived+Nginx高可用实例 注意事项: 1.VIP不需要在服务器网络配置文件中配置. 2.nginx主不可用时,需要kill掉nginx主的keepalived服务,这样才可以实现VI ...

  3. LVS+KeepAlived+Nginx高可用实现方案

    文章目录概念LVSKeepAlived为什么要使用准备软件安装KeepAlived 安装源码安装yum安装服务启动.重启.关闭安装ipvsadmnginx安装防火墙(iptables)防火墙配置(方式 ...

  4. Keepalived+nginx高可用

    这种方法会把Keepalived进程结束掉,在教育机构学习到的方法,我个人对这种方法不认可. 参考: https://www.cnblogs.com/gshelldon/p/14504236.html ...

  5. Keepalived+Nginx高可用集群

    Keepalived简介 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替 ...

  6. keepalived+nginx 高可用集群

    一.什么是高可用?   nginx做负载均衡,能达到分发请求的目的,但是不能很好的避免单点故障. 1.nginx集群单点问题 分发器宕机怎么处理? 假如nginx服务器挂掉了,那么所有的服务也会跟着瘫 ...

  7. centos安装与配置keepalived+nginx高可用

    一.安装启动keepalived 1.下载keepalived 在centos系统中,用wget下载最新版本: mkdir tool cd tool wget https://www.keepaliv ...

  8. linux下实现keepalived+nginx高可用

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

  9. keepalived+nginx高可用负载均衡环境搭建

    上篇说道keepalived的环境搭建,本来keepalived结合lvs更有优势,但是也可以结合nginx来使用.下面接着说下nginx的环境搭建 环境信息: nginx(master)  192. ...

随机推荐

  1. The most widely used name server software: BIND

    https://www.isc.org/downloads/bind/ The most widely used name server software: BIND BIND is open sou ...

  2. Flume-ng-sdk源码分析

    Flume 实战(2)--Flume-ng-sdk源码分析 - mumuxinfei - 博客园 http://www.cnblogs.com/mumuxinfei/p/3823266.html

  3. (linux)初学者学linux块设备驱动

      面对不断升级的linux内核.GNU开发工具.linux环境下的各种图形库,很多linux应用程序开发人员和linux设备驱动开发人员即兴奋,又烦躁.兴奋的是新的软件软件.工具给我提供了更强大的功 ...

  4. Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  5. POJ3376 Finding Palindromes —— 扩展KMP + Trie树

    题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS   Memory Limit: 262 ...

  6. Hibernate写hql语句与不写hql语句的区别?

    写hql语句与不写hql语句的区别? 写hql语句:书写HQL语句,所有的查询与投影的设计均使用HQL语句完成. 不写hql语句:没有任何查询语句,所有的查询与投影的设计使用面向对象格式完成. 二者选 ...

  7. codeforces 460A Vasya and Socks 解题报告

    题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...

  8. Redis和StackExchange.Redis

    redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库以整数表示的号码.redisDb.dict 存储着该 ...

  9. Eclipse慢慢学会的快捷键

    Java编辑器 添加单个import Ctrl+Shift+M Java编辑器 组织多个import Ctrl+Shift+O Ctrl+M切换窗口的大小 Ctrl+D删除当前行 ---------- ...

  10. iOS 开发图片资源选择png格式还是jpg格式

    对于iOS本地应用程序来说最简单的答案就是始终使用PNG,除非你有非常非常好的理由不用它. 当iOS应用构建的时候,Xcode会通过一种方式优化.png文件而不会优化其它文件格式.它优化得相当的好 他 ...