第十四周

1、LVS实现nat,dr

LVS实现NAT:

lvs服务器,搭建两个网卡,设定VIP和DIP,并且开启路由转发功能

sysctl -w net.ipv4.ip_forward=1

在后端服务器搭建http服务器:两台服务器的网关指向DIP

第一台:

yum install httpd

vim /var/www/html/index.html

server1

systemctl start httpd

第二台:

yum install httpd

vim /var/www/html/index.html

server2

systemctl start httpd

在LVS服务器安装ipvsadm设定规则

yum install ipvsadm

ipvsadm -A -t 192.168.0.114:80 -s wrr

ipvsadm -a -t 192.168.0.114:80 -r 192.168.174.129 -m -w 2

ipvsadm -a -t 192.168.0.114:80 -r 192.168.174.130 -m -w 3

[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  192.168.0.114:80 wrr

  -> 192.168.174.129:80          Masq    2      0          0       

  -> 192.168.174.130:80          Masq    3      0          0   

在客服端测试

for i in {1..10}; do curl http://192.168.0.114/index.html; done


LVS实现DR:

在后端服务器搭建http服务器 

第一台:

yum install httpd

vim /var/www/html/index.html

server1

systemctl start httpd

vim setparam.sh                  ##或者换个脚本

#!/bin/bash

vip='192.168.0.200'

mask='255.255.255.255'

iface='lo:0'

case $1 in

start)

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

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

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

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

        ifconfig $iface $vip netmask $mask broadcast $vip up

        route add -host $vip dev $iface

        ;;

stop)

        ifconfig $iface down

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

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

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

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

        ;;

*)

        echo "Usage: $(basename $0) start|stop"

        exit 1

        ;;

esac

bash setparam.sh start 

第二台:

yum install httpd

vim /var/www/html/index.html

server2

systemctl start httpd

vim setparam.sh                  ##或者换个脚本

#!/bin/bash

vip='192.168.0.200'

mask='255.255.255.255'

iface='lo:0'

case $1 in

start)

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

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

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

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

        ifconfig $iface $vip netmask $mask broadcast $vip up

        route add -host $vip dev $iface

        ;;

stop)

        ifconfig $iface down

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

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

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

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

        ;;

*)

        echo "Usage: $(basename $0) start|stop"

        exit 1

        ;;

esac

bash setparam.sh start 

在LVS服务器安装ipvsadm设定规则

yum install ipvsadm

ifconfig ens33:0 192.168.0.200 netmask 255.255.255.255 broadcast 192.168.0.200 up

[root@localhost ~]# ipvsadm -A -t 192.168.0.200:80 -s rr

[root@localhost ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.117 -g

[root@localhost ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.108 -g

[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  192.168.0.200:80 rr

  -> 192.168.0.108:80            Route  1      0          0       

  -> 192.168.0.117:80            Route  1      0          0       

在客服端测试

for i in {1..10}; do curl http://192.168.0.114/index.html; done



2、nginx反向代理,虚拟主机

反向代理:

在后端服务器搭建http服务器 

yum install httpd

vim /var/www/html/index.html

server1

systemctl start httpd

在前端搭建反代服务器

yum install nginx 

vim /etc/nginx/conf.d/default.conf

server {

    listen      80;

    server_name  localhost;

location / {

        root  /usr/share/nginx/html;

        index  index.html index.htm;

        proxy_pass http://192.168.0.108:80;

    }

}

systemctl start nginx

在客户端测试

curl 192.168.0.114 


实现后端两台服务器动静分离:

配置server1为后端静态web服务器

yum install -y nginx

mkdir -pv /data/nginx/html                  ##创建nginx主页目录

mv phpMyAdmin-4.0.10.20-all-languages /data/nginx/html/

cd /data/nginx/html/                          

ln -sv phpMyAdmin-4.0.10.20-all-languages pma

vim /etc/nginx/nginx.conf                   ##编辑nginx配置文件

server{

     listen    80 ;

     server_name   192.168.45.60;

     root    /data/nginx/html;

nginx -t

systemctl start nginx

配置server2为后端动态web服务器

yum install -y php-fpm php-mysql php-mbstring php-mcrypt mariadb-server httpd

vim /etc/php-fpm.d/www.conf                ##编辑php-fpm配置文件

listen = 0.0.0.0:9000

;listen.allowed_clients = 127.0.0.1

pm.max_children = 150

pm.status_path = /status

ping.path = /ping

mkdir /var/lib/php/session                         ##创建session目录

chown apache:apache /var/lib/php/session/            ##授权给apache用户

systemctl start php-fpm.service

systemctl start httpd.service

systemctl start mariadb.service

mkdir /data/apps -pv

vim /data/apps/index.php                      编辑php主页,验证php是否安装成功

       phpinfo();

?>

vim /etc/my.cnf                             ##编辑mysql配置文件

[mysqld]

skip_name_resolve=ON

innodb_file_per_table=ON

systemctl start mariadb

mysql_secure_installation                         ##mysql加固

unzip phpMyAdmin-4.0.10.20-all-languages.zip

cp config.sample.inc.php config.inc.php

vim config.inc.php

配置前端服务器

yum install nginx

vim /etc/nginx/conf.d/nginx.conf                     ##编辑nginx配置

http {

     fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m                       max_size=2g;                                           ##定义缓存   

    server {

        listen 80;

        server_name 192.168.45.57;

        index index.php index.html;

        location / {

                root /data/nginx/html;

                proxy_pass http://192.168.45.60:80;

        }

        location ~* \.php$ {

                fastcgi_pass 192.168.45.59:9000;        ##代理后端动态php主机地址

                fastcgi_index index.php;                  

                fastcgi_param SCRIPT_FILENAME    /data/apps/$fastcgi_script_name;

                include fastcgi_params;

                fastcgi_keep_conn    on;

                fastcgi_cache fcache;                           ##调用缓存及设置参数

                fastcgi_cache_key $request_uri;

                fastcgi_cache_valid 200 302  10m;

                fastcgi_cache_valid 301      1h;

                fastcgi_cache_valid any      1m;

        }

        location ~* ^/(status|ping)$ {

                                        include    fastcgi_params;

                                        fastcgi_pass  192.168.45.59:9000;

                                        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

        }

    }

}

nginx -t

nginx-s reload

验证:

http://192.168.45.57/pma/index.php


只安装动态php服务器


动静分离后的情况


实现upstream负载均衡

第一台http服务器:

yum install httpd

vim /var/www/html/index.html

server1


sysctemctl stop firewalld

systemctl start httpd

第二台http服务器:

yum install httpd

vim /var/www/html/index.html

server2

sysctemctl stop firewalld

systemctl start httpd

nginx服务器:

yum install nginx 

vim /etc/nginx/conf.d/nginx.conf

http {

    upstream websrvs {

        server 192.168.0.108;

        server 192.168.0.118;

    }

    server {

        listen      80;

        server_name  192.168.0.121;

        root /data/nginx/html;

        location / {

            index  index.html index.htm index.php;

            proxy_pass http://websrvs;

         }

     }

}

nginx -t

systemctl start nginx 

nginx -s reload

测试:

curl 192.168.0.121

你可能感兴趣的:(第十四周)