综合实验:LVS+LAMP+NFS+MySQL读写分离

目录

  • 客户端配置
  • 路由器配置
  • 配置powerDNS
  • 防火墙
  • 107~109:mysql主从半同步复制
  • 106:布署ProxySQL
  • 105:NFS配置
  • 105:布署wordpress
  • 104:webB安装apache+php
  • 103:webA安装apache+php
  • LVS实现web服务的负载均衡

综合实验:LVS+LAMP+NFS+MySQL读写分离_第1张图片

实验环境:12台虚拟机


客户端配置

  1. 网络配置:仅主机模式1
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=f297f992-f824-4489-890c-e237ea9c4a9f
DEVICE=ens33
ONBOOT=no
IPADDR=192.168.88.100
PREFIX=24
GATEWAY=192.168.88.254  
DNS1=172.16.23.211

路由器配置

用Linux虚拟机模拟

  1. 一个仅主机模式1,一个桥接网卡
  2. 网络配置
[路由器]$ ip a
#下面是输出
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e1:fc:ac brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.254/24 brd 192.168.88.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::6a7a:d4a:a7ed:75b5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e1:fc:b6 brd ff:ff:ff:ff:ff:ff
    inet 172.16.23.254/16 brd 172.16.255.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::9a8a:1c56:7288:a4ca/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
  1. 开启ip转发
echo "net.ipv4.ip_forward=1" > /etc/sysctl.conf

查看

[路由器]$ sysctl -p
net.ipv4.ip_forward = 1
  1. 开启SNAT
[路由器]$ iptables -t nat -A POSTROUTING -s 192.168.88.0/24 ! -d 192.168.88.0/24 -j SNAT --to-source 172.16.23.254

配置powerDNS

IP: 172.16.23.211

  1. 安装包:基于EPEL源
yum install -y pdns pdns-backend-mysql
  1. 安装http + php + mariadb
yum -y install httpd  php php-mysql php-mbstring mariadb-server
  1. 启动服务
systemctl start httpd
systemctl start mariadb
  1. 配置PowerDNS使用mariadb作为后台数据存储
#查找到包含launch= 的行,修改并添加下面的内容
[powerDNS]$ vim /etc/pdns/pdns.conf
#大概在250行
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123
  1. 创建数据库和帐号给powerDNS用
[powerDNS]$ mysql

MariaDB [(none)]> create database powerdns;
MariaDB [(none)]> grant all privileges on powerdns.* to powerdns@localhost identified by "123";
  1. 对,又是5
USE powerdns;
CREATE TABLE domains (
  id                    INT AUTO_INCREMENT,
  name                  VARCHAR(255) NOT NULL,
  master                VARCHAR(128) DEFAULT NULL,
  last_check            INT DEFAULT NULL,
  type                  VARCHAR(6) NOT NULL,
  notified_serial       INT DEFAULT NULL,
  account               VARCHAR(40) DEFAULT NULL,
  PRIMARY KEY (id)
) Engine=InnoDB;

CREATE UNIQUE INDEX name_index ON domains(name);


CREATE TABLE records (
  id                    BIGINT AUTO_INCREMENT,
  domain_id             INT DEFAULT NULL,
  name                  VARCHAR(255) DEFAULT NULL,
  type                  VARCHAR(10) DEFAULT NULL,
  content               VARCHAR(64000) DEFAULT NULL,
  ttl                   INT DEFAULT NULL,
  prio                  INT DEFAULT NULL,
  change_date           INT DEFAULT NULL,
  disabled              TINYINT(1) DEFAULT 0,
  ordername             VARCHAR(255) BINARY DEFAULT NULL,
  auth                  TINYINT(1) DEFAULT 1,
  PRIMARY KEY (id)
) Engine=InnoDB;

CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
CREATE INDEX recordorder ON records (domain_id, ordername);


CREATE TABLE supermasters (
  ip                    VARCHAR(64) NOT NULL,
  nameserver            VARCHAR(255) NOT NULL,
  account               VARCHAR(40) NOT NULL,
  PRIMARY KEY (ip, nameserver)
) Engine=InnoDB;


CREATE TABLE comments (
  id                    INT AUTO_INCREMENT,
  domain_id             INT NOT NULL,
  name                  VARCHAR(255) NOT NULL,
  type                  VARCHAR(10) NOT NULL,
  modified_at           INT NOT NULL,
  account               VARCHAR(40) NOT NULL,
  comment               VARCHAR(64000) NOT NULL,
  PRIMARY KEY (id)
) Engine=InnoDB;

