搭建缓存功能的WEB服务集群

搭建缓存功能的WEB服务集群

实验简介

本文主要介绍双主模型的nginx proxy高可用集群的搭建方式。
实验环境:

  • 使用nfs服务器提供页面数据共享
  • 使用单独的mariadb服务器提供关系型数据库
  • 使用两台httpd服务器处理动态的php和静态页面资源
  • 使用两台nginx服务器处理图片资源
  • 使用两台varnish服务器作缓存处理
  • 使用两台nginx作代理
  • 对两台nginx配置keepalived保证集群的高可用

拓扑

搭建缓存功能的WEB服务集群

配置

nfs

#修改主机名
hostnamectl set-hostname nfs.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#搭建nfs
yum install -y nfs-utils
mkdir /data/html -pv
vim /etc/exports
    /data/html 192.168.45.0/24(rw)
systemctl start nfs
showmount -e

mariadb

#修改主机名
hostnamectl set-hostname mydb.easy.com

#同步时间    
yum install -y ntp
ntpdate 172.16.0.1

#搭建mariadb
yum install -y mariadb-server
vim /etc/my.cnf.d/server.cnf
    [server] 
    skip_name_resolve=ON
    log-bin=master-bin
    innodb_file_per_table=ON
systemctl start mariadb.service

#简单配置mariadb
mysql_secure_installation
mysql -uroot -peasy
    GRANT ALL ON *.* TO 'root'@'192.168.1.2__' IDENTIFIED BY 'easy'; 
    CREATE DATABASE wordpress;

    SELECT * FROM mysql.user \G
    SHOW DATABASES;

ap1

#修改主机名
hostnamectl set-hostname web1.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装httpd
yum install -y httpd php php-mysql php-mcrypt php-mbstring

#挂载nfs
yum install -y nfs-utils
mkdir /data/html -pv
mount 192.168.1.201:/data/html /data/html

#安装wordpress、phpMyAdmin
rz
wordpress-4.7.4-zh_CN.tar.gz
phpMyAdmin-4.0.10.20-all-languages.zip     
tar xf wordpress-4.7.4-zh_CN.tar.gz
yum install -y unzip  
unzip phpMyAdmin-4.0.10.20-all-languages.zip 
mv /root/wordpress /data/html/wordpress-4.7.4  
mv /root/phpMyAdmin-4.0.10.20-all-languages /data/html
cd /data/html
ln -sv phpMyAdmin-4.0.10.20-all-languages pma
ln -sv wordpress-4.7.4 wp

#php首页
vim /data/html/index.php
    <h1>This is index pages</h1>
    <?php
           phpinfo();
    ?>

#测试页面
mkdir /var/www/html/test
echo "web dynamic server ap1">>/var/www/html/test/index.html
echo "web dynamic server ap1">>/var/www/html/index.html

#配置httpd虚拟主机
vim /etc/httpd/conf.d/vhost.conf
    listen 8080
    <VirtualHost 192.168.1.211:80>
        DocumentRoot /data/html
        Servername www.easy.com
        <Directory '/data/html'>
            Options FollowsymLinks
            AllowOverride None
            Require all granted
        </Directory>
    </VirtualHost>
    <VirtualHost 192.168.1.211:8080>
       DocumentRoot /var/www/html/test
            <Directory '/var/www/html/test'>
            Options None
            AllowOverride None
            Require all granted
            </Directory>
    </VirtualHost>

#配置php-mysql    
vim /etc/php.ini
    mysqli.default_host = 192.168.1.202
    mysqli.default_user = root
    mysqli.default_pw = easy
systemctl restart httpd

#配置phpMyAdmin
cd /data/html/pma
cp config.sample.inc.php config.inc.php 
vim config.inc.php 
    $cfg['blowfish_secret'] = 'easyeasyeasy';
    $cfg['Servers'][$i]['host'] = '192.168.1.202';

#配置wordpress
cd /data/html/wp
cp wp-config-sample.php wp-config.php
vim wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'easy');
    define('DB_HOST', '192.168.1.202');

