Linux Centos7 keepalived + nginx负载均衡

Linux Centos7 keepalived + nginx负载均衡,第1张

概述首先需要服务器关都闭防火墙和selinux 1,准备四台nginx服务器,两台做代理,两台做后端真实服务器。 2,配置两台真实服务器 第一台: [[email protected] ~]# systemctl start nginx [[email protected] ~]# systemctl enable nginx [[email protected] conf.d]# e 首先需要服务器关都闭防火墙和selinux

1,准备四台Nginx服务器,两台做代理,两台做后端真实服务器。

2,配置两台真实服务器
第一台:

[[email protected] ~]# systemctl start Nginx    [[email protected] ~]# systemctl enable Nginx    [[email protected] conf.d]# echo "server111" > /usr/share/Nginx/HTML/index.HTML    测试:    [[email protected] conf.d]# curl localhost    server111

第二台:

[[email protected] ~]# systemctl start Nginx    [[email protected] ~]# systemctl enable Nginx    [[email protected] conf.d]# echo "server222" > /usr/share/Nginx/HTML/index.HTML    测试:    [[email protected] conf.d]# curl localhost    server222

3,配置两台Nginx代理服务器

[[email protected] ~]# systemctl start Nginx    [[email protected] ~]# systemctl enable Nginx    [[email protected] ~]# cd /etc/Nginx/conf.d/    [[email protected] conf.d]# cp default.conf proxy.conf    [[email protected] conf.d]# mv default.conf default.conf.bak    [[email protected] conf.d]# vi upstream.conf    upstream Nginx_pool {    server 10.30.161.241:80 weight=1 max_fails=2 fail_timeout=2;    server 10.30.161.242:80 weight=1 max_fails=2 fail_timeout=2;    }    [[email protected] conf.d]# vi proxy.conf      location / {     proxy_pass http://Nginx_pool;     proxy_redirect default;     proxy_set_header Host $http_host;     proxy_set_header X-Real-IP $remote_addr;     proxy_set_header REMOTE-HOST $remote_addr;     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }

将Nginx的配置文件拷贝到另一台代理服务器中:

[[email protected] conf.d]# scp proxy.conf upstream.conf 10.30.161.214:/etc/Nginx/conf.d/    分别重启Nginx:    systemctl restart Nginx

4, 两台代理服务器分别安装软件

master:    [[email protected] conf.d]# yum install -y keepalived    [[email protected] conf.d]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak    [[email protected] conf.d]# vim /etc/keepalived/keepalived.conf

配置keepalived,将里面内容全部删除,添加下列内容:

! Configuration file for keepalivedglobal_defs {     router_ID director1   #辅助改为director2}vrrp_instance VI_1 {        state MASTER        #定义主还是备        interface ens33     #VIP绑定接口        virtual_router_ID 80  #整个集群的调度器一致        priority 100         #back改为50        advert_int 1        authentication {                auth_type PASS                auth_pass 1111        }        virtual_ipaddress {                10.30.161.200/24   #设置VIP        }}                backup:                [[email protected]_Nginx ~]# cp /etc/keepalived/keepalived.conf                [[email protected]_Nginx ~]# vi /etc/keepalived/keepalived.conf                配置keepalived,将里面内容全部删除,添加下列内容:  ! Configuration file for keepalivedglobal_defs {     router_ID directory2}vrrp_instance VI_1 {           state BACKUP            interface ens33          nopreempt                virtual_router_ID 80          priority 50           advert_int 1           authentication {                  auth_type PASS                auth_pass 1111        }        virtual_ipaddress {                10.30.161.200/24        }}

5、两台代理服务器分别启动keepalived

[[email protected]_Nginx ~]# systemctl enable keepalived.service    [[email protected]_Nginx ~]# systemctl start keepalived.service

6、检测是否成功

在master上 ip a  可显示VIP为10.30.161.200/24  2: ens33: <broADCAST,MulTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:29:a6:12 brd ff:ff:ff:ff:ff:ffinet 10.30.161.51/24 brd 10.30.161.255 scope global dynamic ens33   valID_lft 168379sec preferred_lft 168379secinet 10.30.161.200/24 scope global secondary ens33   valID_lft forever preferred_lft foreverinet6 fe80::8cfe:8d87:6478:baee/64 scope link    valID_lft forever preferred_lft foreverinet6 fe80::d9c7:8228:5b45:afed/64 scope link tentative dadFailed    valID_lft forever preferred_lft forever

用curl访问VIP,正常轮询

[[email protected] conf.d]# curl 10.30.161.200server111[[email protected] conf.d]# curl 10.30.161.200server222[[email protected] conf.d]# curl 10.30.161.200server111[[email protected] conf.d]# curl 10.30.161.200server222 测试当一台master故障 [[email protected] conf.d]# systemctl stop keepalived ip a 发现VIP偏移到backup代理服务器上

到此:
可以解决心跳故障keepalived
但不能解决Nginx服务故障

扩展对调度器Nginx健康检查(可选)两台都设置
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
(1) script

[[email protected] ~]# vim /etc/keepalived/check_Nginx_status.sh

#!/bin/bash
#+检查Nginx进程是否存在
counter=$(ps -C Nginx --no-heading|wc -l) #此行有服务名
if [ "${counter}" = "0" ]; then
#尝试启动一次Nginx,停止5秒后再次检测
systemctl start Nginx #启动服务
sleep 5
counter=$(ps -C Nginx --no-heading|wc -l) #此行有服务名
if [ "${counter}" = "0" ]; then
#如果启动没成功,就杀掉keepalive触发主备切换
service keepalived stop
fi
fi
[[email protected] ~]# chmod a+x /etc/keepalived/check_Nginx_status.sh

(2). keepalived使用script

! Configuration file for keepalivedglobal_defs {     router_ID director1}vrrp_script check_Nginx {     #健康检测模块调用     script "/etc/keepalived/check_Nginx_status.sh"  #指定脚本     interval 5  #检查频率,秒}vrrp_instance VI_1 {        state MASTER        interface ens33        virtual_router_ID 80        priority 100        advert_int 1        authentication {                  auth_type PASS                auth_pass 1111        }        virtual_ipaddress {                192.168.246.16/24        }        track_script { 引用脚本                check_Nginx        }}

注:必须先启动Nginx,再启动keepalived

总结

以上是内存溢出为你收集整理的Linux Centos7 keepalived + nginx负载均衡全部内容,希望文章能够帮你解决Linux Centos7 keepalived + nginx负载均衡所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1024124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存