转自  https://www.jianshu.com/p/da26df4f7d60

Keepalived+Nginx实现高可用Web负载均衡

Master  192.168.0.69    nginx、keepalived    Centos7.
backup 192.168.0.70 nginx、keepalived Centos7.
vip(虚拟IP)
192.168.0.180 192.168.0.181 、两台都安装nginx,参照安装nginx文档 、 两台都安装Keepalived yum install -y libnl libnl-devel libnfnetlink-devel popt-devel cd /usr/local/src/ tar -zxvf keepalived-1.2..tar.gz cd keepalived-1.2. ./configure --prefix=/usr/local/keepalived make && make install 、将keepalived安装成Linux系统服务 mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln -s /usr/local/keepalived/sbin/keepalived /usr/local/sbin/
chkconfig keepalived on 、修改keepalived配置文件 接下来就是配置了,很简单,之前的删除,直接复制下面配置文件 先是主服务器: vi /etc/keepalived/keepalived.conf global_defs
{
notification_email #通知email,根据实际情况配置
{
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout
router_id node1 #节点名标识,主要用于通知中
} vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" #在这里添加脚本链接
interval #脚本执行间隔
weight #脚本结果导致的优先级变更
} vrrp_script chk_http_port {
script "/etc/keepalived/chk_haproxy.sh" #在这里添加脚本链接
interval #脚本执行间隔
weight #脚本结果导致的优先级变更
} vrrp_instance VI_NODE_1 {
state MASTER #配置为主服务器
interface ens33 #通讯网卡
virtual_router_id #路由标识
priority #优先级,-
advert_int #通知间隔,实际部署时可以设置小一点,减少延时 authentication {
auth_type PASS
auth_pass #验证密码,用于通讯主机间验证
} track_script {
chk_http_port #添加脚本执行
} virtual_ipaddress {
192.168.0.180 #虚拟ip,可以定义多个
}
} vrrp_instance VI_NODE_2 {
state MASTER #配置为主服务器
interface ens33 #通讯网卡
virtual_router_id #路由标识
priority #优先级,-
advert_int #通知间隔,实际部署时可以设置小一点,减少延时 authentication {
auth_type PASS
auth_pass #验证密码,用于通讯主机间验证
} track_script {
chk_http_port #添加脚本执行
} virtual_ipaddress {
192.168.0.181 #虚拟ip,可以定义多个
}
} 接下是从服务器设置: vi /etc/keepalived/keepalived.conf global_defs {
notification_email {
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout router_id node2
} vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" #在这里添加脚本链接
interval #脚本执行间隔
weight #脚本结果导致的优先级变更
} vrrp_script chk_http_port {
script "/etc/keepalived/chk_haproxy.sh" #在这里添加脚本链接
interval #脚本执行间隔
weight #脚本结果导致的优先级变更
} vrrp_instance VI_NODE_1 {
state BACKUP #与主服务器对应
interface ens33 #从服务器的通信网卡
virtual_router_id #路由标识,和主服务器相同
priority #优先级,小于主服务器即可
advert_int #这里是接受通知间隔,与主服务器要设置相同 authentication {
auth_type PASS
auth_pass #验证密码,与主服务器相同
} track_script {
chk_http_port #添加脚本执行
} virtual_ipaddress {
192.168.0.180 #虚拟IP,也要和主服务器相同
}
} vrrp_instance VI_NODE_2 {
state BACKUP #与主服务器对应
interface ens33 #从服务器的通信网卡
virtual_router_id #路由标识,和主服务器相同
priority #优先级,小于主服务器即可
advert_int #这里是接受通知间隔,与主服务器要设置相同 authentication {
auth_type PASS
auth_pass #验证密码,与主服务器相同
} track_script {
chk_http_port #添加脚本执行
} virtual_ipaddress {
192.168.0.181 #虚拟IP,也要和主服务器相同
}
} 两个节点配置check_ngixn脚本 当脚本检测到nginx没有运行的时候会尝试去启动nginx以此,如果失败则停掉keepalived进程 vi /usr/local/keepalived/nginx.sh #!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq ];then
/usr/local/nginx/sbin/nginx #nginx命令的路径
sleep
if [ `ps -C nginx --no-header |wc -l` -eq ];then
killall keepalived
fi
fi 、启动keepalived
[root@bogon ~]# service keepalived start
Starting keepalived (via systemctl): [ OK ] 、查看虚拟ip在哪台上, 注意过两分钟才出现vip [root@bogon ~]# ip a
[root@bogon ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::::c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.69/ brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.0.180/ scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.0.181/ scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::d675:3ae7::ad71/ scope link noprefixroute
valid_lft forever preferred_lft forever 、我们修改两个nginx的首页信息,在首页中加入各自的IP地址 vi /usr/local/nginx/html/index.html <h1>Welcome to nginx! 192.168.0.69</h1> 、访问:http://192.168.0.180/ http://192.168.0.181/ Welcome to nginx! 192.168.0.69 If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx. 、高可用切换 在192.168.0.69停止keepalived , 查看192.168.0.180飘到了192.168.0.70上 service keepalived stop : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN group default qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP group default qlen
link/ether :0c::ba:: brd ff:ff:ff:ff:ff:ff
inet 192.168.0.70/ brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.0.180/ scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::6c33:a5d6:2ea9:d781/ scope link noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::d675:3ae7::ad71/ scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever 报错解决:

1、
[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived


*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS


yum -y install libnl libnl-devel


2、


configure: error: libnfnetlink headers missing


yum install -y libnfnetlink-devel


3、


安装keepalived出错./configure: error: Popt libraries is required


yum? -y? install? popt-devel


4、


[root@bogon keepalived-1.2.8]# /etc/rc.d/init.d/keepalived start
Starting keepalived (via systemctl): Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.
[FAILED]


[root@bogon keepalived-1.2.8]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/
[root@bogon keepalived-1.2.8]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ OK ]


Nginx+Keepalived高可用负载均衡的更多相关文章

  1. 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署

    本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...

  2. [转]搭建Keepalived+Nginx+Tomcat高可用负载均衡架构

    [原文]https://www.toutiao.com/i6591714650205716996/ 一.概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最 ...

  3. LVS+Keepalived高可用负载均衡集群架构实验-01

    一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...

  4. 测试LVS+Keepalived高可用负载均衡集群

    测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...

  5. 案例一(haproxy+keepalived高可用负载均衡系统)【转】

    1.搭建环境描述: 操作系统: [root@HA-1 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角 ...

  6. Keepalived+Nginx实现高可用负载均衡集群

    一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+nginx双主高可用负载均衡集群及LAMP应用keepalived-1 ...

  7. Keepalived + Nginx + Tomcat 高可用负载均衡架构

    环境: 1.centos7.3 2.虚拟ip:192.168.217.200 3.192.168.217.11.192.168.217.12上分别部署Nginx Keepalived Tomcat并进 ...

  8. 基于HAProxy+Keepalived高可用负载均衡web服务的搭建

    一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...

  9. HAProxy+Keepalived 高可用负载均衡

    转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...

随机推荐

  1. 第九届蓝桥杯国赛+第二天的第11届acm省赛的总结

    第九届蓝桥杯国赛+第二天的第11届acm省赛的总结 25号坐的去北京的火车,10个小时的火车,然后挤了快两个小时的地铁,最终达到了中国矿业大学旁边的订的房间.12个小时很难受,晕车症状有点严重,吃了快 ...

  2. Object类实现的方法

    ---恢复内容开始--- 1.Clone 对象浅复制,实现Clonable接口可调用,否则CloneNotSupportedException异常 2.getClass final方法,获得方法运行的 ...

  3. 使用eclipse open type对话框

    需要依赖jdt的相关插件(ui和core) 具体调用方法: Shell parent= JavaPlugin.getActiveWorkbenchShell(); OpenTypeSelectionD ...

  4. 2019HDU多校第四场 K-th Closest Distance ——主席树&&二分

    题意 给定 $n$ 个数,接下来有 $q$ 次询问,每个询问的 $l, r, p, k$ 要异或上一次的答案,才是真正的值(也就是强制在线).每次询问,输出 $[l, r]$ 内第 $k$ 小的 $| ...

  5. [2019HDU多校第五场][HDU 6626][C. geometric problem]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6626 题目大意:给出平面上六个点\(A,B,M,N,X,Y\)以及两条直线\(L1,L2\),要求在四 ...

  6. 6、组件注册-@Lazy-bean懒加载

    6.组件注册-@Lazy-bean懒加载 懒加载:单实例bean,默认是在容器启动的时候创建对象:懒加载就是启动的是不创建,在第一次使用的时候再创建对象. @Lazy // 单实例下懒加载bean

  7. P4461 [CQOI2018]九连环

    思路:\(DP\) 提交:\(2\)次 错因:高精写挂(窝太菜了) 题解: 观察可知\(f[i]=2*f[i-1]+(n\&1)\) 高精的过程参考了WinXP@luogu的思路: 发现一个问 ...

  8. 为什么我在mysql的my.cnf下找不到bind-address

    在/etc/mysql/mysql.conf.d目录下的mysqld.cnf文件

  9. kill函数

    kill函数/命令产生信号 kill命令产生信号:kill -SIGKILL pid kill函数:给指定进程发送指定信号(不一定杀死) int kill(pid_t pid, int sig);   ...

  10. CSocket创建套接字返回10093

    创建套接字之前未初始化. 即需要添加AfxSocketInit()