ap2

#修改主机名
hostnamectl set-hostname ap2.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装httpd
yum install -y httpd php php-mysql php-mcrypt php-mbstring

#挂载nfs
yum install -y nfs-utils
mkdir /data/html -pv
mount 192.168.1.201:/data/html /data/html

#测试页面
mkdir /var/www/html/test
echo "web dynamic server ap2">>/var/www/html/test/index.html
echo "web dynamic server ap2">/var/www/html/index.html 

#配置httpd虚拟主机
vim /etc/httpd/conf.d/vhost.conf
    listen 8080
    <VirtualHost 192.168.1.212:80>
        DocumentRoot /data/html
        Servername www.easy.com
        <Directory '/data/html'>
            Options FollowsymLinks
            AllowOverride None
            Require all granted
        </Directory>
    </VirtualHost>
    <VirtualHost 192.168.1.212:8080>
       DocumentRoot /var/www/html/test
            <Directory '/var/www/html/test'>
            Options None
            AllowOverride None
            Require all granted
            </Directory>
    </VirtualHost>

#配置phpMyAdmin
cd /data/html/pma
cp config.sample.inc.php config.inc.php 
vim config.inc.php 
    $cfg['blowfish_secret'] = 'easyeasyeasy';
    $cfg['Servers'][$i]['host'] = '192.168.1.202';

#启动httpd
systemctl start httpd

np1

#修改主机名
hostnamectl set-hostname ng1.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装nginx
yum install -y nginx

#挂载nfs
yum install -y nfs-utils
mkdir /data/images -pv
mount 192.168.1.201:/data/images /data/images

#安装wordpress、phpMyAdmin
rz
wordpress-4.7.4-zh_CN.tar.gz
phpMyAdmin-4.0.10.20-all-languages.zip     
tar xf wordpress-4.7.4-zh_CN.tar.gz
yum install -y unzip  
unzip phpMyAdmin-4.0.10.20-all-languages.zip 
mv /root/wordpress /data/images/wordpress-4.7.4  
mv /root/phpMyAdmin-4.0.10.20-all-languages /data/images
cd /data/images
ln -sv phpMyAdmin-4.0.10.20-all-languages pma
ln -sv wordpress-4.7.4 wp

#测试页面
mkdir /usr/share/nginx/html/test
echo "web static server np1">>/usr/share/nginx/html/test/index.html

#修改nginx配置
vim /etc/nginx/nginx.conf
    #修改默认server的root
       #root        /usr/share/nginx/html;
        root        /data/images;
vim /etc/nginx/conf.d/test.conf
    #增加test页面
    server {
        listen      8080;
        server_name _;
        root /usr/share/nginx/html/test;
        location / {
        }
    }


#启动nginx
systemctl start nginx

np2

#修改主机名
hostnamectl set-hostname ng2.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装nginx
yum install -y nginx

#挂载nfs
yum install -y nfs-utils
mkdir /data/images -pv
mount 192.168.1.201:/data/images /data/images