CREATE INDEX comments_domain_id_idx ON comments (domain_id);
CREATE INDEX comments_name_type_idx ON comments (name, type);
CREATE INDEX comments_order_idx ON comments (domain_id, modified_at);


CREATE TABLE domainmetadata (
  id                    INT AUTO_INCREMENT,
  domain_id             INT NOT NULL,
  kind                  VARCHAR(32),
  content               TEXT,
  PRIMARY KEY (id)
) Engine=InnoDB;

CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind);


CREATE TABLE cryptokeys (
  id                    INT AUTO_INCREMENT,
  domain_id             INT NOT NULL,
  flags                 INT NOT NULL,
  active                BOOL,
  content               TEXT,
  PRIMARY KEY(id)
) Engine=InnoDB;

CREATE INDEX domainidindex ON cryptokeys(domain_id);


CREATE TABLE tsigkeys (
  id                    INT AUTO_INCREMENT,
  name                  VARCHAR(255),
  algorithm             VARCHAR(50),
  secret                VARCHAR(255),
  PRIMARY KEY (id)
) Engine=InnoDB;

CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm);
  1. 启动服务
systemctl start pdns
systemctl enable pdns
  1. 安装httpd和php相关包
yum -y install  php-devel php-gd php-mcrypt php-imap php-ldap  php-odbc php-pear php-xml php-xmlrpc php-mcrypt php-mhash gettext
  1. 启动服务
systemctl restart httpd
  1. 下载poweradmin程序,
cd /var/www/html
wget http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz
  1. 解压缩到相应目录
tar xvf poweradmin-2.1.7.tgz
mv poweradmin-2.1.7 poweradmin

设置下权限

setfacl -Rm u:apache:rwx poweradmin
  1. 访问下面地址,启动PowerAdmin的网页安装向导:
    http://172.16.23.211/poweradmin/install/
    选英语,下一步。下一步
    综合实验:LVS+LAMP+NFS+MySQL读写分离_第2张图片

提供先前配置的数据库详情,同时为Poweradmin设置管理员密码

综合实验:LVS+LAMP+NFS+MySQL读写分离_第3张图片


  1. 为Poweradmin创建一个受限用户
标题 说明
Username PowerAdmin用户名
Password 上述用户的密码
Hostmaster 创建SOA记录指定默认主机管理员
Primary nameserver 主域名服务器
Secondary namesever 辅域名服务器

综合实验:LVS+LAMP+NFS+MySQL读写分离_第4张图片


  1. 按照下面页面说明,在数据库中创建用户并授权,然后再下一步
综合实验:LVS+LAMP+NFS+MySQL读写分离_第5张图片

示例(根据你自己的):

[powerDNS]$ mysql

MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE ON powerdns.* TO 'poweradmin'@'localhost' IDENTIFIED BY '123';

  1. 按下面页面说明,修改config.in.php文件内容,注意不要复制我的,不要复制我的,看你自己的
    综合实验:LVS+LAMP+NFS+MySQL读写分离_第6张图片
[powerDNS]$ mv poweradmin/inc/config-me.inc.php poweradmin/inc/config.inc.php

[powerDNS]$ vim /var/www/html/poweradmin/inc/config.inc.php
 18 $db_host = 'localhost';
 19 $db_port = '3306';
 20 $db_user = 'powerdns';
 21 $db_pass = '123';
 22 $db_name = 'powerdns';
 23 $db_type = 'mysql';
...
 26 $db_layer       = 'PDO';    # or MDB2
...
 30 $session_key = 'gKB$t5Qx%l!%$d+M~T$Hv+lABp$nNKKRe{7v}W3SMO0=kN';
...
 34 $iface_lang = 'en_EN';
...
 43 $dns_hostmaster = 'powerserver';
 44 $dns_ns1 = '192.168.99.101';
 45 $dns_ns2 = '192.168.99.101';

综合实验:LVS+LAMP+NFS+MySQL读写分离_第7张图片
  1. 安装完毕后,删除install目录
[powerDNS]$ rm -rf /var/www/html/poweradmin/install/
综合实验:LVS+LAMP+NFS+MySQL读写分离_第8张图片
  1. 登录http://powerdns服务器IP/poweradmin/
username:admin
password:123

这里如果登录不了提示:Error: You have to install PHP mcrypt extension
解决:

vim /etc/httpd/conf.modules.d/10-php.conf
...
  5 
  6   LoadModule php5_module modules/libphp5.so
  7   LoadModule php5_module modules/mcrypt.so
  8 

