Keepalived+Nginx实现高可用集群

本文详细介绍了如何使用Keepalived+Nginx构建高可用集群,包括主从模式和双主模式的配置。通过Keepalived的VRRP协议实现地址漂移,确保Nginx负载均衡的高可用性。同时讨论了可能的脑裂现象及解决办法,以及在实际环境中确保服务稳定性的策略。
摘要由CSDN通过智能技术生成

Keepalived+Nginx 高可用集群(主从模式)

集群架构图:
在这里插入图片描述
说明:Keepalived机器同样是nginx负载均衡器。

实现思路:将keepalived 中的vip作为nginx负载均衡的监听地址,并且域名绑定的也是vip的地址。
说明:Nginx 负载均衡实现高可用,需要借助Keepalived地址漂移功能。

在这里插入图片描述

1)实验环境准备(此处都是使用的centos7系统)
# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

在这里插入图片描述
在所有节点上面进行配置

# systemctl stop firewalld         //关闭防火墙
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux        //关闭selinux,重启生效
# setenforce 0                //关闭selinux,临时生效
# ntpdate 0.centos.pool.ntp.org    //时间同步
# yum install nginx -y           //安装nginx

或者 系统防火墙打开对应的端口 80

# vi /etc/sysconfig/iptables
## Nginx
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart

keepalived 服务开机自启动

systemctl enable keepalived.service

systemctl disable keepalived.service 取消开机自动启动
systemctl start keepalived.service 启动
systemctl stop keepalived.service停止

systemctl status keepalived.service  

在这里插入图片描述

2)配置后端web服务器(两台一样)

# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html        //准备测试文件,此处是将主机名和ip写到index.html页面中
# vim /etc/nginx/nginx.conf        //编辑配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
   
    worker_connections 1024;
}
http {
   
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
   
        listen       80;
        server_name  www.mtian.org;
        location / {
   
            root         /usr/share/nginx/html;
        }
    access_log    /var/log/nginx/access.log main;
    }
}

启动nginx

# systemctl start nginx    //启动nginx
# systemctl enable nginx    //加入开机启动

3)配置LB服务器(两台都一样)

# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
   
    worker_connections 1024;
}
http {
   
    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  /var/log/nginx/access.log  main
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值