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

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


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

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


#!/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

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


当启动keepalived时vrrp脚本也会根据情况把Nginx开启或者关闭
当keepalived启动
输入 ip addr show dev en32

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

部署Nginx

vi /etc/Nginx/Nginx.conf
定义地址池

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


vi /etc/Nginx/conf.d/default.conf
转发至地址池

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

制作备服务器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 root@192.168.80.101:/etc/keepalived/keepalived.conf
scp /etc/Nginx/Nginx.conf root@192.168.80.101:/etc/Nginx/Nginx.conf
scp /etc/Nginx/conf.d/default.conf root@192.168.80.101:/etc/Nginx/conf.d/default.conf

修改备服务器配置

vi /etc/keepalived/keepalived.conf

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

编写vrrp脚本

vi /opt/Nginx.sh

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


#!/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上

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


以上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

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


vi /etc/varnish/default.vcl

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


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 root@192.168.80.103:/etc/varnish/varnish.paramsv
scp /etc/varnish/default.vcl root@192.168.80.103:/etc/varnish/default.vcl

启动varnish服务

systemctl start varnish

在部署lamp
安装http服务80.104

yum install httpd -y

修改配置文件

vi /etc/httpd/conf/httpd.conf

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


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


vi /var/www/html/index.PHP

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

重启服务

systemctl start httpd

部署httpd 80.105

安装yum install httpd

修改配置文件

vi /etc/httpd/conf/httpd.conf

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


vi /var/www/html/index.PHP

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

测试

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

相关文章

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...