综合实验:LVS+LAMP+NFS+MySQL读写分离_第9张图片
  1. 来添加个zone
    综合实验:LVS+LAMP+NFS+MySQL读写分离_第10张图片

  1. list zone,准备给这个zone添加记录
    综合实验:LVS+LAMP+NFS+MySQL读写分离_第11张图片

  1. 添加A记录,指向172.16.23.200

综合实验:LVS+LAMP+NFS+MySQL读写分离_第12张图片


  1. 查看,直接search就可以

综合实验:LVS+LAMP+NFS+MySQL读写分离_第13张图片


  1. 看,这就有条记录了。
    综合实验:LVS+LAMP+NFS+MySQL读写分离_第14张图片

防火墙

外网IP:172.16.23.200
内网IP:192.168.99.101

  1. 配置转发
[防火墙]# vim /etc/sysctl.conf 
net.ipv4.ip_forward=1
[防火墙]# sysctl -P
  1. 配置DNAT
iptables -t nat -A PREROUTING -d 172.16.23.200 -p tcp --dport 80 -j DNAT --to-destination 192.168.99.100

107~109:mysql主从半同步复制

主机:192.168.99.107~109

107~109主机上都安装

  1. 直接yum安装吧
yum -y install mariadb-server
  1. 启动
systemctl start mariadb
systemctl enable mariadb
主服务器:192.168.99.107
  1. 修改配置文件
vim /etc/my.cnf
	[mysqld]
 #加这2条
	server_id=107
	log_bin
  1. 重启服务
systemctl restart mariadb
  1. 创建帐号用于复制
[主mysql]$ mysql

MariaDB [(none)]> grant replication slave on *.* to repluser@'%' identified by '123';
从服务器1:192.168.99.108
  1. 修改配置文件
[从1]$ vim /etc/my.cnf
	[mysqld]
	server_id=108
  read_only
  1. 重启服务
systemctl restart mariadb
  1. 连接到主服务器
[从1]$ mysql

MariaDB [(none)]> change master to 
master_host='192.168.99.107',
master_user='repluser',
master_password='123',
master_port=3306,
master_log_file='mariadb-bin.000001',
master_log_pos=0;
  1. 启动slave
MariaDB [(none)]> start slave ;
  1. 查看是否连接上
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.99.107
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 386
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 672
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...
从服务器2:192.168.99.109
  1. 修改配置文件
[从2]$ vim /etc/my.cnf
	[mysqld]
	server_id=109
  read_only
  1. 重启服务
systemctl restart mariadb
  1. 连接到主服务器
[从2]$ mysql

MariaDB [(none)]> change master to 
master_host='192.168.99.107',
master_user='repluser',
master_password='123',
master_port=3306,
master_log_file='mariadb-bin.000001',
master_log_pos=0;
  1. 启动slave
MariaDB [(none)]> start slave ;
  1. 查看是否启动成功
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.99.107
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 386
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 672
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...
主服务器:192.168.99.107
  1. 在主服务器上安装主服务器的插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
  1. 启用这个插件
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=on;
从服务器1:192.168.99.108
  1. 从服务器安装的插件不太一样,看
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
  1. 启用它
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
  1. 设置完还得重启线程
MariaDB [(none)]> stop slave; 

MariaDB [(none)]> start slave; 
从服务器2:192.168.99.109
  1. 跟107一样
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
  1. 启用它
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
  1. 设置完还得重启线程
MariaDB [(none)]> stop slave; 

MariaDB [(none)]> start slave; 
主服务器:192.168.99.107
  1. 回到主服务器上看,已经有2个线程了
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 2     |
...
  1. 给wordpress创建帐号用
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all privileges on wpdb.* to wpuser@'192.168.99.%' identified by "123";
  1. 创建帐号给ProxySQL监控用
MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.99.%' identified by '123';
  1. 创建帐号用来ProxySQL访问的(没写错,这里真的还是4)
MariaDB [(none)]> grant all on *.* to sqluser@'%' identified by '123';

106:布署ProxySQL

proxySQL:192.168.99.106
  1. 安装前还得配置下官方的yum源
    要不就自行下载安装:https://github.com/sysown/proxysql/releases
[106]$ vim /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
  1. 安装proxySQL
yum clean all 

yum install proxysql
  1. 启动proxySQL
systemctl start proxysql
  1. 登录前你还需要
yum -y install mariadb
  1. 登录到proxysql试试
mysql -uadmin -padmin -P6032 -h127.0.0.1

连接成功,简单看看

MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
  1. 添加你的3台MySQL主机
MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.107',3306);

MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.108',3306);

MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.109',3306);
  1. 添加上了,可以看看
