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 yum客户端的配置及yum命令

    一、什么是yum我们在Linux系统上安装处理软件,一般是使用RPM,它是通过预先编译完成并且把软件打包为RPM文件格式后,再加以安装的一种方式,使用者只要拿到这个打包好的软件,然后将里头的文件放置到应该摆放的目录,这样就完成了安装。但是,由于有些软件是有依赖于其他软件的,当你要安装某个RPM类型的软件时,RPM会检验RPM软件数据库,它所依赖的相关软件包是…

    2017-06-11
  • 实例;编译安装apache 2.2,并启动此服务

    基于c语言的源代码编译安装,通常有三个步骤     ./configure         (1)通过选项传递参数,指定启用特性,安装路径等:执行时会参考用户的指定以及makefile.in文件生成makefile.  &nbsp…

    Linux干货 2016-08-24
  • 马哥教育网络班22期第5周课程作业

    1、显示当前系统上root、fedora或user1用户的默认shell; cat /etc/passwd | grep ^root | cut -d: -f7 2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello(); …

    Linux干货 2016-12-05
  • 从LongAdder看更高效的无锁实现

    接触到AtomicLong的原因是在看guava的LoadingCache相关代码时,关于LoadingCache,其实思路也非常简单清晰:用模板模式解决了缓存不命中时获取数据的逻辑,这个思路我早前也正好在项目中使用到。 言归正传,为什么说LongAdder引起了我的注意,原因有二: 作者是Doug lea ,地位实在举足轻重。 他说这个比AtomicLon…

    Linux干货 2016-06-01
  • [转]百万级访问网站前期的技术准备

       [转]百万级访问网站前期的技术准备 开了自己域名的博客,第一篇就得来个重磅一点的才对得起这4美金的域名。作为一个技术从业者十年,逛了十年发现有些知识东一榔头西一棒槌的得满世界  看个遍才整理出个头绪,那咱就系统点的从头一步一步的说,一个从日几千访问的小小网站,到日访问一两百万的小网站,怎么才能让它平滑的度过这个阶段,别在 …

    Linux干货 2016-10-29
  • raid5创建

    创建分区并改类型 创建raid5   创建把文件系统 生成配置文件    

    2017-12-17