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 17:59
下一篇 2017-06-22 22:26

相关推荐

  • Linux网络配置

    配置网络 实现网络组测试网络网络工具     基本网络配置将Linux主机接入到网络,需要配置网络相关设置。一般包括如下内容:主机名IP/netmask路由:默认网关DNS服务器主DNS服务器次DNS服务器第三DNS服务器 网络配置方式静态指定:ifcfg: ifconfig, route, netstatip: object {link…

    Linux干货 2017-08-19
  • Web缓存核心技术点需知

    Edit Web缓存核心技术点需知 5.1 HTTP首部控制 5.2 基于新鲜度检测机制: 2.1 特征1:时间局部性 2.2 特征2:空间局部性 2.3 缓存的优点 2.4 哪类数据应该被缓存 2.5 哪类数据可缓存但不应该被缓存 2.6 缓存命中率决定缓存有效性 2.7 缓存数据生命周期 2.8 缓存处理步骤 2.9 缓存和普通数据读取的区别 1. 完整…

    Linux干货 2016-11-14
  • N25-第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@node1 ~]# who | cut -d" " -f1 | uniq (unknown) root 2、取出最后登录到当前系统的用户的相关信息…

    Linux干货 2016-12-17
  • 文本处理三剑客之一的sed

    处理文本的工具sed Stream EDitor, 行编辑器 sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Se…

    Linux干货 2017-04-27
  • FHS文件系统下个各目录功能

    FHS文件系统下个各目录功能 FHS文件系统的建立是为了让开发者和用户可以预测软件安装文件和文件夹的位置。对整个linux的文件系统系统做了以下的规范:     /bin:命令二进制文件的存放目录;     /boot:系统启动时一些文件存放的目录,包含引导linux的重要文件,…

    Linux干货 2016-10-18
  • 网络管理基础-子网划分及网络配置练习

    1、某公司申请到一个C 类IP 地址,但要连接6 个的子公司,最大的一个子  公司有26 台计算机,每个子公司在一个网段中,则子网掩码应设为?  192.168.100.1 网络位192.168.100 192.168.100.00000000 C类掩码255.255.255.0 192.168.100. 000 00001  …

    Linux干货 2016-09-05