lnmap实战之负载均衡架构(无高可用)

lnmap实战之负载均衡架构(无高可用)

架构图如下:

image

此次实战软件,全部yum安装

1.准备好机器,同步好时间

192.168.42.150 node1 [负载均衡器]
192.168.42.152 node3 [web2]
192.168.42.153 node4 [web1]
192.168.42.151 node2 [memcached session存储]
192.168.42.154 node5 [nfs 共享存储]
192.168.42.155 node6 [mariadb]

我们这次实战从后面的节点开始吧

2.在node6节点上安装mariadb

安装mariadb

yum install mariadb-server -y

更改基本配置

vim /etc/my.cnf.d/server.cnf
[mysqld] 
skip_name_resolve=1
log-bin=mysql-bin
innodb_file_per_table = 1

启动mariadb

systemctl start mariadb

更改密码

mysqladmin -uroot -p password "root"

登录mysql

mysql -u root -p

添加外部访问账号

MariaDB [(none)]> grant all privileges on *.* to 'magedu'@'192.168.42.%' identified by '123456';
MariaDB [(none)]> flush privileges;

登录测试一下

mysql -u magedu -h 192.168.42.155 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

3.node5 安装nfs

安装nfs

yum install nfs-utils rpcbind -y

添加用户(用户id,组id来自node3,node4的apache用户,我们需要统一)

[root@node5 ~] groupadd -g 48 apache
[root@node5 ~] useradd -r -g 48 -u 48  apache
[root@node5 ~] id apache
uid=48(apache) gid=48(apache) groups=48(apache)

创建共享目录

mkdir -p /data
chown  -R apache.apache /data

配置共享目录

vim  /etc/exports
/data   192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48)

启动rpcbind,nfs

systemctl start rpcbind
systemctl start nfs

设置开机启动 因为需要先启动rpcbind,然后再启动nfs,因此我们把开机启动放入rc.local里

chmod +x  /etc/rc.local/rc.local
vim /etc/rc.d/rc.local
systemctl start rpcbind
systemctl start nfs

查看此时的端口

ss -tnl
State      Recv-Q Send-Q   Local Address:Port        Peer Address:Port              
LISTEN     0      128                  *:9743                   *:*                  
LISTEN     0      128                  *:111                    *:*                  
LISTEN     0      128                  *:20048                  *:*                  
LISTEN     0      128                  *:22                     *:*                  
LISTEN     0      100          127.0.0.1:25                     *:*                  
LISTEN     0      64                   *:2049                   *:*                  
LISTEN     0      64                   *:28518                  *:*                  
LISTEN     0      64                  :::8812                  :::*                  
LISTEN     0      128                 :::61836                 :::*                  
LISTEN     0      128                 :::111                   :::*                  
LISTEN     0      128                 :::20048                 :::*                  
LISTEN     0      128                 :::22                    :::*                  
LISTEN     0      100                ::1:25                    :::*                  
LISTEN     0      64                  :::2049                  :::*

查看挂载的目录

showmount -e 127.0.0.1
Export list for 127.0.0.1:
/application/data 168.92.168.42.0/24

去node4,node3挂载试一下

yum install nfs-utils rpcbind httpd -y
id apache
uid=48(apache) gid=48(apache) groups=48(apache)
mkdir /test
chown -R apache.apache /test
mount -t  nfs  192.168.42.154:/data  /test

4.node4,node3在测试nfs的时候已经安装过httpd了 所以我们只需要在node4,node3节点上安装php就行了

yum install php php-mysql -y



此步骤只适合2.2版本,yum安装的2.4版本/etc/httpd/conf.d/php.conf已经帮我们添加了
配置apache虚拟主机,并让apache支持php
vim /etc/httpd/conf/httpd.conf
找到对应的位置,添加
ServerName localhost:80
找到对应的位置,添加
LoadModule php5_module modules/libphp5.so 
找到对应的位置,添加
AddType application/x-httpd-php .php 
找到对应的位置,添加
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

测试php是否能顺利运行

vim  /var/www/html/index.php
<?php

phpinfo();

?>

测试是否能顺利连接mysql

<?php
  $conn = mysql_connect("192.168.42.155","magedu","123456");
  if($conn){
     echo "Connection success";
  }else{
     echo mysql_error(); 
  }
?>

5.在node2上安装memcached

