; LVS负载均衡WordPress | Linux运维部落

LVS负载均衡WordPress

LVS:

       LVS是四层协议的开源负载均衡器,工作在netfilter框架INPUT链中,它根据不同的算

       法将用户的请求调度给后端服务器,它不受sockt套接字文件限制,性能比nginx七层   

        负载均衡器性能高很多。

    

        类型:

                lvs-nat:多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发

                lvs-dr:通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,

                          目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变;

                lvs-tun:在请求原报文的首部再次封装一个IP首部

                lvs-fullnat:修改请求报文的源和目标IP

           相关术语:

                    DS: Director Server,指的是前端负载均衡器节点。

                    RS:Real Server。后端提供服务的服务器。

                    VIP: 作为用户请求的目标的IP地址

                    DIP :DS与RS之间相互通讯

                     RIP: 后端服务器IP

                     CIP: Client IP 客户端IP


                     CIP <–> VIP == DIP <–> RIP 

LVS-NAT 类型的工作原理:

                   

  • 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP
  • PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
  • IPVS比对数据包请求的服务是否为集群服务,若是,根据调度算法修改数据包的目标IP地址为后端某一服务器IP,然后将数据包发至POSTROUTING链。 此时报文的源IP为CIP,目标IP为RIP
  • POSTROUTING链通过选路,将数据包发送给Real Server
  • Real Server比对发现目标为自己的IP,开始构建响应报文发回给Director Server。 此时报文的源IP为RIP,目标IP为CIP
  • Director Server在响应客户端前,此时会将源IP地址修改为自己的VIP地址,然后响应给客户端。 此时报文的源IP为VIP,目标IP为CIP

环境部署:

         1 准备三台主机    A    B    C    D

         2 主机A作为DS服务器,配置VIP:172.18.0.9 ,DIP为:192.168.10.254

         3 主机B C作为RS服务器 ,配置IP时网关指向DS服务器的192.168.10.254

            B主机IP:192.168.10.3/24 网关:192.168.10.254

            C主机IP:192.168.10.4/24 网关:192.168.10.254

            (备注:centos7用nmtui命令,centos6用setup命令)

          D主机IP:192.168.10.5(提供wordpress等)

D主机配置:(centos6)

    

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

                                  # service mysqld start
            [root@node1 ~]# mysql 
## 创建数据库和授权用户
            mysql> create database wpdb;
            mysql> grant all on wpdb.* to 
‘wpuser’@’192.168.10.%’ identified by ‘wppass’
            mysql> exit
            [root@node1 ~]# mkdir /data/
            [root@node1 ~]# cd /data/
            wget 
ftp://172.18.0.1/pub/Sources/sources/httpd/wordpress-4.3.1-zh_CN.zip
            [root@node1 data]# unzip wordpress-4.3.1-zh_CN.zip
            [root@node1 data]# chown -R nginx.nginx /data/wordpress
            [root@node1 data]# vim /etc/exports
            /data/wordpress 192.168.10.[34](rw)
            [root@node1 ~]# service nfs start
            [root@node1 ~]# showmount -e 172.18.99.7 
## 查看 nfs 共享目录 

B  C两台主机配置:

   [root@centos7 ~]# yum install -y nginx php-fpm php-mysql nfs-utils
            [root@centos7 ~]# mkdir -p /data/wordpress
                                    #vim /etc/nginx/conf.d/wps/conf
            server {
                        listen  80 default_server;
                        server_name www.magedu.com;
                        root  /data/wordpress/;
                        index index.php index.html index.html;
            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@centos7 nginx]# vim nginx.conf
                # default server 
##注释或删除主配置文件里的 default server 
                [root@centos7 conf.d]# nginx
                [root@centos7 conf.d]# systemctl start php-fpm
                [root@centos7 conf.d]# mount 172.18.99.7:/data/wordpress /data/wordpress
 
                

             

A主机配置:

  ipvsadm -A -t 172.18.0.9 -s rr

  ipvsadm -a -t 172.18.0.9:80 -r 192.168.10.3 -m -w 2

  ipvsadm -a -t 172.18.0.9:80 -r 192.168.10.4 -m -w 1

   sysctl -w net.ipv4.ip_forward=1(打开核心转发功能)

LVS-dr模型工原理:

(1)每一个服务器都配有VIP

(2)Ditector转发数据包给后端RealServer时,在原有数据包上再封装一层RealServer的真是IP的mac到达。

