目录
1.IP规划
IP | 主机 | 备注 |
10.5.20.11 | iscsi | 额外添加四块相同规格硬盘 |
10.5.20.12 | nfs | 文件共享服务 |
10.5.20.13 | web1 | 网站服务 |
10.5.20.14 | web2 | |
10.5.20.15 | nginx+keepalived | nginx负载均衡,keepalived实现网站服务器虚IP主备跳转 |
10.5.20.16 | nginx+keepalived | |
10.5.20.17 | mysql_master | 主库,主主复制 |
10.5.20.18 | mysql_master | |
10.5.20.19 | mysql_slave | 从库 |
10.5.20.20 | mysql_slave | |
10.5.20.21 | keepalived+lvs | keepalived实现数据库服务器虚IP主备跳转,lvs分流 |
10.5.20.22 | keepalived+lvs | |
10.5.20.23 | redis+keepalived | redis缓存,keepalived实现缓存服务器虚IP主备跳转 |
10.5.20.24 | redis+keepalived | |
10.5.20.25 | / | web虚IP |
10.5.20.26 | / | 数据库虚IP |
10.5.20.27 | / | 缓存虚IP |
2.搭建过程
2.1 初始化
基础源(所以主机)
# 删除系统自带网络源,该源已不可用
rm -fr /etc/yum.repos.d/*
# 使用curl获取华为源文件
curl -L https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo -o /etc/yum.repos.d/CentOS-Base.repo
epel-relase源(web,redis)
yum -y install epel-release
php74源(web)
# 安装remi-release源
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# 安装yum扩展工具
yum -y install yum-utils
# 启用php74源
yum-config-manager --enable remi-php74
nginx源
rpm -Uvh https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
mysql57源(四台数据库服务器)
cat >> /etc/yum.repos.d/mysql.repo <<-EOF
[mysql]
name=mysql57
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
enabled=1
gpgcheck=0
EOF
关闭防护
# 关闭SELinux
setenforce 0
# 关闭SELinux开机自启动
sed "s/SELINUX=.*/SELINUX=disabled/" /etc/selinux/config
# 关闭防火墙
systemctl stop firewalld
# 关闭防火墙开机自启动
systemctl disable firewalld
2.2 iscsi
安装所需工具
yum -y install mdadm target*
磁盘管理
# 创建RAID5
mdadm -C /dev/md5 -l5 -n3 -x1 /dev/sd{b..e}
# 创建卷组
vgcreate vg5 /dev/md5
# 创建逻辑卷
lvcreate vg5 -L 9G -n lv1
lvcreate vg5 -L 9G -n lv2
lvcreate vg5 -L 9G -n lv3
lvcreate vg5 -L 9G -n lv4
配置targetcli
# 进入targetcli
targetcli
# 创建block
cd /backstores/block
create name=disk1 dev=/dev/vg5/lv1
create name=disk2 dev=/dev/vg5/lv2
create name=disk3 dev=/dev/vg5/lv3
create name=disk4 dev=/dev/vg5/lv4
# 创建iscsi对象
cd /iscsi
create wwn=iqn.2024-09.localhost.localdomain:nfs
# 创建luns
cd /iscsi/iqn.2024-09.localhost.localdomain:nfs/tpg1/luns
create /backstores/block/disk1
create /backstores/block/disk2
create /backstores/block/disk3
create /backstores/block/disk4
# 创建portals
cd ../portals/
delete 0.0.0.0 3260
create 10.5.20.11 3260
# 创建acls
cd ../acls
create wwn=iqn.2024-09.localhost.localdomain:nfs
# 退出targetcli
exit
启动服务
systemctl start targetd
2.3 nfs
安装所需工具
yum -y install iscsi-initiator-utils nfs-utils mdadm
配置initiator
# 给initiator命名
echo "InitiatorName=iqn.2024-09.localhost.localdomain:nfs" > /etc/iscsi/initiatorname.iscsi
# 启动服务
systemctl start iscsi iscsid
# 发现存储
iscsiadm --mode discoverydb --type sendtargets --portal 10.5.20.11 --discover
# 登录存储/连接target
iscsiadm --mode node --targetname iqn.2024-09.localhost.localdomain:nfs --portal 10.5.20.11:3260 --login
配置RAID5
# 创建RAID5
mdadm -C /dev/md5 -l5 -n3 -x1 /dev/sd{b..e}
# 格式化
mkfs.ext4 /dev/md5
配置nfs
# 创建nfs共享目录
mkdir /nfs
# 挂载
mount /dev/md5 /nfs/
# 设置允许访问的网段
echo "/nfs 10.5.20.*(rw,no_root_squash)" > /etc/exports
# 启动服务
systemctl restart rpcbind
systemctl restart nfs
# 重读配置文件
exportfs -r
准备Discuz压缩包,下载地址:Discuz!官方网站 - 开放、连接、共赢https://www.discuz.vip/download
使用工具(xftp、winscp等)上传WordPress压缩包
# 解压Discuz压缩包至nfs共享目录
unzip Discuz_X3.5_SC_UTF8_20230726_2.zip -d /nfs/
# 授予文件所需权限
mv /nfs/upload/* /nfs/
chmod -R 757 /nfs/{data,config,uc_server,uc_client}
2.4 web
安装httpd,php及php扩展
yum -y install php php-devel php-mysql php-xml php-json php-gd httpd nfs-utils
挂载nfs共享目录至apache主页
mount 10.5.20.12:/nfs /var/www/html/
启动httpd服务
systemctl start httpd
2.5 nginx+keepalived
2.5.1 keepalived
安装keepalived,nginx
yum -y install keepalived nginx
清空keepalived配置文件
echo "" > /etc/keepalived/keepalived.conf
编辑keepalived配置文件
vim /etc/keepalived/keepalived.conf
# 主
global_defs {
router_id 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.25
}
}
# 备
global_defs {
router_id 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.25
}
}
启动keepalived(先启动主服务器,再启动备服务器)服务
systemctl start keepalived
2.5.2 nginx
编辑nginx配置文件
vim /etc/nginx/nginx.conf
# 在指定选项添加内容
http {
...
upstream discuz {
# 分别写两个web服务器的IP及端口号
server 10.5.20.13:80;
server 10.5.20.14:80;
}
...
}
vim /etc/nginx/conf.d/default.conf
# 在指定选项添加内容
server {
...
location / {
...
proxy_pass http://discuz;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
...
}
...
}
启动nginx服务
systemctl start nginx
2.6 web
添加路由信息及内核参数
# 将web虚IP绑定在回环接口
cat >> /etc/sysconfig/network-scripts/ifcfg-lo:0 <<-EOF
DEVICE=lo:0
IPADDR=10.5.20.25
NETMASK=255.255.255.255
ONBOOT=yes
EOF
# 重启网络服务
systemctl restart network
# 查看配置是否生效
ip a
# 添加永久路由
echo "route add -host 10.5.20.25 dev lo:0" >> /etc/rc.local
# 临时添加路由
route add -host 10.5.20.25 dev lo:0
# 查看路由表
route -n
# 添加内核参数
cat >> /etc/sysctl.conf <<-EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
# 加载内核参数
sysctl -p
2.7 mysql
2.7.1 主从复制
安装mysql57
yum -y install mysql-community-server
编辑配置文件(主库),在选项[mysqld]里添加内容
vim /etc/my.cnf
# 主库1
server-id=1
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
relay-log=mysql-relay-bin
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8
log_slave_updates=1
# 主库2
server-id=2
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
relay-log=mysql-relay-bin
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8
log_slave_updates=1
编辑配置文件(从库),在选项[mysqld]里添加内容
# 从库1
server-id=3
relay-log=mysql-relay-bin
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8
# 从库2
server-id=4
relay-log=mysql-relay-bin
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
init-connect='SET NAMES UTF8'
skip-character-set-client-handshake
character-set-server=utf8
启动mysql服务
systemctl start mysqld
修改mysql数据库root用户密码
# 获取初始密码
grep "temporary password" /var/log/mysqld.log
# 使用初始密码登录mysql
mysql -u root -p
set password for 'root'@'localhost' = 'Mysql_123';
授权用户(主库)
# 主库1
grant replication slave on *.* to 'slave1'@'10.5.20.%' identified by 'Slave_123';
flush privileges;
# 查看所需复制信息
show master status;
# 主库2
grant replication slave on *.* to 'slave2'@'10.5.20.%' identified by 'Slave_123';
flush privileges;
show master status;
执行复制(主库)
# 主库1
change master to
master_host='10.5.20.18',
master_user='slave2',
master_password='Slave_123',
master_port=3306,
master_log_file='mysql-bin.000002',
master_log_pos=843;
start slave;
show slave status\G;
# 主库2
change master to
master_host='10.5.20.17',
master_user='slave1',
master_password='Slave_123',
master_port=3306,
master_log_file='mysql-bin.000002',
master_log_pos=843;
start slave;
show slave status\G;
执行复制(从库)
# 从库1
change master to
master_host='10.5.20.17',
master_user='slave1',
master_password='Slave_123',
master_port=3306,
master_log_file='mysql-bin.000002',
master_log_pos=843;
start slave;
show slave status\G;
# 从库2
change master to
master_host='10.5.20.18',
master_user='slave2',
master_password='Slave_123',
master_port=3306,
master_log_file='mysql-bin.000002',
master_log_pos=843;
start slave;
show slave status\G;
2.7.2 创建数据库
drop database if exists ultrax;
create database ultrax;
grant all privileges on ultrax.* to 'ultrax'@'10.5.20.%' identified by 'Ultrax_1234' WITH GRANT OPTION;
flush privileges;
2.8 keepalived+lvs
安装keepalived,ipvsadm
yum -y install keepalived ipvsadm
清空keepalived配置文件
echo "" > /etc/keepalived/keepalived.conf
编辑keepalived配置文件
vim /etc/keepalived/keepalived.conf
主
global_defs {
router_id 3
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 2
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.26
}
}
virtual_server 10.5.20.26 3306 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.5.20.17 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 10.5.20.18 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
备
global_defs {
router_id 4
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 2
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.26
}
}
virtual_server 10.5.20.26 3306 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.5.20.17 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 10.5.20.18 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
modprobe ip_vs
lsmod | grep ip_vs
echo "modprobe ip_vs" >> /etc/rc.local
启动keepalived服务(先启动主服务器,再启动备服务器)
systemctl start keepalived
2.9 mysql
主库
cat >> /etc/sysconfig/network-scripts/ifcfg-lo:0 <<-EOF
DEVICE=lo:0
IPADDR=10.5.20.26
NETMASK=255.255.255.255
ONBOOT=yes
EOF
systemctl restart network
ip a
echo "route add -host 10.5.20.26 dev lo:0" >> /etc/rc.local
route add -host 10.5.20.26 dev lo:0
route -n
cat >> /etc/sysctl.conf <<-EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
sysctl -p
2.10 安装Discuz论坛
使用web虚IP进行访问安装Discuz
2.11 redis+keepalived
安装redis,keepalived
yum -y install redis keepalived
启动服务
nohup redis-server &
netstat -antup | grep redis
清空keepalived配置文件
echo "" > /etc/keepalived/keepalived.conf
编辑keepalived配置文件
vim /etc/keepalived/keepalived.conf
# 主
global_defs {
router_id 5
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 3
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.27
}
}
# 备
global_defs {
router_id 6
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 3
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.5.20.27
}
}
启动keepalived服务(先启动主服务器,再启动备服务器)
systemctl start keepalived
2.12 web
获取phpredis压缩包
# 解压压缩包
tar -xf phpredis-3.1.2.tar.gz
cd phpredis-3.1.2/
phpize
# 编译安装
./configure --with-php-config=/usr/bin/php-config --enable-redis
make && make install
编辑php.ini
vim /etc/php.ini
# 730行加入以下内容
extension = redis.so
编辑php测试文件
cat >> /var/www/html/info.php <<-EOF
<?php
phpinfo();
?>
EOF
重启httpd服务
systemctl restart httpd
使用浏览器访问该文件,查看是否已支持redis扩展
修改网页文件,告知web缓存服务器IP
vim /var/www/html/config/config_global.php
# 第20行
$_config['memory']['redis']['server'] = '10.5.20.27';