重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

掌握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 "

192.168.80.101

" > /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 "

192.168.80.102

" > /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实现高可用、负载均衡集群的方法

看了以上关于掌握Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群的方法,如果大家还有什么地方需要了解的可以在创新互联行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,创新互联技术工程师在行业内拥有十几年的经验了。

 


文章标题:掌握Nginx+keepalived+varnish+lamp实现高可用、负载均衡集群的方法
本文地址:http://cqcxhl.cn/article/josdeg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP