bjghzly 发表于 2019-1-2 13:39:44

实验:基于lvs

一、实验环境:

1.各软件版本:
  系统版本:CentOS Linux release 7.4.1708 (Core)
  php版本:PHP 7.2
  nginx版本:nginx-1.12.2
  数据库版本:MariaDB 10
  WordPress版本:4.9.4
  关闭防火墙与selinux

2.实验架构及IP分配:
http://i2.运维网.com/images/blog/201808/11/cca9e070f22238101af623ba47508057.jpg
  调度器一台:
  安装ipvsadm
  VIP:192.168.11.128(模拟公网IP,域名www.ready.cn) DIP:172.16.142.128
  realserver两台:
  两台都安装nginx+PHP,且保持配置一致
  RIP1:172.16.142.11 RS2:172.16.142.22
  数据库服务器一台:
  安装MariaDB
  IP:172.16.142.33
  存储服务器一台:
  提供NFS存储服务

二、所需软件安装配置,并测试PHP是否正常解析

1.RS1和RS2安装nginx+PHP:

a>安装epel源
  rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

b>yum安装nginx
  yum install -y nginx

c>yum安装PHP
  rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  yum install -y yum install php72w-common php72w-fpm php72w-opcache php72w-gd php72w-mysqlnd php72w-mbstring php72w-pecl-redis php72w-pecl-memcached php72w-devel

d>测试两台RS是否可以正常接收并响应web请求,添加nginx server,创建网站目录,将用户请求结尾为.php的URL交给php-fpm解析,并创建phpinfo做页面测试
  vim /etc/nginx/conf.d/php.conf

server {
listen      80;
server_namewww.ready.org;#因为是做试验,所以将hosts中添加本地域名解析
location / {
roothtml/php;#nginx配置中网站的默认根路径为/usr/share/nginx/,这里指定的是相对路径
indexindex.html index.htm;
}
location ~ .*\.(php|php5)?$ { #以任意开头以.php结尾的URI用此location做匹配
roothtml/php;
fastcgi_pass127.0.0.1:9000; #将请求交由fastcgi(php-fpm)处理
fastcgi_index index.php;
include fastcgi.conf;
}
}
  mkdir -p /usr/share/nginx/html/php/ #创建网站目录(不是必须步骤,也可使用默认目录)
  vim /usr/share/nginx/html/php/index.php

e>将nginx启动并设置开机启动
  systemctl start nginx
  systemctl enable nginx

f>开启php-fpm并设置为开机启动

g>浏览器访问www.ready.org与www.ready.org/index.php,显示以下界面便表示nginx与PHP已安装成功。
  然后删除index.php文件,安全因素。
  以上步骤不涉及负载均衡的使用,下面进行加入负载均衡服务器的配置测试,先删除之前配置好的nginx测试server,或将其注释掉
  mv /etc/nginx/conf.d/php.conf /etc/nginx/conf.d/php.conf.backup
  若是要将RS1、RS2并入负载均衡集群,则需将RS1、RS2设置为监听本机80端口且将结尾为.php的URI交php-fpm解析
  即修改/etc/nginx/nginx.conf将匹配PHP解析的location加入默认配置即可,修改如下:
  vim /etc/nginx/nginx.conf