yum install libevent libevent-devle memcached -y
systemctl enable memcached
systemctl start memcached
[root@node2 ~] ss -tnl
State      Recv-Q Send-Q    Local Address:Port   Peer Address:Port              
LISTEN     0      128                   *:22                *:*                  
LISTEN     0      100           127.0.0.1:25                *:*                  
LISTEN     0      1024                  *:11211             *:*                  
LISTEN     0      128                  :::22               :::*                  
LISTEN     0      100                 ::1:25               :::*                  
LISTEN     0      1024                 :::11211            :::*

6.在node4,node3上操作,将php的session存储设置为memcached

(1)安装memcached扩展

yum install php-pecl-memcache* -y

(2)设置php的session存储

vim /etc/httpd/conf.d/php.conf
php_value session.save_handler "memcache"
php_value session.save_path    "tcp://192.168.42.151:11211"

(3)用例子测试一下:

代码如下:

vim /var/www/html/session.php

<?php  
    session_start();  
    if (!isset($_SESSION['TEST'])) {  
        $_SESSION['TEST'] = "set web1 ok";  
    }  

    echo  $_SESSION['TEST'];  
?>

(4).用另一个文件查看一下:

代码如下:

vim /var/www/html/get.php
<?php
session_start();
echo $_SESSION['TEST'];
?>

7.在node1上安装nginx 负载均衡器

yum install nginx -y

