lvs负载集群实验(实现wordpress)

lvs负载集群实验(实现wordpress)

项目要求:

(1) lvs 调度两台 nginx+php-fpmreal-server,采用 dr 模型,调度算法为 rr

(2) mariadb real-server 提供数据库,nfs 挂载至 2 real-server 提供文件服务

(3) 由于实验环境地址冲突将VIP替换为172.18.60.60

实施步骤:

一、配置NFS+mariadb-server

1、使用yum安装相关软件

[root@localhost
~]# yum install -y nfs-utils rpcbind mysql-server nginx

2、启动数据库,配置数据库,创建数据库和授权用户,并退出

[root@localhost
~]# service start mysqld

[root@localhost
~]# mysql

mysql>
create database wpdb;

Query
OK, 1 row affected (0.00 sec)

mysql>
grant all on wpdb.* to ‘wpuser’@’172.18.%’ identified by ‘wppass’;

Query
OK, 0 rows affected (0.00 sec)

mysql>
exit

Bye

3、创建data目录,下载wordpress安装包并解压,并且赋予wordpress文件夹属主属组

[root@localhost
~]# mkdir /data/

[root@localhost
~]# cd /data/

wget
ftp://172.18.0.1/pub/Sources/sources/httpd/wordpress-4.3.1-zh_CN.zip

[root@localhost
~]#unzip wordpress-4.3.1-zh_CN.zip

[root@localhost
data]# chown -R nginx.nginx /data/wordpress

[root@localhost
data]# ll

drwxr-xr-x
5 nginx nginx    4096 Sep 16  2015 wordpress

-rw-r–r–
1 root  root  7518362 May 
9 20:59 wordpress-4.3.1-zh_CN.zip

4、设置文件夹共享

[root@localhost
data]# vim /etc/exports

/data/wordpress
172.18.10.1[01](rw)

 

5、启动nfs服务,注意,必须优先启动rpcbind服务,才能成功启动nfs服务,不然报错。

[root@localhost
data]# service rpcbind start

Starting
rpcbind:                                         
[  OK  ]

[root@localhost
data]# service nfs start

Starting
NFS services:                                     [  OK  ]

Starting
NFS quotas:                                       [  OK  ]

Starting
NFS mountd:                                       [  OK  ]

Starting
NFS daemon:                                       [  OK  ]

Starting
RPC idmapd:                                       [  OK  ]

6、查看共享的文件服务

[root@localhost
data]# showmount -e 172.18.249.57

Export
list for 172.18.249.57:

/data/wordpress
172.18.10.1[01]

成功共享

二、配置Nginx+php-fpm server1

1、 使用yum安装相关服务

[root@localhost
~]# yum install -y nginx php-fpm php-mysql nfs-utils

2、 创建文件路径wordpress目录

[root@localhost
~]# mkdir -pv /data/wordpress

3、 启动nginx服务,并编辑相关配置文件wdps.conf

[root@localhost
~]# vim /etc/nginx/conf.d/wdps.conf

server
{

        listen          80 default_server;

        server_name     www.magedu.com;

        root            /data/wordpress/;

        index           index.php index.html index.htm;

        location / {

        }

 

        location ~ \.php$ {

                fastcgi_pass 127.0.0.1:9000;

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME
/data/wordpress/$fastcgi_script_name;

                include fastcgi_params;

        }

}

[root@localhost
conf.d]# scp wdps.conf 172.18.10.11:/etc/nginx/conf.d/wdps.conf

[root@localhost
conf.d]# vim default.conf

删除 default_server字段

4、 启动nginx

[root@localhost
conf.d]# nginx

[root@localhost
conf.d]# ss –tnl

5、 启动php-fpm服务

[root@localhost
~]# service php-fpm start

Starting
php-fpm:                                          [ 
OK  ]

6、 将后端共享的wordpress文件夹挂载

[root@localhost
data]# mount -t nfs 172.18.249.57:/data/wordpress /data/wordpress

7、 编写lvs-dr脚本

[root@localhost
~]# vim dr.sh

#!/bin/bash

VIP=172.18.50.50

case
“$1” in

start)

        echo “配置 lvsRealServer 开始…”

        ifconfig lo:0 $VIP broadcast $VIP
netmask 255.255.255.255 up  ##
配置 VIP

        route add -host $VIP dev lo:0  ## 添加主机路由

        ## 限制响应和通告级别

        echo “1” >
/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo “1” >
/proc/sys/net/ipv4/conf/all/arp_ignore

        echo “2” >
/proc/sys/net/ipv4/conf/lo/arp_announce

        echo “2” >
/proc/sys/net/ipv4/conf/all/arp_announce

        ;;

stop)

        echo “正在关闭 lvsRealserver”

        ifconfiglo:0down

        echo “0” >
/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo “0” >
/proc/sys/net/ipv4/conf/all/arp_ignore

        echo “0” >
/proc/sys/net/ipv4/conf/lo/arp_announce

        echo “0” >
/proc/sys/net/ipv4/conf/all/arp_announce

        ;;

*)

        echo “用法:$0{start|stop}”

        exit 1

esac

[root@localhost
~]# bash dr.sh start

配置 lvsRealServer 开始

[root@localhost
~]# scp dr.sh 172.18.10.11:/root/

[root@localhost
~]# ip a

