使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第1张图片

环境:

202.106.0.6: as client
202.106.0.17:as PowerDNS
192.168.205.27: as LAMP+Wordpress
192.168.205.37: as LAMP+Wordpress
192.168.205.47: as Mariadb master
192.168.205.57: as Mariadb slave
192.168.205.67: as NFS share files with inotify sync with backup server
192.168.205.77: as rsync server for backup NFS
192.168.205.87: as lvs
192.168.205.97: as Router (linux with ip forward)

注:所有操作系统默认停用firewalld,iptable为空,关闭selinux

版本:

OS: centos 7 1810 with mini install
poweradmin-2.1.7.tgz
mariadb-10.2.25-linux-x86_64.tar.gz
apr-1.7.0.tar.bz2
apr-util-1.6.1.tar.bz2
httpd-2.4.39.tar.bz2
php-7.3.7.tar.xz
wordpress-5.2.2.tar.gz
Discuz_X3.3_SC_UTF8.zip

目地:

编译安装apache2.4.39, 编译安装php7.37, 二进制安装mariadb server10.2.25, 并实现数据库的主从复制,通过dns解析web服务器lvs的VIP实现wordpress,和disscuz的负载均衡,并将文件存在nfs共享文件中,通地inotify 和rsync进行实时的备份网站的数据。

步骤:

1. 安装mariadb并配置主从  
2. 在mariadb上准备powerdns的数据库文件和权限  
3. 安装powerdns  
4. 配置rsync server  
5. 配置NFS服务器  
6. 安装apache and php创建WEBSERVERS  
7. 安装lvs服务器  
8. 配置real server  
9. 测试  