vim /etc/nginx/conf.d/test.conf
server {
  listen 80;
  server_name www.test.com;
  location / {
        proxy_pass http://sshsrvs;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

负载均衡配置

vim /etc/nginx/nginx.conf
upstream sshsrvs {
    server 192.168.42.152:80;
    server 192.168.42.153:80;
    least_conn;
}

8.在node4,node3添加虚拟主机测试
node3:

创建应用目录
mkdir -p /application/discuz
chown -R apache.apache /application/discuz
测试页
echo "this is discuz_1 test page." > /application/discuz/index.html
定义discuz虚拟主机
vim /etc/httpd/conf.d/discuz.conf

<VirtualHost *:80>
        ServerName test.discuz.com
        DocumentRoot "/application/discuz"
        <Directory "/application/discuz">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access_log" combined
</VirtualHost>

域名解析
vim /etc/hosts
192.168.42.152 test.discuz.com

重启apache
systemctl restart httpd

node4:

创建应用目录
mkdir -p /application/discuz
chown -R apache.apache /application/discuz
测试页
echo "this is discuz_4 test page." > /application/discuz/index.html

定义discuz虚拟主机
vim /etc/httpd/conf.d/discuz.conf

<VirtualHost *:80>
        ServerName test.discuz.com
        DocumentRoot "/application/discuz"
        <Directory "/application/discuz">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access_log" combined
</VirtualHost>

域名解析
vim /etc/hosts
192.168.42.153 test.discuz.com

重启apache
systemctl restart httpd

9.在负载均衡上添加域名解析并测试

vim /etc/hosts
192.168.42.150  test.discuz.com

添加discuz虚拟主机

vim /etc/nginx/conf.d/discuz.conf

server {
  listen 80;
  server_name test.discuz.com;
  location / {
        proxy_pass http://sshsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

测试test.discuz.com

[root@node1 conf.d] for i in {1..10};do curl test.discuz.com ; done
this is discuz_1 test page.
this is discuz_4 test page.
this is discuz_1 test page.
this is discuz_4 test page.
this is discuz_1 test page.
this is discuz_4 test page.
this is discuz_1 test page.
this is discuz_4 test page.
this is discuz_1 test page.
this is discuz_4 test page.

负载均衡效果已经出来了

10.安装discuz测试
为了避免出错发生,我们需要把负载均衡,卸掉一个,留一个,因为我们是从物理机的浏览器安装discuz
在node1上注释掉一个

vim /etc/nginx/nginx.conf
upstream sshsrvs {
    server 192.168.42.152:80;
    server 192.168.42.153:80;
    least_conn;
}

重启nginx

systemctl restart nginx

测试一下

[root@node1 nginx] for i in {1..10};do curl test.discuz.com ; done
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.
this is discuz_1 test page.

进入node3的/application/discuz目录下载discuz

wget -c http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip
yum install unzip -y
unzip Discuz_X3.3_SC_UTF8.zip
ls
[root@node3 discuz] ls
Discuz_X3.3_SC_UTF8.zip  index.html  readme  upload  utility

删掉之前测试的index.html

rm -f index.html

我们可以看到discuz的目录层次有点深,因此我们需要把网站的根目录指向upload

vim /etc/httpd/conf.d/discuz.conf
DocumentRoot "/application/discuz/upload"

重启apache

systemctl restart httpd

因为discuz需要检查目录权限,因此我们

chown -R apache.apache /application/discuz

添加物理机hosts域名解析

192.168.42.150 test.discuz.com

浏览器输入 test.discuz.com

可以看到以下效果:

image

按照提示操作即可完成discuz安装

11.discuz安装完成以后,我们需要把文件推送到node4节点上

cd /application
scp -rp discuz root@192.168.42.153:/application

推送完成以后

node4操作:

cd /application/discuz
rm -f index.html
chown -R apache.apache /application/discuz

更改虚拟主机discuz的根目录为upload

vim /etc/httpd/conf.d/discuz.conf
DocumentRoot "/application/discuz/upload"

完成上述操作以后,还记得之前在node1上注释的吗,我们需要把它打开

vim /etc/nginx/nginx.conf
upstream sshsrvs {
    server 192.168.42.152:80;
    server 192.168.42.153:80;
    least_conn;
}

重启nginx

12.在去浏览器中访问,登录后台,操作等,一切正常

13.还有最后一个问题需要处理,discuz的数据目录,共享存储 在node5上操作:

mkdir -p  /data/discuz/

进入node3 把data推送过来

cd /application/discuz/upload
scp -rp uc_server root@192.168.42.154:/data/discuz/
scp -rp data root@192.168.42.154:/data/discuz/

在node5上操作:

vim /etc/exports
/data/discuz/data   192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48)
/data/discuz/uc_server   192.168.42.0/24(rw,sync,all_squash,anonuid=48,anongid=48)
exportfs -rv
chown -R  apache.apache /data

在node3上挂载并加入开机挂载

cd  /application/discuz/upload/data
rm -rf * 
umount /test
cd ../
mount -t nfs 192.168.42.154:/data/discuz/data  /application/discuz/upload/data
mount -t nfs 192.168.42.154:/data/discuz/uc_server  /application/discuz/upload/uc_server
chmod +x /etc/rc.d/rc.local
echo "mount -t nfs 192.168.42.154:/data/discuz/data  /application/discuz/upload/data" >>/etc/rc.d/rc.local
echo "mount -t nfs 192.168.42.154:/data/discuz/uc_server  /application/discuz/upload/uc_server" >>/etc/rc.d/rc.local

在node4做同样的操作:

至此工作已经全部完成

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

(1)
sraybansrayban
上一篇 2017-06-22
下一篇 2017-06-22

相关推荐

  • Linux进程管理三剑客htop、ps、dstat

    ps命令 Unix有两种主要分支:官方的Unix(AT&T)和非官方的Unix(加利福利亚大学伯克利分校)。UNIX("带-")和BSD(不带东西)都有自己的ps版本,并且ps版本都很出名,并被广泛使用。最终结果是许多现代版本的ps都同时支持两种类型的选项,即所谓的Unix选项和BSD选项。而且还有一组选项支持GUN的选项. 所以…

    Linux干货 2016-11-22
  • 三次握手和四次挥手

    今天来聊一下事实标准协议TCP/IP中传输层里TCP协议中,主机与服务器建立连接时的三次握手,和断开连接时的四次挥手。 本博文分两部分介绍,    一:状态详解    二:三次握手和四次挥手状态介绍 这里总共涉及到十种状态,其实总共有十一种状态,接下来分别介绍一下它们; 一:状态详解 CLOSED:关闭—&…

    2017-09-01
  • 初学Linux之文本处理工具和正则表达

    1、各种文本工具来查看、分析、统计文本 
    2、grep 
    3、正则表达式和扩展正则表达式 
    4、egrep

    2017-12-09
  • 02葵花宝典之bash特性及文件入门

    文件管理 目录 命令替换 命令执行状态 元数据 时间戳

    2018-03-11
  • vim编辑器

    vim编辑器:          文本:ASCII, Unicode(全球统一编码格式)          文本编辑种类:       &n…

    Linux干货 2016-08-10
  • Linux基础(七)-软RAID,LVM,bash脚本之循环

    1.创建一个10G的分区,并格式为ext4的文件系统; (1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;(2)挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@localhost ~]# fdisk /dev/sdb We…

    Linux干货 2016-11-06