防止混淆可以根据功能修改主机名
配置客户端
vi /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=th0 DEVICE=eth0 ONBOOT=yes NETBOOT=yes BOOTPROTO=none IPADDR=172.16.12.6 TYPE=Ethernet GATEWAY=172.16.12.7 DNS1=192.168.12.8 PREFIX=16
路由器配置
1、ip配置
桥接:172.16.12.7 NAT:192.168.12.7
2、开启ip_forward转发
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf sysctl -p #生效配置
配置PowerDNS
官网 https://www.powerdns.com
1、IP配置
192.168.12.8 gw:192.168.12.7
2、安装软件包,基于epel
yum install -y pdns pdns-backend-mysql -y
3、安装mariadb,直接装在本机了,不创建在主从上面了
yum install mariadb-server -y systemctl start mariadb
4、准备PowerDNS使用的数据库
CREATE DATABASE powerdns; GRANT ALL ON powerdns.* TO 'powerdns'@'localhost' IDENTIFIED BY '123456'; USE powerdns
创建powerdns数据库中的表,参看下面文档实现,直接复制粘贴到mariadb中
https://doc.powerdns.com/md/authoritative/backend-generic-mysql/
5、配置PowerDNS使用mariadb作为后台数据存储
vim /etc/pdns/pdns.conf
launch=gmysql 在原有基础上修改,下面的是新添加的 gmysql-host=localhost gmysql-port=3306 gmysql-dbname=powerdns gmysql-user=powerdns gmysql-password=123456
6、启动服务
systemctl start pdns systemctl enable pdns
7、安装httpd和php相关包
yum -y install httpd php php-devel php-gd php-mcrypt php-imap \ php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-mbstring \ php-mcrypt mcrypt php-mhash gettext
systemctl start httpd systemctl enable httpd
8、安装PowerDNS的web界面
cd /var/www/html wget http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz tar xf poweradmin-2.1.7.tgz mv poweradmin-2.1.7 poweradmin setfacl -Rm u:apache:rwx poweradmin
在本地浏览器输入http://192.168.12.8/poweradmin/
第二步
第三步
第四步
第五步
按照提示在数据库中授权
第六步
vi /var/www/html/poweradmin/inc/config.inc.php 将自己页面中的内容粘贴过来
第七步
安装后删除
rm -rf /var/www/html/poweradmin/install/
登录http://192.168.12.8/poweradmin/index.php
9、添加主域
10、添加解析记录,结果如下
配置半同步复制
1、配置ip
192.168.12.15 192.168.12.16 192.168.12.17
2、安装数据库
yum install mariadb-server
主服务器: 192.168.12.15
(1) vi /etc/my.cnf 添加2行
[mysqld] server-id=15 log-bin
systemctl start mariadb 启动服务
(2)创建账号用于复制
MariaDB [(none)]> grant replication slave on *.* to repluser@'%' identified by '123456';
从服务器1 :192.168.12.16
(1)vi /etc/my.cnf
server-id=16 read-only #必须加,要根据此项判断服务器属于读组还是写组的
systemctl start mariadb 启动服务
(2)连接到主服务器
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.12.15', MASTER_USER='repluser', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=245;
(3)启动slave,并查看状态
MariaDB [(none)]> start slave;
从服务器2 :192.168.12.17
(1) vi /etc/my.cnf 添加2行
[mysqld] server-id=15 log-bin
(2)(3)和上面一样
测试下主从工作是否正常,在主服务器创建数据库
安装半同步插件
主服务器: 192.168.12.15
MariaDB [(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; #安装主节点的插件 MariaDB [(none)]>SET GLOBAL rpl_semi_sync_master_enabled=1; #启用 MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout = 10000; #10000ms不同步,主服务器直接回应客户端,默认10000 MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%semi%'; #查看主节点状态
从服务器 :配置一样
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; #安装从节点插件 MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1; #启用 MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%semi%'; MariaDB [(none)]> stop slave; #从节点配置过需要重启线程才生效 MariaDB [(none)]> start slave; #启动后在主服务器状态变量中可以看到一个从节点
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%'; #查看状态变量
实现读写分离
IP 192.168.12.14
1、安装ProxySQL
yum install https://github.com/sysown/proxysql/releases/download/v1.4.15/proxysql-1.4.15-1-centos7.x86_64.rpm
2、安装mariadb客户端用来登录ProxySQL
yum install mariadb
3、启动proxysql
启动后会监听两个默认端口
6032:ProxySQL的管理端口
6033:ProxySQL对外提供服务的端口
service proxysql start
4、使用mysql客户端连接到ProxySQL的管理接口6032,默认管理员用户和密码都是admin:
mysql -uadmin -padmin -P6032 -h127.0.0.1
5、查看表结构
MySQL [(none)]> show tables; MySQL [(none)]> select * from sqlite_master where name='mysql_servers'\G
6、添加主从节点到分组
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.12.15',3306); MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.12.16',3306); MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.12.17',3306); MySQL [(none)]> load mysql variables to runtime; #加载到RUNTIME, MySQL [(none)]> save mysql variables to disk; #保存到disk 10组为默认为写组,全部添加到写组,monitor账号会通过read-only自动判断
7、在master(192.168.12.15)上创建账号,让proxysql能够监控
MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.12.%' identified by '123456';
8、在ProxySQL上配置监控账号
MySQL [(none)]> set mysql-monitor_username='monitor'; MySQL [(none)]> set mysql-monitor_password='123456'; MySQL [(none)]> load mysql variables to runtime; MySQL [(none)]> save mysql variables to disk; MySQL [(none)]> select * from mysql_server_connect_log; #查看是否连接成功
9、需要修改的是main库中的mysql_replication_hostgroups表,该表有3个字段:writer_hostgroup写组,reader_hostgroup读组,comment备注, 指定写组的id为10,读组的id为20
MySQL> insert into mysql_replication_hostgroups values(10,20,"test"); MySQL> load mysql servers to runtime; MySQL> save mysql servers to disk;
Monitor模块监控后端的read_only值,按照read_only的值将节点自动移动到读/写组
MySQL> select hostgroup_id,hostname,port,status,weight from mysql_servers; MySQL [(none)]> select hostgroup_id,hostname,port,status,weight from mysql_servers;
10、在master节点上创建访问用户
MySQL> grant all on *.* to sqluser@'192.168.12.%' identified by '123456'; MariaDB [(none)]> create database wordpress; 创建wordpress数据库 MariaDB [(none)]> grant all on wordpress.* to wpuser@'%' identified by '123456';
11、在ProxySQL配置,将用户sqluser添加到mysql_users表中, default_hostgroup默认组设
置为写组10,当读写分离的路由规则不符合时,会访问默认组的数据库
MySQL> insert into mysql_users(username,password,default_hostgroup) values('sqluser','123456',10); MySQL> insert into mysql_users(username,password,default_hostgroup) values('wpuser','123456',10); MySQL> load mysql users to runtime; MySQL> save mysql users to disk;
12、没有添加读写路由规则测试下
使用sqluser用户测试是否能路由到默认的10写组 实现读、写数据,由于没有添加路由规则读写都发往10组,也就是server-id是15的
mysql -usqluser -p123456 -P6033 -h127.0.0.1 -e 'select @@server_id'
mysql -usqluser -p123456 -P6033 -h127.0.0.1 -e 'create database testdb' mysql -usqluser -p123456 testdb -P6033 -h127.0.0.1 -e 'create table t(id int)'
13、在proxysql上配置路由规则,将select语句分离到20的读组,select语句中有一个特殊语句SELECT...FOR UPDATE它会申请写锁,应路由到10的写组
MySQL [(none)]> insert into mysql_query_rules (rule_id,active,match_digest,destination_hostgroup,apply)VALUES (1,1,'^SELECT.*FORUPDATE$',10,1),(2,1,'^SELECT',20,1); MySQL> load mysql query rules to runtime; MySQL> save mysql query rules to disk;
注意:因ProxySQL根据rule_id顺序进行规则匹配,select ... for update规则的
14、测试是否实现读写分离,添加过路由查询语句发往2个从节点
mysql -usqluser -p123456 -P6033 -h127.0.0.1 -e 'select @@server_id'
配置NFS
IP: 192.168.12.13
1、安装软件包
yum install nfs-utils
2、配置
mkdir -p /data/wordpress vi /etc/exports /data/wordpress *(rw,all_squash,anonuid=48,anongid=48) exportfs -r 生效配置 systemctl start nfs-server #启动服务
3、创建账号
useradd -r -u 48 -s /sbin/nologin apache
4、上传到wordpress程序到/data目录下解压
chown -R apache.apache /data/wordpress
配置WEB服务
IP:192.168.12.11
IPADDR=192.168.12.12 DNS1=192.168.12.8
IP:192.168.12.12
IPADDR=192.168.12.12 DNS1=192.168.12.8
1、安装php+apache
yum -y install php httpd php-mysql php-mbstring
2、修改配置文件
vi /etc/httpd/conf.d/test.conf
documentroot /data/wordpress servername www.heng.com require all granted
3、挂载nfs到本机
yum install nfs-utils -y 安装nfs软件 mkdir /data/wordpress -p echo '192.168.12.13:/data/wordpress /data/wordpress nfs defaults 0 0' >> /etc/fstab mount -a
另一台web服务器的配置一样,按照1、2、3步骤
4、安装wordpress,注意数据库地址
在浏览器输入192.168.12.11开始安装
5、测试在一台web服务器安装之后,无论在哪一台web服务器上都可以访问
配置单主KEEPALIVED+LVS
ka1:192.168.12.9
gw:192.168.12.7
ka2:192.168.12.10
gw:192.168.12.7
1、安装keepalived
yum install keepalived
2、修改配置文件
vi /etc/keepalived/keepalived.conf
192.168.12.9
global_defs { notification_email { admin@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka1 #vrrp_mcast_group4 224.100.100.100 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.12.100/24 dev eth0 label eth0:1 } } virtual_server 192.168.12.100 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP sorry_server 127.0.0.1 80 real_server 192.168.12.11 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.12.12 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
192.168.12.10
vi /etc/keepalived/keepalived.conf
global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka2 #vrrp_mcast_group4 224.100.100.100 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 priority 88 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.12.100/24 dev eth0 label eth0:1 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } virtual_server 192.168.12.100 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP sorry_server 127.0.0.1 80 real_server 192.168.12.11 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.12.12 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
3、两台web服务器都要执行
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.12.100 dev lo:1
4、启动keepalived
systemctl start keepalived
修改本地hosts文件,开始测试