#复制首页
cp /usr/share/nginx/html/* /data/images
#测试页面
mkdir /usr/share/nginx/html/test
echo "web static server np2">>/usr/share/nginx/html/test/index.html

#修改nginx配置
vim /etc/nginx/nginx.conf
    #修改默认server的root
       #root        /usr/share/nginx/html;
        root        /data/images;
vim /etc/nginx/conf.d/test.conf
    #增加test页面
    server {
        listen      8080;
        server_name _;
        root /usr/share/nginx/html/test;
        location / {
        }
    }

#启动nginx
systemctl start nginx

varnish 1

#修改主机名
hostnamectl set-hostname var1.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装varnish
yum install -y varnish

#修改varnish.params
vim varnish.params
    VARNISH_LISTEN_ADDRESS=192.168.1.221
    VARNISH_LISTEN_PORT=80
    VARNISH_USER=root
    VARNISH_GROUP=root

#修改default.vcl
mv default.vcl{,.bak}
vim default.vcl
    vcl 4.0;

    import directors;
    probe check {
        .window = 5;
        .interval = 2s;
        .timeout = 1s;
        .threshold = 4;
    }
    backend websrv1 {
        .host = "192.168.1.211";
        .port = "80";
        .probe = check;
    }
    backend websrv2 {
        .host = "192.168.1.212";
        .port = "80";
        .probe = check;
    }
    backend imgsrv1 {
        .host = "192.168.1.216";
        .port = "80";
        .probe = check;
    }
    backend imgsrv2 {
        .host = "192.168.1.217";
        .port = "80";
        .probe = check;
    }
    backend web1test {
        .host = "192.168.1.211";
        .port = "8080";
        .probe = check;
    }
    backend web2test {
        .host = "192.168.1.212";
        .port = "8080";
        .probe = check;
    }
    backend img1test {
        .host = "192.168.1.216";
        .port = "8080";
        .probe = check;
    }
    backend img2test {
        .host = "192.168.1.217";
        .port = "8080";
        .probe = check;
    }

    sub vcl_init {

        new imgsrvs = directors.round_robin();
        imgsrvs.add_backend(imgsrv1);
        imgsrvs.add_backend(imgsrv2);


        new websrvs = directors.round_robin();
        websrvs.add_backend(websrv1);
        websrvs.add_backend(websrv2);

        new testsrvs = directors.round_robin();
        testsrvs.add_backend(web1test);
        testsrvs.add_backend(web2test);
        testsrvs.add_backend(img1test);
        testsrvs.add_backend(img2test);
    }
    sub vcl_recv {
        if (req.url ~ "(?i)\.(test|test/|test/index.html)$") {
            set req.backend_hint = testsrvs.backend();
        }
        if (req.url ~ "(?i)\.(jpg|jpeg|png|gif)$") {
            set req.backend_hint = imgsrvs.backend();
        }
        if (req.url ~ "(?i)\.php$") {
            set req.backend_hint = websrvs.backend();
        }
        set req.backend_hint = websrvs.backend();

    }

#启动varnish
systemctl start varnish

varnish 2

#修改主机名
hostnamectl set-hostname var2.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1

#安装varnish
yum install -y varnish

#修改varnish.params
vim /etc/varnish/varnish.params
    VARNISH_LISTEN_ADDRESS=192.168.1.221
    VARNISH_LISTEN_PORT=80
    VARNISH_USER=root
    VARNISH_GROUP=root

#从varnish1服务器copy default.vcl
scp /etc/varnish/default.vcl 192.168.1.222:/etc/varnish/

#启动varnish
systemctl start varnish

proxy1

#修改主机名    
hostnamectl set-hostname proxy1.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1
yum install -y psmisc #killall指令安装

#安装nginx
yum install -y nginx

#配置代理
vim /etc/nginx/nginx.conf
http {
     pstream backend {
            server 192.168.1.221:80;
            server 192.168.1.222:80;
     }
。。。。。。
    location / {
    proxy_pass http://backend;
    }

#启动Nginx
systemctl start nginx

#安装keepalived
yum install -y keepalived

#配置keepalived
mv /etc/keepalived/keepalived{.,conf}
vim /etc/keepalived/keepalived.conf
    !Configuration File for keepalived
    global_defs {
        notification_email {
            root@localhost;
            }
        notification_email_from keepadmin@localhost
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        route_id nginx1
        vrrp_mcast_group4 224.51.151.251
    }
    vrrp_script chk_down{
        script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0 "
        interval 1
        weight -5
        fall 1
        rise 1
    }
    vrrp_script chk_nginx{
        script "killall -0 nginx && exit 0 || exit 1"
        interval 1
        weight -5
        fall 2
        rise 2
    }

    vrrp_instance VI_1{
        state MASTER
        interface ens37
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {

            auth_type PASS
            auth_pass SWF5FW2DF
        }
        virtual_ipaddress {
            172.16.51.1/16 dev ens37 label ens37:6
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_bachup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }
    vrrp_instance VI_2{
        state BACKUP
        interface ens37
        virtual_router_id 52
        priority 96
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 7D2SS5DF
        }
        virtual_ipaddress {
            172.16.51.2/16 dev ens37 label ens37:8
        }
        track_script {
            chk_down
            chk_nginx
        }
        notify_master "/etc/keepalived/notify.sh master"
        notify_bachup "/etc/keepalived/notify.sh backup"
        notify_fault "/etc/keepalived/notify.sh fault"
    }

#启动keeplived
systemctl start keepalived

proxy2

#修改主机名    
hostnamectl set-hostname proxy2.easy.com

#同步时间
yum install -y ntp
ntpdate 172.16.0.1
yum install -y psmisc #killall指令安装

#安装nginx
yum install -y nginx

#从proxy 1 复制代理配置
scp /etc/nginx/nginx.conf 192.168.1.232:/etc/nginx

#启动Nginx
systemctl start nginx

#安装keepalived
yum install -y keepalived

#复制keepalived配置并修改权限
scp /etc/keepalived/keepalived.conf 192.168.1.232:/etc/keepalived/
vim /etc/keepalived/keepalived.conf     
    vrrp_instance VI_1{
        state BACKUP
        priority 96
    vrrp_instance VI_2{
        state MASTER
        priority 100

原创文章,作者:easyTang,如若转载,请注明出处:http://www.178linux.com/81267

(0)
easyTangeasyTang
上一篇 2017-07-15 21:16
下一篇 2017-07-16 10:20

相关推荐

  • LAMP及部署wordpress/phpMyadmin

    LAMP详解 wordpress安装 一、引言 lamp含义:黄金组合。简要介绍一下下面这四个东西吧。linux,不用说了有很多发行版本,主流的三大版本是Debian系列,RedHat系列,slackware系列。apache,全称叫Apache HTTP Server,是世界使用排名第一的web服务器软件,httpd是超文本传输协议http服务器的主程序。…

    Linux干货 2016-12-13
  • 初入linux_基础(一)

    初入Linux知识点总结(一) 一、理论知识 1、时间点: 1946年 第一代计算机ENIAC诞生于美国宾州大学,特点:块头大、效率低 数学家冯诺•依曼提出计算机体系结构组成部分:计算器、运算器、存储器、输入设备、输出设备 1969年 UNIX在美国贝尔实验室诞生 1984年 GNU项目和软件基金会成立 1991年 Linux在芬兰赫尔辛基诞生 2、计算机的…

    Linux干货 2017-02-18
  • date(时间),timedatectl(时区),cal(日历)的用法

    date+%F 显示日期,   显示格式如 2017-07-15+%T 显示时间    显示格式如 15:00:15+%Y 显示年      显示格式如 2017+%m 月 +%d 日+%H 时+%M 分+%S 秒+%s 从linux初始到现在经历了多少秒+%w 显示数字形式的星期+%a …

    Linux干货 2017-07-14
  • shell脚本编程和文件查找及压缩

    shell脚本编程 read:使用read来把输入值分配一个或多个shell变量     -p 指定要显示的提示     -t TIMEOUT     read 从标准输入中读取值,给每个单词分配一个变量   &nbsp…

    Linux干货 2016-08-18
  • 超全超详细的HTTP状态码大全

    本部分余下的内容会详细地介绍 HTTP 1.1中的状态码。这些状态码被分为五大类:  100-199 用于指定客户端应相应的某些动作。 200-299 用于表示请求成功。 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息。 400-499 用于指出客户端的错误。 500-599 用…

    Linux干货 2015-03-20
  • M20 – 1- 第二周(2):课堂练习与作业

    课堂练习: 1、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件和目录 [root@centos6 ~]# ls /var/l*[[:digit:]]*[[:lower:]] ls: cannot access /var/l*[[:digit:]]*[[:lower:…

    Linux干货 2016-08-02