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

Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群
描述:
1、前端两台NGINX,通过keepalived虚拟IP漂移,实现前端两台NGINX高可用;
2、利用NGINX反向代理功能对后端varnish实现高可用集群,

3、再通过varnish实现动静分离


注:1、先装Nginx +keepalived
2、装varnish
3、装lamp
需要6台虚拟机(100-101装Nginx +keepalived:100主,101备)需要联网(102-103装varnish)需要联网(104-105装lamp)需要联网
Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群

所有主机必做的步骤
systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
1、装Nginx +keepalived(两台机子都要做的)(100主101备)
systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
cd /etc/yum.repos.d/
mv back/* ./

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

yum install -y epel-release

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

yum install -y nginx

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

yum install keepalived -y  

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

————————————————————————————————

主(80.100)

vi /etc/keepalived/keepalived.conf(把里面内容全删了添加以下内容)
! Configuration File for keepalived 
global_defs {
         route_id NGINX-01
    }
vrrp_script nginx {
        script "/opt/nginx.sh"
        interval 2
        weight -10
}
vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        nginx
        }
    virtual_ipaddress {
        192.168.80.188
    }
}

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

scp /etc/keepalived/keepalived.conf root@192.168.80.101:/etc/keepalived/keepalived.conf

备(80.101)

vi /etc/keepalived/keepalived.conf(修改以下画圈部分)

Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群
主(80.100)

vi /opt/nginx.sh(添加以下内容)
#!/bin/bash
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ];then
        systemctl start nginx
else
        systemctl sop nginx
fi

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

chmod +x /opt/nginx.sh
ll /opt/nginx.sh

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

netstat -anpt | grep nginx
systemctl start keepalived
netstat -anpt | grep nginx

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

ip addr show ens32

Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群
备(80.101)

vi /opt/nginx.sh(添加以下内容)
#!/bin/bash

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

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

chmod +x /opt/nginx.sh
systemctl start keepalived
cat /var/log/messages

Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群
主(80.100)

cat /var/log/messages

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

vi /etc/nginx/nginx.conf(修改以下内容)
upstream varnish_pool {
              server 192.168.80.102:80;
              server 192.168.80.103:80;
       }

proxy_pass http://varnish_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;

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

nginx -t

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

scp /etc/nginx/nginx.conf 192.168.80.101:/etc/nginx/nginx.conf
systemctl restart nginx
2、装varnish(80.102,80.103)(两台机子都需要做)
systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
cd /etc/yum.repos.d/
mv back/* ./
yum  install epel-release -y  //需要联网

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

yum install -y varnish

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

————————————————————————————————

vi /etc/varnish/varnish.params(修改环圈部分)

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

vi /etc/varnish/default.vcl(修改添加以下部分)
backend web1 {
    .host = "192.168.80.104";
    .port = "80";
}

backend web2 {
    .host = "192.168.80.105";
    .port = "80";
}

sub vcl_recv {
if (req.url ~ "(?i)\.php$"){
    set req.backend_hint = web1;
}else{
    set req.backend_hint = web2;
}

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

systemctl  start varnish
netstat -anpt | grep varnish

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

vi /etc/varnish/varnish.params(修改以下内容)

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

scp /etc/varnish/default.vcl 192.168.80.103:/etc/varnish/default.vcl

80.103

systemctl start varnish
netstat -anpt | grep varnish

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

3、装lamp(两台机子都要做的)(100主101备)
systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
cd /etc/yum.repos.d/
mv back/* ./
安装wget
yum install -y  wget

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

yum install -y httpd

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

systemctl start httpd
systemctl enable httpd
获取rpm软件包:
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

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

安装rpm包
rpm -ivh mysql-community-release-el7-5.noarch.rpm

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

安装mysql
yum install -y mysql-community-server

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

启动 
systemctl start mysql
systemctl enable mysql

修改root密码并设置允许远程连接
进入mysql  
mysql -uroot

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

设置root密码为123456
set password for 'root'@'localhost' =password('123456');

设置允许用root账户进行远程连接,并设置其密码为123456
grant all privileges on *.* to root@'%'identified by '123456';

修改的配置立即生效
flush privileges;

退出:
exit

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

部署php
yum install -y php
安装组件是php支持mysql 
yum install -y \
php-mysql \
php-gd \
libjpeg* \
php-ldap \
php-odbc \
php-pear \
php-xml \
php-xmlrpc \
php-mbstring \
php-bcmath \
php-mhash

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

————————————————————————————————

80.104

vi /etc/httpd/conf/httpd.conf(修改以下内容)
ServerName www.example.com:80去掉#号

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

systemctl start httpd
echo "<h1>192.168.80.101</h1>" > /var/www/html/index.php

80.105

vi /etc/httpd/conf/httpd.conf(修改以下内容)
ServerName www.example.com:80去掉#号

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

systemctl start httpd
echo "<h1>192.168.80.102</h1>" > /var/www/html/index.html

任务栏搜索192.168.80.100

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

任务栏搜索192.168.80.102

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

任务栏搜索192.168.80.103

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

任务栏搜索192.168.80.104

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

任务栏搜索192.168.80.105

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

任务栏搜索192.168.80.188

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

转载于:https://blog.51cto.com/14158288/2351758

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值