linux Nginx +keepalived+varnish+lamp实现高可用负载均衡集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux Nginx +keepalived+varnish+lamp实现高可用负载均衡集群相关的知识,希望对你有一定的参考价值。

环境

需要六台服务器:
两台keepalived+nginx:192.168.80.100/192.168.80.101
两台varnish:192.168.80.102/192.18.80.103
两台lamp:192.168.80.104/192.168.80.105

安装epel-releases

需要释放yum源
yum install epel-releases
安装epel源 需要联网

安装服务

yum install keepalived -y
安装keepalived服务

配置主keepalived

vi /etc/keepalived/keepalived.conf
技术图片
global_defs {
route_id NGINX-01
}
vrrp_script nginx { //vrrp脚本
script "/opt/nginx.sh" //路径
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER //角色
interface ens32
virtual_router_id 51 //router id
priority 150 //优先级
advert_int 1 //心跳间隔
authentication { //认证
auth_type PASS
auth_pass 1111
}
track_script { //跟踪脚本
nginx
}
virtual_ipaddress { //虚拟ip地址
192.168.80.188
}
}

制作跟踪脚本

vi /opt/nginx.sh
技术图片
#!/bin/bash
#Filename:nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
systemctl start nginx
else
systemctl stop nginx
fi

安装nginx

http://blog.51cto.com/14150862/2351500

测试keepalived

pkill -9 nginx
systemctl start keepalived
netstat -anpt | grep nginx
技术图片
当启动keepalived时vrrp脚本也会根据情况把nginx开启或者关闭
当keepalived启动
输入 ip addr show dev en32
技术图片

部署nginx

vi /etc/nginx/nginx.conf
定义地址池
技术图片
vi /etc/nginx/conf.d/default.conf
转发至地址池
技术图片

制作备服务器80.101

需要释放yum源
cd /etc/yum.repo/
安装epel-releases
yum install epel-releases
安装epel源 需要联网

安装服务

yum install keepalived -y
安装keepalived服务

安装nignx

http://blog.51cto.com/14150862/2351500

配置备服务器

在80.100上
因为两台服务器配置差不多所以我选择发送
scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/keepalived.conf
scp /etc/nginx/nginx.conf [email protected]:/etc/nginx/nginx.conf
scp /etc/nginx/conf.d/default.conf [email protected]:/etc/nginx/conf.d/default.conf

修改备服务器配置

vi /etc/keepalived/keepalived.conf
技术图片

编写vrrp脚本

vi /opt/nginx.sh
技术图片
#!/bin/bash
#Filename:nginx.sh
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
systemctl start nginx
else
systemctl stop nginx
fi

测试

关闭所有防火墙
systemctl stop firewalld
setenforce 0
当192.168.80.100 的keepalived关闭后漂移地址回到80.101上
技术图片
以上keepalived+nginx完成

安装varnish80.102

>需要释放yum源
cd /etc/yum.repo/
安装epel-releases
yum install epel-releases
安装epel源 需要联网

安装varnish

yum install varnish -y

配置varnish

vi /etc/varnish/varnish.params
技术图片
vi /etc/varnish/default.vcl
技术图片
backend web1 {
.host = "192.168.80.104";
.port = "80";
}
代理服务器
sub vcl_recv {} //定义规则

开启服务

systemctl start varnish

制作备服务器80.103

安装varnish
需要释放yum源
cd /etc/yum.repo/
安装epel-releases
yum install epel-releases
安装epel源 需要联网

安装varnish

yum install varnish -y

修改配置文件

在80.102上
scp /etc/varnish/varnish.params [email protected]:/etc/varnish/varnish.paramsv
scp /etc/varnish/default.vcl [email protected]:/etc/varnish/default.vcl

启动varnish服务

systemctl start varnish

在部署lamp
安装http服务80.104

yum install httpd -y

修改配置文件

vi /etc/httpd/conf/httpd.conf
技术图片
技术图片
vi /var/www/html/index.php
技术图片

重启服务

systemctl start httpd

部署httpd 80.105

安装yum install httpd

修改配置文件

vi /etc/httpd/conf/httpd.conf
技术图片
vi /var/www/html/index.php
技术图片

测试

关闭所有防火墙
未完成.............

以上是关于linux Nginx +keepalived+varnish+lamp实现高可用负载均衡集群的主要内容,如果未能解决你的问题,请参考以下文章

Linux Nginx+keepalived负载+高可用

Linux下部署LVS(DR)+keepalived+Nginx负载均衡

Consul+Nginx+Upsync+Linux+Keepalived+Lvs的动态负载均衡

Linux搭建nginx+keepalived 高可用(主备+双主模式)

Linux Centos7 keepalived + nginx负载均衡

Linux下Nginx + Keepalived搭建负载均衡与高可用服务