MySQL > select * from mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 192.168.99.107 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 192.168.99.108 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 192.168.99.109 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+


  1. 当然,这2步少不了。加载并保存到磁盘
MySQL > load mysql servers to runtime;

MySQL > save mysql servers to disk;
  1. 添加监控后端节点的用户。
MySQL [(none)]> set mysql-monitor_username='monitor';
MySQL [(none)]> set mysql-monitor_password='123';
  1. 加载到RUNTIME,并保存到disk
MySQL [(none)]> load mysql variables to runtime;
MySQL [(none)]> save mysql variables to disk;
  1. 查看监控连接是否正常的 (对connect指标的监控):(如果connect_error的结果为NULL则表示正常,看最后2条)
MySQL [(none)]> select * from mysql_server_connect_log;
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| hostname       | port | time_start_us    | connect_success_time_us | connect_error                                                           |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
...
| 192.168.99.107 | 3306 | 1564319683112439 | 3277                    | NULL                                                                    |
| 192.168.99.108 | 3306 | 1564319683641071 | 3359                    | NULL                                                                    |
| 192.168.99.109 | 3306 | 1564319684169766 | 1197                    | NULL                                                                    |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+


  1. 也可以查看监控心跳信息 (对ping指标的监控)(如果ping_error的结果为NULL则表示正常)
MySQL> select * from mysql_server_ping_log;
  1. 还有其它的,查看read_only和replication_lag的监控日志
MySQL> select * from mysql_server_read_only_log;
MySQL> select * from mysql_server_replication_lag_log;

设置分组信息

  1. 需要修改的是main库中的mysql_replication_hostgroups表,该表有3个字段:writer_hostgroup写组,reader_hostgroup读组,comment备注, 指定写组的id为10,读组的id为20
MySQL> insert into mysql_replication_hostgroups values(10,20,"test");
  1. 加载到RUNTIME生效并保存
MySQL> load mysql servers to runtime;
MySQL> save mysql servers to disk;
  1. Monitor模块监控后端的read_only值,按照read_only的值将节点自动移动到读/写组
MySQL> select hostgroup_id,hostname,port,status,weight from mysql_servers;
+--------------+----------------+------+--------+--------+
| hostgroup_id | hostname       | port | status | weight |
+--------------+----------------+------+--------+--------+
| 10           | 192.168.99.107 | 3306 | ONLINE | 1      |
| 20           | 192.168.99.108 | 3306 | ONLINE | 1      |
| 20           | 192.168.99.109 | 3306 | ONLINE | 1      |
+--------------+----------------+------+--------+--------+

  1. 在ProxySQL配置,将用户sqluser添加到mysql_users表中, default_hostgroup默认组设置为写组10,当读写分离的路由规则不符合时,会访问默认组的数据库
MySQL> insert into mysql_users(username,password,default_hostgroup) values('sqluser','123',10);

MySQL> insert into mysql_users(username,password,default_hostgroup) values('wpuser','123',10);

  1. 保存生效
MySQL> load mysql users to runtime;
MySQL> save mysql users to disk;
  1. 使用sqluser用户测试是否能路由到默认的10写组实现读、写数据。是的,我的写组就是主服务器,也就是server_id=2的主机。
[proxySQL]$ mysql -usqluser -p123 -P6033 -h127.0.0.1 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
|         106 |
+-------------+

在proxysql上配置路由规则,实现读写分离

与规则有关的表:mysql_query_rules和mysql_query_rules_fast_routing,后者是前者的扩展表,1.4.7之后支持
插入路由规则:将select语句分离到20的读组,select语句中有一个特殊语句SELECT…FOR UPDATE它会申请写锁,应路由到10的写组

  1. 可以这样写
#先进入
[proxySQL]$ mysql -uadmin -padmin -P6032 -h127.0.0.1

