LVS的简单示例

   

 1.使用NAT模型的TCP协议类型的lvs服务负载均衡一个php应用,如Discuz!论坛或者phpMyAdmin;

   必要条件:三台主机(一个调度器两个服务器)

   声明:172.16.1.11作为调度器,192.168.100.2和192.168.100.3作为服务器

      第一步:给调度器配置一个网络适配器,配置其IP地址为192.168.100.1

      第二步:

        服务器配置(Real Server即RS):

          配置两个RS的为同一网关:

            # route add default gw 192.168.100.1

          搭建LAMP来提供php服务:

            安装httpd,php,php-mysql,mariadb-server(mysql-server)

            # yum insatll httpd php php-mysql mariadb-server(mysql-server)

          RS1:1) 配置虚拟主机:

              # cd /etc/httpd/conf.d/

              # vim vhost1.conf

              <VirtualHost 192.168.100.2:80>

                 ServerName

                 DocumentRoot "/myweb/vhost1"

                 DirectoryIndex  index.php

                 <Diretory "/myweb/vhost1">

                   Options  None

                   AllowOverride None

                   Require all granted

                 </Diretory>

              </VirtualHost>

             2) 完善虚拟主机的目录要求:

             # mkdir -pv /myweb/vhost1

             # cd /myweb/vhost1/

            3)做测试页面,查看php能否提供服务

                # vim index.php

              Real Server 1 <dr>

              ?php>

                phpinfo();

              ?>

             4) 开启httpd服务:

             # systemtl start httpd.service

   

          RS2:1) 配置虚拟主机

             # cd /etc/httpd/conf.d/

             # vim vhost2.conf

              <VirtualHost 192.168.100.3:80>

                 ServerName

                 DocumentRoot "/myweb/vhost2"

                 DirectoryIndex  index.php

                 <Diretory "/myweb/vhost2">

                   Options  None

                   AllowOverride None

                   Require all granted

                 </Diretory>

              </VirtualHost>

          2) 完善虚拟主机的目录要求:

             # mkdir -pv /myweb/vhost2

             # cd /myweb/vhost2/

          3)做测试页面,查看php能否提供服务

                # vim index.php

              Real Server 2 <dr>

              ?php>

                phpinfo();

              ?>

          4) 开启httpd服务:

             # systemtl start httpd.service

      第三步:调度器配置:

          1)开启核心路由转发功能:

             # echo 1 > /proc.sys/net/ipv4/ip_forward

          2) 创建集群服务:

             # ipvsadm -A -t 172.16.1.11:80 -s rr(前提安装ipvsadm:yum install ipvsadm )

          3) 向集群中添加RS:

             # ipvsadm -a -t 172.16.1.11:80 -r 192.168.100.2:80 -m -w 1

             # ipvsadm -a -t 172.16.1.11:80 -r 192.168.100.3:80 -m -w 1

          注意:需注意SElinux 和 iptables的干扰:

            我将三台主机做了如下操作:

             # iptables -F

                # setenforce 0

       配置完毕后:

            web端测试:

            第一次输入:172.16.1.11/

                               QQ截图20180606163705.png

                               第二次输入:172.16.1.11/

          QQ截图20180606163759.pngf

                             测试完后,php可以成功提供服务:

          接下来,在RS1和RS2上配置wordpress应用:

          将wordpress应用分别传送到RS1的/myweb/vhost1和RS2的/myweb/vhost2目录下并解压:

           # tar xf wordpress-4.2-zh_CN.tar.gz

          在web端测试:

           输入172.16.1.11/wordpress/index.php

           QQ截图20180606164455.png

                       需要注意的是:

            1)调度器需要进行会话保持,如果不进行会话保持,登录则不会成功;

            2)RS1和RS2需要进行数据库共享,不然RS1服务器损坏后,用woedpress时,之前的信息全部丢失,

              所以,RS1和RS2需共享存储;

                           

          1):调度器设置会话保持功能:

               # ipvsadm -E -t 172.16.1.11:80  -s  rr  -p  5

                                2):服务器配置:

           开启MySQL服务,为wordpress创建一个数据库,并授权用户登录wordpress

            # systemctl  start mariadb.service

             MariaDB [(none)]> create database wordpressdb;

               MariaDB [(none)]> grant all  on  *.* to 'wpuser'@'192.168.%.%' identified by '123456';

           共享数据库:

             NFS实现:

                首先在RS1上创建共享目录/data并修改其权限:

              ~]# mkdir /data

              ~]# chmod 757 /data

              编辑/etc/exports文件,声明共享目录:

              即在文件中添加一行:

              /data    *(rw,no_root_squash)

              开启NFS服务并查看2049端口是否被监听:

              ~]# systemctl start nfs

              然后在RS2创建目录/mydata

              ~]# mkdir /mydata

                 将RS1的共享目录/data挂载到RS2的/mydata下

              ~]# mount -t nfs 172.16.1.20:/data /mydata

              将本地的mysqld服务的数据目录设置为/mydata:

              ~]# vim /etc/my.cnf

              将datadir设置为/mydata

              [mysqld]

              datadir=/mydata
              把资源放到/mydata下并查看/mydata:

              ~]# cp -a /var/lib/mysql/* /mydata/

              ~]# ls /mydata

                 ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  test  ultrax  ytc  ytc123  ytc.txt
              开启数据库服务:

              ~]# service mysqld start

              Starting mysqld:                                   [  OK  ]

             

              完成后在web端访问:

              输入172.16.1.11/

              QQ截图20180608123532.png

                                              输入用户名密码后:

                                              QQ截图20180608123548.png

                                               这样就可以使用wordpress了;