(3)RS通过VIP回数据包时,数据必须从lo:0转发给本机的ethX物理端口后,再由本机的ethX的物理端口转发给客户端

(4)RIP和VIP不一定在哦同一个网段(不在同一网段的可能性非常大,这样可以起到隐藏后端RS的作用)

LVS-dr:

环境部署:

            1准备四台主机 A B C D

            2 A主机是DS服务器:172.18.0.9

            3 B C 主机是RS服务器

               B主机IP:172.18.0.3    C主机IP:172.18.0.4

            4 D主机提供wordpress等  IP:172.18.0.5

配置D 主机:(centos6)

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

                                  # service mysqld start
            [root@node1 ~]# mysql
## 创建数据库和授权用户
            mysql> create database wpdb;
            mysql> grant all on wpdb.* to
‘wpuser’@’172.18.99.%’ identified by ‘wppass’
            mysql> exit
            [root@node1 ~]# mkdir /data/
            [root@node1 ~]# cd /data/
            wget
ftp://172.18.0.1/pub/Sources/sources/httpd/wordpress-4.3.1-zh_CN.zip
            [root@node1 data]# unzip wordpress-4.3.1-zh_CN.zip
            [root@node1 data]# chown -R nginx.nginx /data/wordpress
            [root@node1 data]# vim /etc/exports
            /data/wordpress 172.18.99.1[01](rw)
            [root@node1 ~]# service nfs start
            [root@node1 ~]# showmount -e 172.18.99.7
## 查看 nfs 共享目录 

配置B主机:(centos7)
            

            [root@centos7 ~]# yum install -y nginx php-fpm php-mysql nfs-utils
            [root@centos7 ~]# mkdir -p /data/wordpress
                                    #vim /etc/nginx/conf.d/wps/conf
            server {
                        listen  80 default_server;
                        server_name www.magedu.com;
                        root  /data/wordpress/;
                        index index.php index.html index.html;
            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@centos7 nginx]# vim nginx.conf
                # default server
##注释或删除主配置文件里的 default server
                [root@centos7 conf.d]# nginx
                [root@centos7 conf.d]# systemctl start php-fpm
                [root@centos7 conf.d]# mount 172.18.99.7:/data/wordpress /data/wordpress

                

                [root@centos7 ~]# vim dr.sh ## real-server 配置 vip
                #!/bin/bash
                 VIP=172.18.0.24
                case “$1” in
                    start)
                            echo “
配置 lvs Real Server 开始…”
                            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 “
正在关闭 lvs Real server”
                            ifconfig lo:0 down
                            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@centos7 ~]# bash dr.sh start
            配置 lvs Real Server 开始

配置C主机:(centos7)

                

                [root@centos7 ~]# yum install -y nginx php-fpm php-mysql nfs-utils
                [root@centos7 ~]# mkdir -p /data/wordpress
                [root@centos7 ~]# mount 172.18.99.7:/data/wordpress /data/wordpress

                                        #systemctl start php-fpm

                    #vim /etc/nginx/conf.d/wps/conf
            server {
                        listen  80 default_server;
                        server_name www.magedu.com;
                        root  /data/wordpress/;
                        index index.php index.html index.html;
            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@centos7 nginx]# vim nginx.conf
                # default server 
##注释或删除主配置文件里的 default server         

                 [root@centos7 ~]# bash dr.sh start
                 配置 lvs Real Server 开始

配置A主机:

                [root@node2 ~]# yum install ipvsadm -y
                                      #ifconfig eth1 192.18.0.24 netmask 255.255.255.255 broadcast 172.18.0.24 up
                                      #ifconfig
                [root@node2 ~]# ipvsadm -A -t 172.18.0.24:80 -s rr
##新增一个集群服务
                [root@node2 ~]# ipvsadm -a -t 172.18.0.24:80 -r 172.18.99.10:80 -g
                ## 为集群添加 real-server
                [root@node2 ~]# ipvsadm -a -t 172.18.0.24:80 -r 172.18.99.11:80 -g
                [root@node2 ~]# ipvsadm -Ln
## 查看集群规则
备注:route add -host 172.18.0.24 dev lo:0(可以在俩台RS主机上设置,目的是为了让响应报文从lo出去,但这不是必须的。

 

                

原创文章,作者:m22shanyongguo,如若转载,请注明出处:/75103

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

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

友情链接:万达娱乐注册  万达娱乐开户  测试  万达直属QQ  万达娱乐招商QQ  万达招商  guoqibee.com  万达开户  万达娱乐