1:
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd
00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet 172.18.50.50/32 brd 172.18.50.50 scope
global lo:0

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2:
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000

    link/ether 00:0c:29:07:27:ff brd
ff:ff:ff:ff:ff:ff

    inet 172.18.10.10/16 brd 172.18.255.255
scope global eth1

    inet6 fe80::20c:29ff:fe07:27ff/64 scope
link

       valid_lft forever preferred_lft forever

三、配置Nginx+php-fpm server2

8、 使用yum安装相关服务

[root@localhost
~]# yum install -y nginx php-fpm php-mysql nfs-utils

9、 创建文件路径wordpress目录

[root@localhost
~]# mkdir -pv /data/wordpress

10、              启动nginx服务,并编辑相关配置文件wdps.conf

[root@localhost
~]# vim /etc/nginx/conf.d/wdps.conf

server
{

        listen          80 default_server;

        server_name     www.magedu.com;

        root            /data/wordpress/;

        index           index.php index.html index.htm;

        location / {

        }

 

        location ~ \.php$ {

                fastcgi_pass 127.0.0.1:9000;

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME
/data/wordpress/$fastcgi_script_name;

                include fastcgi_params;

        }

}

[root@localhost
conf.d]# scp wdps.conf 172.18.10.11:/etc/nginx/conf.d/wdps.conf

[root@localhost
conf.d]# vim default.conf

删除 default_server字段

11、              启动nginx

[root@localhost
conf.d]# nginx

[root@localhost
conf.d]# ss –tnl

12、              启动php-fpm服务

[root@localhost
~]# service php-fpm start

Starting
php-fpm:                                          [  OK  ]

13、              将后端共享的wordpress文件夹挂载

[root@localhost
data]# mount -t nfs 172.18.249.57:/data/wordpress /data/wordpress

14、              编写lvs-dr脚本

[root@localhost
~]# vim dr.sh

#!/bin/bash

VIP=172.18.50.50

case
“$1” in

start)

        echo “配置 lvsRealServer 开始…”

        ifconfig lo:0 $VIP broadcast $VIP
netmask 255.255.255.255 up  ##
配置 VIP

        route add -host $VIP dev lo:0  ## 添加主机路由

        ## 限制响应和通告级别

        echo “1” >
/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo “1” >
/proc/sys/net/ipv4/conf/all/arp_ignore

        echo “2” >
/proc/sys/net/ipv4/conf/lo/arp_announce

        echo “2” >
/proc/sys/net/ipv4/conf/all/arp_announce

        ;;

stop)

        echo “正在关闭 lvsRealserver”

        ifconfiglo:0down

        echo “0” >
/proc/sys/net/ipv4/conf/lo/arp_ignore

        echo “0” >
/proc/sys/net/ipv4/conf/all/arp_ignore

        echo “0” >
/proc/sys/net/ipv4/conf/lo/arp_announce

        echo “0” >
/proc/sys/net/ipv4/conf/all/arp_announce

        ;;

*)

        echo “用法:$0{start|stop}”

        exit 1

esac

[root@localhost
~]# bash dr.sh start

配置 lvsRealServer 开始

[root@localhost
~]# scp dr.sh 172.18.10.11:/root/

[root@localhost
~]# ip a

1:
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd
00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet 172.18.50.50/32 brd 172.18.50.50 scope
global lo:0

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2:
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000

    link/ether 00:0c:29:07:27:ff brd
ff:ff:ff:ff:ff:ff

    inet 172.18.10.10/16 brd 172.18.255.255
scope global eth1

    inet6 fe80::20c:29ff:fe07:27ff/64 scope
link

       valid_lft forever preferred_lft forever

四、配置LVS-server

1、 安装ipvsadm

[root@localhost
~]# yum install ipvsadm

2、 添加网卡别名,并查看

[root@localhost
~]# ip addr add 172.18.50.50/32 dev eth1

[root@localhost
~]# ip a

1:
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd
00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2:
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000

    link/ether 00:0c:29:23:f3:8d brd
ff:ff:ff:ff:ff:ff

    inet 172.18.200.100/16 brd 172.18.255.255
scope global eth1

    inet 172.18.50.50/16 scope global secondary
eth1

    inet6 fe80::20c:29ff:fe23:f38d/64 scope
link

       valid_lft forever preferred_lft forever

3、 配置负载均衡

[root@localhost
~]# ipvsadm -A -t 172.18.50.50:80 -s rr ##
新增集群服务,使用rr轮巡模式

4、为集群添加real-server

[root@localhost
~]# ipvsadm -a -t 172.18.50.50:80 -r 172.18.10.10:80 -g

[root@localhost
~]# ipvsadm -a -t 172.18.50.50:80 -r 172.18.10.11:80 –g

5、查看集群规则

[root@localhost
~]# ipvsadm -Ln

IP
Virtual Server version 1.2.1 (size=4096)

Prot
LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.18.50.50:80 rr

  -> 172.18.10.10:80              Route   1     
0          0        

  -> 172.18.10.11:80              Route   1     
0          0 

 

 

打开浏览器,输出172.18.50.50

成功访问

lvs负载集群实验(实现wordpress)lvs负载集群实验(实现wordpress)lvs负载集群实验(实现wordpress)lvs负载集群实验(实现wordpress)

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

联系我们

400-080-6560

在线咨询

工作时间:周一至周五,9:30-18:30,节假日同时也值班

QR code