安装database在主和从服务器上

  1. copy mariadb to /data
    [root@centos7 data]#ls
    mariadb-10.2.25-linux-x86_64.tar.gz  mysql_install.sh
  2. 执行如下脚本

    [root@centos7 scripts]#cat mysql_install.sh 
    ###################
    #1.install MariaDB
    ###################
    install(){
    #1.1 create user
    yum install -y libaio 
    useradd -r -s /sbin/nologin -d /data/mysql mysql
    mkdir /data/mysql
    mkdir -p /data/logs
    chown mysql.mysql /data/logs
    chown mysql:mysql /data/mysql
    tar xf  mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local
    cd /usr/local
    ln -sv mariadb-10.2.25-linux-x86_64 mysql
    chown -R root.root /usr/local/mysql/
    
    #1.2 modify config file
    cp /usr/local/mysql/support-files/my-huge.cnf /etc/my.cnf
    sed -i '/\[mysqld\]/a datadir=/data/mysql' /etc/my.cnf
    sed -i '/\[mysqld\]/a innodb_file_per_table = on' /etc/my.cnf
    sed -i '/\[mysqld\]/a skip_name_resolve = on' /etc/my.cnf
    sed -i 's@log-bin=mysql-bin@log-bin=/data/logs/bin@' /etc/my.cnf
    cd /usr/local/mysql
    scripts/mysql_install_db --datadir=/data/mysql --user=mysql
    cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    
    #1.3 variables 
    echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    source /etc/profile.d/mysql.sh
    }
    install
  3. 在主服务器中
    sed -i 's@^server-id.*$@server_id = 47@' /etc/my.cnf
    sed -i '/\[mysqld\]/a log-basename=master' /etc/my.cnf
    /usr/local/mysql/bin/mysql -e "grant replication slave on *.* to 'repluser'@'192.168.205.%' identified by 'centos'" 
    service mysqld restart
  4. 主服务器中执行记录下复制的位置
    MariaDB [(none)]> show master logs;
    +------------+-----------+
    | Log_name   | File_size |
    +------------+-----------+
    | bin.000001 |       322 |
    | bin.000002 |       322 |
    | bin.000003 |       322 |
    | bin.000004 |       322 |
    | bin.000005 |       322 |
    | bin.000006 |       322 |
    | bin.000007 |     29295 |
    | bin.000008 |       540 |
    +------------+-----------+
    8 rows in set (0.00 sec)
  5. 从服务器中修改配置文件
    sed -i 's@^server-id.*$@server_id = 57@' /etc/my.cnf
    sed -i '/\[mysqld\]/a read_only=ON' /etc/my.cnf
    sed -i '/\[mysqld\]/a relay_log=relay-log' /etc/my.cnf
    sed -i '/\[mysqld\]/a relay_log_index=relay-log.index' /etc/my.cnf
    service mysqld restart
  6. 从服务器修改change master to
    MariaDB [(none)]> CHANGE MASTER TO
        ->   MASTER_HOST='192.168.205.47',
        ->   MASTER_USER='repluser',
        ->   MASTER_PASSWORD='centos',
        ->   MASTER_PORT=3306,
        ->   MASTER_LOG_FILE='bin.000008',
        ->   MASTER_LOG_POS=540;
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> start slave;
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [(none)]> show slave status\G
  7. 如果出现错误或change master to错误,可以使用如下并重新change master to
    stop slave;
    reset slave all;
  8. 建库测试是否正常
    create database zhaoli;
    show databases;
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | zhaoli             |
    +--------------------+
    5 rows in set (0.00 sec)

    在server 192.168.205.47上准备PowerDNS的数据库,表和用户

  9. 创建数据库
    CREATE DATABASE powerdns;
    GRANT ALL ON powerdns.* TO 'powerdns'@'192.168.205.%' IDENTIFIED BY 'centos';
  10. 创建powerdns数据库中的表
        CREATE TABLE domains (
    id                    INT AUTO_INCREMENT,
    name                  VARCHAR(255) NOT NULL,
    master                VARCHAR(128) DEFAULT NULL,
    last_check            INT DEFAULT NULL,
    type                  VARCHAR(6) NOT NULL,
    notified_serial       INT DEFAULT NULL,
    account               VARCHAR(40) DEFAULT NULL,
    PRIMARY KEY (id)
    ) Engine=InnoDB;
        CREATE UNIQUE INDEX name_index ON domains(name);
        CREATE TABLE records (
    id                    BIGINT AUTO_INCREMENT,
    domain_id             INT DEFAULT NULL,
    name                  VARCHAR(255) DEFAULT NULL,
    type                  VARCHAR(10) DEFAULT NULL,
    content               VARCHAR(64000) DEFAULT NULL,
    ttl                   INT DEFAULT NULL,
    prio                  INT DEFAULT NULL,
    change_date           INT DEFAULT NULL,
    disabled              TINYINT(1) DEFAULT 0,
    ordername             VARCHAR(255) BINARY DEFAULT NULL,
    auth                  TINYINT(1) DEFAULT 1,
    PRIMARY KEY (id)
    ) Engine=InnoDB;
        CREATE INDEX nametype_index ON records(name,type);
    CREATE INDEX domain_id ON records(domain_id);
    CREATE INDEX recordorder ON records (domain_id, ordername);
        CREATE TABLE supermasters (
    ip                    VARCHAR(64) NOT NULL,
    nameserver            VARCHAR(255) NOT NULL,
    account               VARCHAR(40) NOT NULL,
    PRIMARY KEY (ip, nameserver)
    ) Engine=InnoDB;
        CREATE TABLE comments (
    id                    INT AUTO_INCREMENT,
    domain_id             INT NOT NULL,
    name                  VARCHAR(255) NOT NULL,
    type                  VARCHAR(10) NOT NULL,
    modified_at           INT NOT NULL,
    account               VARCHAR(40) NOT NULL,
    comment               VARCHAR(64000) NOT NULL,
    PRIMARY KEY (id)
    ) Engine=InnoDB;
        CREATE INDEX comments_domain_id_idx ON comments (domain_id);
    CREATE INDEX comments_name_type_idx ON comments (name, type);
    CREATE INDEX comments_order_idx ON comments (domain_id, modified_at);
        CREATE TABLE domainmetadata (
    id                    INT AUTO_INCREMENT,
    domain_id             INT NOT NULL,
    kind                  VARCHAR(32),
    content               TEXT,
    PRIMARY KEY (id)
    ) Engine=InnoDB;
        CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind);
        CREATE TABLE cryptokeys (
    id                    INT AUTO_INCREMENT,
    domain_id             INT NOT NULL,
    flags                 INT NOT NULL,
    active                BOOL,
    content               TEXT,
    PRIMARY KEY(id)
    ) Engine=InnoDB;
        CREATE INDEX domainidindex ON cryptokeys(domain_id);
        CREATE TABLE tsigkeys (
    id                    INT AUTO_INCREMENT,
    name                  VARCHAR(255),
    algorithm             VARCHAR(50),
    secret                VARCHAR(255),
    PRIMARY KEY (id)
    ) Engine=InnoDB;
        CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm);

    安装powerDNS在服务器192.168.205.17

  11. 安装包:基于EPEL源
    yum install -y pdns pdns-backend-mysql
  12. 配置PowerDNS使用mariadb作为后台数据存储
    vim /etc/pdns/pdns.conf,查找到包含launch= 的行,修改并添加下面的内容
    launch=gmysql
    gmysql-host=localhost
    gmysql-port=3306
    gmysql-dbname=powerdns
    gmysql-user=powerdns
    gmysql-password=magedu
  13. 启动服务
    systemctl start pdns
    systemctl enable pdns
  14. 安装httpd和php相关包
    yum -y install httpd php php-devel php-gd php-mcrypt php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mhash gettext
    systemctl start httpd
    systemctl enable httpd
  15. 下载poweradmin程序,并解压缩到相应目录
    cd /var/www/html
    wget http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz
    tar xvf poweradmin-2.1.7.tgz
    mv poweradmin-2.1.7 poweradmin
  16. 访问下面地址,启动PowerAdmin的网页安装向导,选择英文然后 go to step2:
    http://192.168.205.17/poweradmin/install/

  17. 提供先前配置的数据库详情,同时为Poweradmin设置管理员密码
    Username: 为上面9步创建的用户名,此处应该为powerdns
    password:为上面9步创建的密码应该为centos
    database type :为myql
    hostname: 为mysql主服务器的IP
    DB port: 为默认的3306
    database: 为上面9步创建的数据库名称powerdns
    poweradmin adminstrator pasword: 此处为powerdns会自动创建一个web管理员名为admin的用户,此为admin的密码

    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第2张图片

  18. 为Poweradmin创建一个受限用户,powerDNS会使用这个帐户来更新powerdns数据库,上面的只是用来连接数据库,此处为真正更新时使用的帐户
    Username:更新的用户名
    Password:上述用户的密码
    Hostmaster:当创建SOA记录指定默认主机管理员
    Primary nameserver:主域名服务器,此处应该为192.168.205.17
    Secondary namesever: 辅域名服务器,  没有辅助服务器,可以不添

    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第3张图片

  19. 跟据上面的输入,他会自动产生sql语句,按照下面页面说明,在数据库中192.168.205.47创建用户并授权
    MariaDB [(none)]>GRANT SELECT, INSERT, UPDATE, DELETE ON powerdns.* TO 'poweradmin'@'192.168.205.17' IDENTIFIED BY 'centos1';

    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第4张图片

  20. 按下面页面说明,创建config.in.php文件内容
    vim /var/www/html/poweradmin/inc/config.inc.php

    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第5张图片

  21. 按照提示安装完毕后,删除install目录
    rm -rf /var/www/html/poweradmin/install/

    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件

  22. 登录http://202.106.0.17/poweradmin/
    username:admin
    password:root123 参看第17步
  23. 添加主机记录在powerdns上
    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第6张图片
  24. 在客户端的windows中测试dns解析
    ping blog.zhaoli.com
    ping forum.zhaoli.com

    配置192.168.205.77为rsync server

  25. 安装rsync服务
    #yum install rsync
  26. 编辑rsyncd.conf文件,让rsyncc以daemon的方式运行
    #vi /etc/rsyncd.conf
    uid = root  #以什么身份开启服务
    gid = root
    use chroot = no   
    max connections = 0  #不限制连接
    ignore errors  忽略错误
    exclude = lost+found/
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    reverse lookup = no  反向解析名称与ip
    hosts allow = 192.168.205.0/24  充许连接的主机列表
    [backup]   起名子
    path = /data/www_backup/
    comment = webserver www backup
    read only = no  可以写
    auth users = rsync
    secrets file = /etc/rsync.pass  密码文件
  27. 服务器端生成验证文件
    #echo "rsync:centos" > /etc/rsync.pass
    #chmod 600 /etc/rsync.pass
  28. 服务器端准备目录
    #mkdir /data/www_backup
  29. 服务器端启动rsync服务
    #systemctl start rsyncd 

    配置NFS服务器192.168.205.67

  30. 安装相关的软件包
    #yum -y install nfs-utils rsync
  31. 创建目录
    mkdir /data/www
    chmod 777 /data/www
  32. 创建用户apache用户
    #useradd -r -s /sbin/nologin apache   
    #id apache
    uid=998(apache) gid=996(apache) groups=996(apache)
  33. 修改NFS配置文件,共享www目录
    #vi /etc/exports
    /data/www 192.168.205.0/24(rw,all_squash,anonuid=998,anongid=996)     
    #systemctl restart nfs-server
  34. 将NFS服务器配置为rsync的客户端,先配置密码文件
    #echo "centos" > /etc/rsync.pass
    #chmod 600 /etc/rsync.pass
  35. 安装inotify实现实时同步数据,客户端创建inotify_rsync.sh脚本
    #!/bin/bash
    SRC='/data/www'
    DEST='[email protected]::backup'
    inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' \
    -e create,delete,moved_to,close_write,attrib ${SRC} | \
    while read DATE TIME DIR FILE;do
        FILEPATH=${DIR}${FILE}
        rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} \
        on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
    done
  36. 将脚本文件存在rc.local中起动进自动执行
    #chmod +x inotify_rsync.sh
    #vi /etc/rc.d/rc.local
    /data/inotify_rsync.sh&   
    #chmod +x /etc/rc.d/rc.local
  37. 解压缩文件 wordpress-5.2.2.tar.gz和Discuz_X3.3_SC_UTF8.zip
    #yum install -y unzip
    #pwd
    /data
    #unzip Discuz_X3.3_SC_UTF8.zip 
    #mv upload/ www/forum
    #tar xf wordpress-5.2.2.tar.gz -C www
    #mv wordpress /app/httpd24/htdocs/
    #ls
    forum  index.php  wordpress
  38. 在rsync服务器192.168.205.77上查看客户端测试同步数据
    [root@centos7 www_backup]#ls
    forum  index.php  wordpress

    创建WEBSERVER and WEBSERVER2

  39. 在安装apache和php之前请开启epel源
    [root@centos7 php]#cat /etc/yum.repos.d/epel.repo 
    [EPEL]
    name=Tsinghua epel
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64/
    gpgcheck=0
    enabled=1
  40. 准备如下文件到一个目录中
    [root@centos7 data]#ls
    apache_php_install.sh  apr-util-1.6.1.tar.bz2  php-7.3.7.tar.xz     
    apr-1.7.0.tar.bz2      httpd-2.4.39.tar.bz2   
  41. 安装apache+PHP使用如下脚本

    ./apache_php_install.sh http
    ./apache_php_install.sh php
    [root@centos7 data]#cat apache_php_install.sh 
    #!/bin/bash
    #########################
    #difination variables
    #########################
    TMP_DIR=`pwd`
    APR="apr-1.7.0.tar.bz2"
    APR_UTIL="apr-util-1.6.1.tar.bz2"
    HTTPD="httpd-2.4.39.tar.bz2"
    PHP="php-7.3.7.tar.xz"
    HTTPD_DIR=`echo "$HTTPD" |rev | cut -d. -f3- | rev`
    APR_DIR=`echo "$APR" |rev | cut -d. -f3- | rev`
    APR_UTIL_DIR=`echo "$APR_UTIL" | rev | cut -d. -f3- | rev`
    PHP_DIR=`echo "$PHP" | rev | cut -d. -f3- | rev`
    INS_HTTPD_DIR=/app/httpd24
    INS_PHP_DIR=/app/php
    
    ##########################
    #Packages check and install
    ##########################
    pkg(){
    for i in $PKGS; do
     rpm -q $i &> /dev/null && echo "Packge `rpm -q $i` is installed" || yum -y install $i
    done
    }
    
    #####################
    #APACHE INSTALLATION
    #1.unarchive binary 
    ####################
    #install dependency packages,just add packages name to variable PKGS separate by SPACE.
    #Example PKGS="libaio gcc glibc"  
    httpd_ins(){
    PKGS="gcc prce-devel openssl-devel expat-devel lbzip2"
    pkg
    cd $TMP_DIR
    if [ -e $TMP_DIR/$HTTPD ]; then
            tar xf $HTTPD
    else
            echo "file $HTTPD does not exist, pleases download it" 
            exit
    fi
    
    if [ -e $TMP_DIR/$APR ]; then
            tar xf $APR 
            mv $APR_DIR $HTTPD_DIR/srclib/apr
    else
            echo "file $APR does not exist, pleases download it"
            exit
    fi
    
    if [ -e $TMP_DIR/$APR_UTIL ]; then
            tar xf $APR_UTIL
      mv $APR_UTIL_DIR $HTTPD_DIR/srclib/apr-util
    else
            echo "file $APR_UTIL does not exist, pleases download it"
            exit
    fi
    
    ########################
    #2.make and make install
    ########################
    id apache||useradd -r -s /sbin/nologin apache
    cd $HTTPD_DIR
    ./configure \
    --prefix=$INS_HTTPD_DIR \
    --enable-so \
    --enable-ssl \
    --enable-cgi \
    --enable-rewrite \
    --with-zlib \
    --with-pcre \
    --with-included-apr \
    --enable-modules=most \
    --enable-mpms-shared=all \
    --with-mpm=prefork
    if [ $? -eq 0 ]; then
            make -j 4 && make install
            cd $TMP_DIR
            rm -rf $HTTPD_DIR
    else
            echo "please remake and make install"
    fi
    
    #####################
    #3. modify config files
    ######################
    sed -ri 's@^(.*) daemon$@\1 apache@' $INS_HTTPD_DIR/conf/httpd.conf
    sed -ri 's@DirectoryIndex@DirectoryIndex index.php @' $INS_HTTPD_DIR/conf/httpd.conf
    sed -ri 's@#(LoadModule proxy_module modules/mod_proxy.so)@\1@' $INS_HTTPD_DIR/conf/httpd.conf
    sed -ri 's@#(LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so)@\1@' $INS_HTTPD_DIR/conf/httpd.conf
    sed -ri 's@#(LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so)@\1@' $INS_HTTPD_DIR/conf/httpd.conf
    cat >> $INS_HTTPD_DIR/conf/httpd.conf <<-EOF
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    ProxyRequests Off
    ProxyPassMatch "^/.*\.php(/.*)?$" "fcgi://localhost:9000/app/httpd24/htdocs/"
    EOF
    echo "PATH=$INS_HTTPD_DIR/bin:\$PATH" > /etc/profile.d/httpd.sh
    source /etc/profile.d/httpd.sh
    echo "$INS_HTTPD_DIR/bin/apachectl start" >> /etc/rc.d/rc.local
    chmod +x /etc/rc.d/rc.local
    apachectl start
    }
    
    #########################
    #PHP INSALLATION
    #1. unarchive install packge
    #########################
    php_ins(){
    PKGS="libxml2-devel bzip2-devel libmcrypt-devel gd-devel"
    PKG
    cd $TMP_DIR
    echo $TMP_DIR
    if [ -e $TMP_DIR/$PHP ]; then
            tar xvf $PHP
    else
            echo "files $PHP does not exist, pleases download it"
            exit
    fi
    #######################
    #2. make and make install
    #######################
    cd $PHP_DIR/
    ./configure \
    --prefix=$INS_PHP_DIR \
    --enable-mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-openssl \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --with-libxml-dir=/usr \
    --with-config-file-path=/etc \
    --with-config-file-scan-dir=/etc/php.d \
    --enable-mbstring \
    --enable-xml \
    --enable-sockets \
    --enable-fpm \
    --enable-maintainer-zts \
    --disable-fileinfo  \
    --with-gd \
    --with-imap\
    --with-ldap\
    --with-odbcver\
    --with-iodbc\
    --with-pear\
    --with-libxml-dir\
    --with-xmlrpc\
    --enable-mbstring\
    --with-mhash\
    --with-gettext
    if [ $? -eq 0 ]; then 
            make && make install
    else
            echo "please re make and make install"
    fi
    ######################
    #3. modify config file
    ######################
    cp php.ini-production /etc/php.ini
    cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm 
    chmod +x /etc/init.d/php-fpm
    cd $INS_PHP_DIR/etc
    cp php-fpm.conf.default php-fpm.conf
    cd php-fpm.d/
    cp www.conf.default www.conf
    sed -ri 's@(^.*) = nobody@\1 = apache@' $INS_PHP_DIR/etc/php-fpm.d/www.conf
    chkconfig --add php-fpm
    service php-fpm start
    cd $TMP_DIR
    rm -rf $PHP_DIR  
    echo '' > /app/httpd24/htdocs/index.php 
    }
    
    ##################
    #4. remove PHP
    #################
    rmphp(){
    service php-rpm stop
    rm /app/php -rf
    rm /etc/php.ini -f
    chkconfig --del php-fpm
    rm /etc/init.d/php-fpm -f
    }
    
    ################
    #5. remove HTTPD
    ################
    rmhttpd(){
    apachectl stop
    rm /app/httpd24 -rf
    sed -i '/\/app\/http24\/bin\/apachectl start/d' /etc/rc.d/rc.local
    rm /etc/profile.d/httpd.sh
    }
    
    ###############
    #OPTIONS SELECT
    ###############
    case $1 in 
    install)
            case $2 in
                    httpd)
                    httpd_ins
                    ;;
            php)
                    php_ins
                    ;;
            *)
                    echo "Useage $0 $1 httpd|php"
                    ;;
            esac
            ;;
    remove)
            case $2 in
                    php)
                    rmphp
                    ;;
                    httpd)
              rmhttpd
                    ;;
                    *)
                    echo "Useage $0 $1 httpd|php"
                    ;;
            esac
            ;; 
    *)
            echo "Useage $0  "
            ;;
    esac
  42. 安装nfs-utils工具才可以mount NFS文件系统
    #yum install nfs-utils 
  43. 测试连接NFS服务器的共享文件
    #mount 192.168.205.67:/data/www /data/www
    #df
  44. 将mount写入到fstab文件中,实现开机自动mount
    #vi /etc/fstab
    192.168.205.67:/data/www  /app/httpd24/htdocs nfs defaults  0 0  
  45. 测试php代码是否正常工作
    echo '' > /app/httpd24/htdocs/index.php
    http://192.168.205.27/index.php
  46. 修改httpd.conf配置文件,使其支持两个虚拟站点

    
    servername blog.zhaoli.com
    documentroot /data/www/wordpress
    
    require all granted
    
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/wordpress/$1
    
    
    
    servername forum.zholi.com
    documentroot /data/www/discuz
    
    require all granted
    
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/forum/$1
        
    #apachectl restart
  47. 从复39到46安装第二台web server

    安装router 192.168.205.97

  48. 修改ipv4 forwarding
    #echo 1 > /proc/sys/net/ipv4/ip_forward
    #vi /etc/sysctl.conf    
    net.ipv4.ip_forward=1 
  49. 添加一台静态路由指定lvs 服务器
    #ip r a 60.1.1.100/32 via 192.168.205.87 dev eth0
    #ip r
    60.1.1.100 via 192.168.205.87 dev eth0 
    192.168.205.0/24 dev eth0 proto kernel scope link src 192.168.205.97 metric 101 
    202.106.0.0/24 dev eth1 proto kernel scope link src 202.106.0.97 metric 100 

    安装lvs服务器

  50. 安装包管理工具
    #yum -y install ipvsadm
  51. 添加IP地址到lo网卡学
    #ip a a dev lo:1 60.1.1.100/32 
    #vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    IPADDR=192.168.205.87
    NETMASK=255.255.255.0
    GATEWAY=192.168.205.97
    #ip r
    default via 192.168.205.97 dev eth0 proto static metric 102 
    192.168.205.0/24 dev eth0 proto kernel scope link src 192.168.205.87 metric 102 
  52. 确保firewalld关闭,并iptables是关闭的
    #iptables -vnL
    #systemctl status firewalld
  53. 添加ipvsadm的策略,默认使用的是DR模试,这里为演示方便使用RR调度方式
    [root@centos7 network-scripts]#ipvsadm -A -t 60.1.1.100:80 -s rr
    [root@centos7 network-scripts]#ipvsadm -a -t 60.1.1.100:80 -r 192.168.205.27 
    [root@centos7 network-scripts]#ipvsadm -a -t 60.1.1.100:80 -r 192.168.205.37  
    [root@centos7 network-scripts]#ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  60.1.1.100:80 rr
      -> 192.168.205.27:80            Route   1      0          0         
      -> 192.168.205.37:80            Route   1      0          0         

    配置两个web server为lvs的real server

  54. 修改arp选项同时在两台服务器上
    [root@webser1 ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    [root@webser1 ~]#echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    [root@webser1 ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    [root@webser1 ~]#echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
  55. 修改网卡的默认网关及ip
    [root@webser1 network-scripts]#vi ifcfg-eth0 
    IPADDR=192.168.205.27
    NETMASK=255.255.255.0
    GATEWAY=192.168.205.97       
  56. 添加虚拟IP到lo网卡中
    [root@webser1 network-scripts]#ip a a dev lo:1 60.1.1.100/32 
    [root@webser1 network-scripts]#ip a
    [root@webser1 network-scripts]#ip r

    测试:

  57. 打开windows7设置DNS为202.106.0.17,分别浏览器中输入blog.zhaoli.com, forum.zhaoli.com,
    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第7张图片
    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第8张图片
  58. 为了更加清晰的看到lvs的RR作用,我们把web server1 和2 页面改一下,将两上虚拟网站删除,将文件存放在本地中,并修改两个index.html

    [root@webser1 htdocs]#cat index.html
    this is webser1
    
    [root@webser2 htdocs]#cat index.html 
    this is webser2
    curl http://blog.zhaoli.com/index.html
    this is webser1
    curl http://blog.zhaoli.com/index.html
    this is webser2
  59. 我们使用个循环来试一下,可以看到,两个网站分别轮询调用,lvs的RR效果明显
    使用LVS实现web站点负载均衡,web服务器使用NFS挂载方式共享文件_第9张图片