Lamp架构扩展
- Client1台,访问站点www.mylinuxops.com
- PowerDNS服务器1台,对用户访问的网址做域名解析
- LVS服务器1台,实现apache负载均衡
- apache+php服务器2台,分担访问压力
- NFS-Sever服务器1台,存放站点动态和静态页面
- NFS-backup服务器1台,实时备份NFS主机上站点数据
- ProxySQL服务器1台,对背后数据库服务器进行读写分离
- MySQL-Maser,主数据库服务器1台,接受对数据库写请求,向后端从服务器同步数据
- MySQL-slave{1,2},从数据库服务器2台,接受对数据库读请求
主机 | 系统 | IP |
---|---|---|
Client | 6 | 172.22.46.10 |
powerdns | 7 | 172.22.46.70 |
route | 7 | 172.22.46.57;192.168.15.107;10.0.0.200 |
LVS | 7 | 192.168.15.108;10.0.0.100 |
webSV1 | 7 | 192.168.15.111;10.0.0.100 |
webSV2 | 7 | 192.168.15.112;10.0.0.100 |
proxysql | 7 | 192.168.15.113 |
mysqlmaster | 7 | 192.168.15.114 |
mysqlslave1 | 7 | 192.168.15.115 |
mysqlslave2 | 7 | 192.168.15.116 |
NFS | 7 | 192.168.15.118 |
NFSbak | 7 | 192.168.15.119 |
一,配置proxysql,mysql主从
在3台数据库主机上安装MySQL
- yum -y install mariadb-server
Master服务器操作
1,修改配置文件
vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
server-id=4
2.创建二进制日志目录
mkdir /data/bin
chown -R mysql.mysql /data/bin
3.启动服务
systemctl start mariadb
4.创建主从复制用的账号
mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'centos';"
5.查看二进制日志位置
mysql -e "SHOW MASTER LOGS;"
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000003 | 245 |
+------------------+-----------+
配置mysqlslave1
1.修改配置文件
vim /etc/my.cnf
[mysqld]
read-only
server-id=5
2.启动服务
systemctl start mariadb
3.数据库同步
CHANGE MASTER TO
MASTER_HOST='192.168.15.114',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=245;
4.启动复制线程
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
6.测试主从复制
MariaDB [(none)]> CREATE DATABASE DB1;
- 从服务器查看是否同步
配置mysqlslave2
1.修改配置文件
vim /etc/my.cnf
[mysqld]
read-only
server-id=6
2.启动服务
systemctl start mariadb
3.数据库同步
CHANGE MASTER TO
MASTER_HOST='192.168.15.114',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=245;
4.启动复制线程
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
6.测试主从复制
MariaDB [(none)]> CREATE DATABASE DB2;
- 从服务器查看是否同步
配置ProxySQL
1.在ProxySQL主机上配置yum源
- ProxySQL内置了一个轻量级的数据库,所以需要有MySQL客户端连上去对其进行配置
- cat <<EOF | tee /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 EOF yum repolist yum install proxysql mariadb -y
2.启动ProxySQL服务
service proxysql start
3.连接管理端口
mysql -uadmin -padmin -P6032 -h127.0.0.1
4.将MySQL主从服务器信息添加入mysql_servers表中
- 向ProxySQL中添加MySQL主从服务器,proxysql根据read-only,自动分到读组和写组
- MySQL [(none)]>insert into mysql_servers(hostgroup_id,hostname,port)values(10,'192.168.15.115',3306); MySQL [(none)]>insert into mysql_servers(hostgroup_id,hostname,port)values(10,'192.168.15.116',3306); MySQL [(none)]> select * from mysql_servers;
5.在MySQL服务器的主节点上为ProxySQL添加账号用来查看MySQL节点是主还是从
MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.15.%' identified by 'centos';
6.在Proxy上配置监控账号
MySQL [(none)]> set mysql-monitor_username='monitor';
MySQL [(none)]> set mysql-monitor_password='centos';
7.将配置加载至内存,将配置保存至磁盘
MySQL [(none)]> load mysql variables to runtime;
MySQL [(none)]> save mysql variables to disk;
8.测试,监控模块的指标保存在monitor库的log表中
- 查看监控连接是否正常的 (对connect指标的监控):(如果connect_error的结果为NULL则表示正常)
- MySQL> select * from mysql_server_connect_log; MySQL> select * from mysql_server_ping_log;
9.在master节点上创建一个账户让客户端连接调度器去访问主从服务器
MySQL> grant all on *.* to sqluser@'192.168.15.%' identified by 'centos';
10.设置读写分组
- 在ProxySQL配置,将用户sqluser添加到mysql_users表中, default_hostgroup默认组设置为写组10,当读写分离的路由规则不符合时,会访问默认组的数据库
- MySQL [(none)]> insert into mysql_replication_hostgroups VALUES(10,20,"test"); MySQL [(none)]> SELECT * FROM mysql_replication_hostgroups; +------------------+------------------+---------+ | writer_hostgroup | reader_hostgroup | comment | | 10 | 20 | test | +------------------+------------------+---------+
11.让读写表生效
MySQL> load mysql users to runtime;
12.查看mysql_server表此时已经将服务器分组
MySQL [(none)]> SELECT * FROM mysql_servers;
13.保存配置至磁盘
MySQL> save mysql users to disk;
定义读写分离规则
1.在ProxySQL服务器上,将sqluser用户添加至mysql_users表中
MySQL [(none)]> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('sqluser','centos',10);
2.查看mysql_user表信息
MySQL [(none)]> SELECT * FROM mysql_users;
3.生效存盘
MySQL [(none)]> load mysql users to runtime;
MySQL [(none)]> save mysql variables to disk;
4.在ProxySQL上定义调度规则
MySQL [(none)]> 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);
5.查看定义规则
MySQL [(none)]> select * from mysql_query_rules\G;
6.生效存盘
MySQL [(none)]> load mysql users to runtime;
MySQL [(none)]> save mysql variables to disk;
在192.168.15.117z主机测试
[root@centos7 ~]# mysql -usqluser -pcentos -h192.168.15.113 -P6033 -e "SELECT @@server_id;"
[root@centos7 ~]# mysql -usqluser -pcentos -h192.168.15.113 -P6033 -e 'begin;use db1;insert t1 values(1);select @@server_id;commit'
二.配置NFS
配置NFS
yum install nfs-utils -y
在apache1,apache2主机上查看apache用户id
id apache
uid=997(apache) gid=995(apache) groups=995(apache)
id apache
uid=997(apache) gid=995(apache) groups=995(apache)
在nfs上创建一个apache用户,将来访问的用户映射成apache用户
useradd -r -u 48 -s /sbin/nologin apache
id apache
uid=48(apache) gid=48(apache) groups=48(apache)
创建出共享的目录
mkdir /data/html
定义共享规则
vim /etc/exports
/data/html 192.168.15.111(rw,no_root_squash,anonuid=997,anongid=995) 192.168.15.112(rw,no_root_squash,anonuid=997,anongid=995)
systemctl restart nfs
配置nfs实时备份
1.在nfs和nfsbak服务器上安装rsync
yum -y install rsync
2.在nfsbak端配置为rsync服务
mkdir /data/backup
vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 0
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
reverse lookup = no
host allow = 192.168.15.118
[backup]
path = /data/backup/
comment = backup
readonly = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
3.生成验证用的账户密码,修改为安全权限
echo "rsyncuser:centos" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
4..启动服务
systemctl start rsyncd
5.nfs端配置,测试rsync一次性任务
yum install inotify-tools -y
echo "centos" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
touch /data/html/test
rsync -avz --password-file=/etc/rsync.pass /data/html/ rsyncuser@192.168.15.119::backup
让inotify配合sync实时同步
1.在nfs端创建脚本
vim rsync.sh
#!/bin/bash
SRC='/data/html/'
DEST='rsyncuser@192.168.15.119::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
2.运行脚本
bash rsync.sh
3.在nfs端创建文件测试
touch /data/html/f1
三.部署Apache+PHP
分别在2台httpd主机上源码编译安装httpd
yum install php-fpm php-mysql -y
1.配置apache+php(1)服务器
vim /app/httpd24/conf/httpd.conf
DirectoryIndex index.php index.html
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
addtype application/x-httpd-php-source .phps
proxyrequests off
proxypassmatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost/data/html
<virtualhost *:80>
servername www.mylinuxops.com
Documentroot /data/html
<directory /data/html>
require all granted
</directory>
</virtualhost>
2.修改php.ini中的时区
vim /etc/php.ini
date.timezone = Aisa/Shanghai
3.修改php-fpm配置文件配置为(UDS模式)
vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /var/run/php.sock
;listen.allowed_clients = 127.0.0.1
listen.mode = 0666
4.将nfs共享目录挂载至apache服务器
showmount -e 192.168.15.118
vim /etc/fstab
192.168.15.118:/data/html /data/html nfs defaults 0 0
mount -a
df
5.在apache下的共享目录创建站点测试页
vim /data/html/index.php
<?php
$dsn='mysql:host=192.168.15.114;dbname=test';
$username='sqluser'; $passwd='centos';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>
6.测试访问
7.RS脚本配置####
vim VS.sh
#!/bin/bash
vip='10.0.0.100'
iface='lo:1'
mask='255.255.255.255'
dev=lo:1
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 $dev $vip netmask $mask #broadcast $vip up
#route add -host $vip dev $dev
;;
stop)
ifconfig $dev 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 RS.sh
配置路由和LVS
配置路由启用ip_forward转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
ifconfig ens33:1 10.0.0.200/24
配置LVS
配置
yum install ipvsadm net-tools -y
vim VS.sh
#!/bin/bash
vip='10.0.0.100'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.15.111'
rs2='192.168.15.112'
scheduler='wrr'
type='-g'
case $1 in
start)
ifconfig $iface $vip netmask $mask #broadcast $vip up
iptables -F
ipvsadm -A -t ${vip}:${port} -s $scheduler
ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
;;
stop)
ipvsadm -C
ifconfig $iface down
;;
*)
echo "Usage $(basename $0) start|stop"
exit 1
esac
bash VS.sh
搭建PowerDNS
搭建LAM
1.安装amp
yum install httpd php php-mysql mariadb-server -y
systemctl start httpd mariadb
2.安装PowerDNS
yum install pdns pdns-backend-mysql -y
mysql < pdns.sql
vim /etc/pdns/pdns.conf
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=centos
3.启动powerdns
systemctl start pdns
systemctl enable pdns
4.安装PowerDNS的WEB管理界面
yum -y install httpd php php-devel php-gd php-mcrypt php-imap phpldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-mbstring phpmcrypt php-mhash gettext
systemctl restart httpd mariadb
systemctl enable httpd mariadb
tar xf poweradmin-2.1.7.tgz -C /var/www/html/
mv /var/www/html/poweradmin-2.1.7 /var/www/html/poweradmin
5.访问web界面
rm -rf /var/www/html/poweradmin/install
使用client客户端访问##
访问www.mylinuxops.com