worker_processes1;
events {
worker_connections1024;
}
http {
include      mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request"
'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
sendfile            on;
tcp_nopush          on;
tcp_nodelay      on;
keepalive_timeout65;
types_hash_max_size 2048;
server {
listen      80;
#server_name _; #我理解的是此负载均衡集群中没有涉及域名解析,所以server_name可以省略
location / {
root/usr/share/nginx/html/php; #我觉得root路径尽量要用绝对路径,最好是写在location中,这样便于管理,方便查阅
indexindex.php index.html index.htm; #不要忘了加index.php,不加会导致只输入域名时返回403错误
}
location ~ .*\.(php|php5)?$ {
root/usr/share/nginx/html/php;
index index.php;
fastcgi_pass127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
2.安装ipvsadm并配置

a>安装ipvsadm
  yum install -y ipvsadm

b>配置lvs服务器,测试调度是否成功
  ipvsadm -A -t 192.168.11.128:80 -s rr #添加tcp协议VIP,端口为80,调度算法为轮询
  ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.11 -m #添加RS1,类型为net
  ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.22 -m #添加RS2,类型为net
  -s 设置调度算法
  rr 轮询
  wrr 加权轮询
  sh 原地址哈希调度,保证整个系统的出入口唯一
  wlc 加权最小连接数调度

c>开启Linux的内核转发功能
  此步骤很关键,因为关系到请求报文能否经由VIP后再转发至DIP。
  sysctl -w net.ipv4.ip_forward=1
  浏览器访问192.168.11.128与192.168.11.128/index.php
  若出现测试页面即为配置成功

3.安装并配置MariaDB

a>在172.16.142.33上yum安装MariaDB(以下数据库配置有很多安全隐患,待后续深入学习数据库后会另起博文详解数据库的相关配置)
  vim /etc/yum.repos.d/mariadb.repo


name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
  yum install -y MariaDB-server

b>配置数据库
  mysql_secure_installation #初始化数据库,设置root密码,匿名用户权限等
  mysql -uroot -p123456 #登入数据库进行用户及库的创建与配置

>GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; #创建账户mysql,密码为123456,并允许任意IP访问此账户数据库,权限为ALL
>CREATE DATABASE wordpress; #创建名为wordpress的库,安装完WordPress后会要求在数据库创建库,这里事先创建好
>quit
4.安装WordPress

a>将下载好的WordPress包解压至已创建好的/usr/share/nginx/html/php目录下

b>为模拟互联网环境,将'www.ready.cn 192.168.11.128'hosts文件加入本地解析规则

c>浏览器输入www.ready.cn,按提示步骤进行操作便可安装完成

三、关于lvs-nat类型下ipvsadm的一些用法
  ipvsadm -ln --stats #状态信息
  例:
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port            ConnsInPktsOutPktsInBytes OutBytes
  -> RemoteAddress:Port
  TCP192.168.11.128:80                25      951      353    838432050036
  -> 172.16.142.11:80                  12      574      205    461571303372
  -> 172.16.142.22:80                  13      377      148    37686746664
  Conns 请求连接数
  InPkts 入站的报文数
  OutPkts 出站的报文数
  InBytes 入站的字节数
  OutBytes 出站的字节数
  ipvsadm -ln --rate #每秒传输速率信息
  ipvsadm -ln -c #当前站点访问情况
  例:
  IPVS connection entries
  pro expire state      source            virtual            destination
  TCP 14:55ESTABLISHED 192.168.11.1:53035 192.168.11.128:80172.16.142.22:80
  TCP 14:55ESTABLISHED 192.168.11.1:53016 192.168.11.128:80172.16.142.11:80
  TCP 01:46TIME_WAIT192.168.11.1:53029 192.168.11.128:80172.16.142.11:80
  TCP 01:09TIME_WAIT192.168.11.1:52990 192.168.11.128:80172.16.142.11:80
  这里需要注意的是每次输入网址访问页面时,并不一定只发送了一个请求,所以只访问一次页面时,也会出现多个RS在响应请求。另外这里的state项可以帮助理解tcp/ip协议的“握手与挥手”
  ipvsadm -S -n #保存规则,一般是保存在/etc/sysconfig/ipvsadm
  例:ipvsadm -S -n > /etc/sysconfig/ipvsadm
  也可使用systemctl stop ipvsadm.service做自动保存
  ipvsadm -C #清空规则

四、缺陷
  网站会有用户上传文件的需求,所以必须保证RS1和RS2上传文件目录保存同步或者共用一个存储
  可用inotify+rsync或者同时挂载NFS实现,后续会更新演示



页: [1]
查看完整版本: 实验:基于lvs