MySQL> insert into mysql_query_rules
(rule_id,active,match_digest,destination_hostgroup,apply)VALUES
(1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
  1. 保存生效
MySQL> load mysql query rules to runtime;
MySQL> save mysql query rules to disk;

注意:因ProxySQL根据rule_id顺序进行规则匹配,select … for update规则的rule_id必须要小于普通的select规则的rule_id

  1. 看下生效了没,已经有了2条记录了
MySQL [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest         | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1       | 1      | ^SELECT.*FOR UPDATE$ | 10                    | 1     |
| 2       | 1      | ^SELECT              | 20                    | 1     |
+---------+--------+----------------------+-----------------------+-------+

到这里就可以实现读写分离了

在这里强调下:
(1)进入proxySQL管理界面是:mysql -uadmin -padmin -P6032 -h127.0.0.1,端口号是:6032,默认的帐号密码是admin和admin。
(2)如果使用mysql -usqluser -p123 -P6033 -h127.0.0.1则访问的是主服务器上的数据库了。


105:NFS配置

主机:192.168.99.105
  1. 安装
[NFS]$ yum install -y nfs-utils
  1. 配置
[NFS]$ mkdir /data/wordpress

[NFS]$ vim /etc/exports
/data/wordpress 192.168.99.0/24(rw,all_squash,anonuid=997,anongid=995)

#加载
[NFS]$ exportfs -r
  1. 启动服务
[NFS]$ systemctl restart nfs-server
  1. 创建用户
[NFS]$ groupadd -g 995 apache
[NFS]$ useradd -r -u 997 -g 995 -s /sbin/nologin apache

105:布署wordpress

192.168.99.105
这是在NFS服务器
  1. 下载
[NFS]$ cd
[NFS]$ wget ftp://192.168.99.1/Magedu37/files/lamp/wordpress-5.0.3-zh_CN.tar.gz
  1. 解压
[NFS]$ tar xf wordpress-5.0.3-zh_CN.tar.gz -C /data/
  1. 删除包
[NFS]$ rm -f wordpress-5.0.3-zh_CN.tar.gz
[NFS]$ chown -R apache.apache /data/wordpress/

104:webB安装apache+php

主机:192.168.99.104
  1. 在主机104上安装php和httpd
[http]$ yum -y install php httpd php-mysql php-mbstring
  1. 修改配置文件
[http]$ vim /etc/httpd/conf.d/test.conf


    documentroot /data/wordpress
    servername blog.jibill.com
    
        require all granted
    

  1. 重启服务
[http]$ systemctl restart httpd
  1. 安装nfs-utils准备挂载nfs
[http]$ yum -y install nfs-utils
[http]$ mkdir /data/wordpress 
[http]$ mount 192.168.99.105:/data/wordpress /data/wordpress
  1. 来测试下。注意下方多图预警
    先用hosts文件添加192.168.99.104测试,
    可以修改hosts文件
vim /etc/hosts
  192.168.99.104 blog.jibill.com

综合实验:LVS+LAMP+NFS+MySQL读写分离_第15张图片


综合实验:LVS+LAMP+NFS+MySQL读写分离_第16张图片


综合实验:LVS+LAMP+NFS+MySQL读写分离_第17张图片


综合实验:LVS+LAMP+NFS+MySQL读写分离_第18张图片


综合实验:LVS+LAMP+NFS+MySQL读写分离_第19张图片


综合实验:LVS+LAMP+NFS+MySQL读写分离_第20张图片


103:webA安装apache+php

主机:192.168.99.103
  1. 在主机103上安装php和httpd
[httpA]$ yum -y install php httpd php-mysql php-mbstring
  1. 修改配置文件
[httpA]$ vim /etc/httpd/conf.d/test.conf


    documentroot /data/wordpress
    servername blog.jibill.com
    
        require all granted
    

  1. 重启服务
[httpA]$ systemctl restart httpd
  1. 安装nfs-utils准备挂载nfs
[httpA]$ yum -y install nfs-utils
[httpA]$ mkdir /data/wordpress 
[httpA]$ mount 192.168.99.105:/data/wordpress /data/wordpress

LVS实现web服务的负载均衡

  1. 设置路由
[LVS]$ ip route del default via 192.168.99.2

[LVS]$ ip route add default via 192.168.99.101
  1. 安装ipvsadm
[LVS]$ yum -y install ipvsadm
  1. 设置回环网卡地址
[LVS]$ ip a a 192.168.99.100 dev lo
  1. 配置LVS
[LVS]$ ipvsadm -A -t 192.168.99.100:80 -s wrr

[LVS]$ ipvsadm -a -t 192.168.99.100:80 -r 192.168.99.103 -g -w 1

[LVS]$ ipvsadm -a -t 192.168.99.100:80 -r 192.168.99.104 -g -w 1

下面2个设置在webA和webB上都要配置
4. 设置webA和webB的路由和arp设置

ip route del default via 192.168.99.2
ip route add default via 192.168.99.101
  1. 配置103和104(web服务A和B),记得,2个主机都要这么配
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
ip a a 192.168.99.100 dev lo

192.168.88.100
  1. 访问blog.jibill.com/成功

综合实验:LVS+LAMP+NFS+MySQL读写分离_第21张图片

你可能感兴趣的:(#,LVS,keepalived高可用集群)