实验架构:
黑线是正常情况数据的流向
红色是异常情况下数据流向
实验环境:
CentOS7-1(master) 192.168.13.128 nginx反向代理(主)、redis缓存处理器(主)、mysql数据库(主)
CentOS7-2(backup) 192.168.13.129 nginx反向代理(备)、redis缓存处理器(备)、mysql数据库(从)
CentOS7-3(tomcat1) 192.168.13.130 tomcat(主)
CentOS7-4(tomcat2) 192.168.13.131 tomcat(备)
1,安装部署nginx和keepalive服务(主备都需安装)
[[email protected] ~]# systemctl stop firewalld.service ##关闭防火墙
[[email protected] ~]# setenforce 0
[[email protected] ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##安装nginx源
[[email protected] ~]# yum install -y keepalived nginx ##下载nginx和keepalive服务
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX_HA ##备份为 NGINX_HB
} ##下面删除4行
##触发脚本
vrrp_script nginx {
script "/opt/shell/nginx.sh"
interval 2 ##间隔时间
}
vrrp_instance VI_1 {
state MASTER ##备份为BACKUP
interface ens33 ##网卡
virtual_router_id 51
priority 100 ##备份优先级小于主
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { ##调用上面的函数
nginx
}
virtual_ipaddress { ##虚拟ip
192.168.200.200
}
}
//备服务器上修改
router_id NGINX_HB ##主备不一样
state BACKUP ##主服务
priority 90 ## 优先级,备服务器设置 90
//
[[email protected] ~]# mkdir /opt/shell
[[email protected] ~]# vim /opt/shell/nginx.sh ##编写nginx触发脚本
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $k -gt 0 ];then
/bin/systemctl start nginx.service
else
/bin/systemctl stop nginx.service
fi
[[email protected] ~]# chmod +x /opt/shell/nginx.sh
[[email protected] ~]# vim /etc/nginx/nginx.conf ##在include 上面一行新增
upstream tomcat_pool {
server 192.168.13.130:8080; ##定义后端节点服务器地址池
server 192.168.13.131:8080;
ip_hash; ##会话稳固功能,否则无法通过vip地址登陆
}
server {
listen 80;
server_name 192.168.13.100; ##虚拟出的ip
location / {
proxy_pass http://tomcat_pool;
proxy_set_header X-Real-IP $remote_addr;
}
}
[[email protected] ~]# nginx -t ##检查nginx配置文件的语法有没有问题
[[email protected] ~]# systemctl start keepalived.service ##启动keepalive服务
[[email protected] ~]# ip a ##查看虚拟ip
2,安装部署MySQL(主备都需要安装)
[[email protected] ~]# yum install -y mariadb-server mariadb
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# systemctl enable mariadb.service
[[email protected] ~]# netstat -natp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 38997/mysqld
[[email protected] ~]# mysql_secure_installation ##初始化数据库
Enter current password for root (enter for none): ##此处直接回车
Set root password? [Y/n] n ##设置密码
Password updated successfully!
Remove anonymous users? [Y/n] n ##是否删除匿名用户,选择不删除
Disallow root login remotely? [Y/n] n ##是否远程连接
Remove test database and access to it? [Y/n] n ##是否删除测试数据库
Reload privilege tables now? [Y/n] y ##是否重新加载
... Success!
[[email protected] ~]# mysql -uroot -p < slsaledb-2014-4-10.sql ##导入数据库
[[email protected] ~]# mysql -uroot -p ##进入数据库
MariaDB [(none)]> show databases; ##查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slsaledb | ##导入的数据库
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘;
##授权slsaledb库下的所有给root用户密码为abc123
MariaDB [(none)]> flush privileges; ##刷新权限
3,部署tomcat服务(两台节点服务器上做)
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/ ##解压压缩包
[[email protected] ~]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/
[[email protected] ~]# cp -r /opt/jdk1.8.0_144/ /usr/local/java ##复制jdk目录到/usr/local/java
[[email protected] ~]# vim /etc/profile ##修改环境变量
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
[[email protected] ~]# source /etc/profile
[[email protected] ~]# java -version ##查看版本
[[email protected] ~]# cp -r /opt/apache-tomcat-8.5.23/ /usr/local/tomcat8 ##tomcat目录
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup ##建立软连接
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[[email protected] ~]# tomcatup ##开启tomcat
[[email protected] ~]# netstat -natp | grep 8080 ##查看tomcat端口号
tcp6 0 0 :::8080 :::* LISTEN 37752/java
[[email protected] ~]# vim /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容
<h1>server 130</h1> //节点1的网页内容
<h1>server 131</h1> //节点2的网页内容
[[email protected] ROOT]# cd /usr/local/tomcat8/conf/
[[email protected] conf]# vim server.xml ##修改tomcat配置文件
148 <Host name="localhost" appBase="webapps"
149 unpackWARs="true" autoDeploy="true">
150 <Context path="" docBase="SLSaleSystem" reloadable="true" debug=" 0"></Context>
##日志调试信息debug为0表示信息越少,docBase指定访问目录
[[email protected] conf]# tomcatup ##启动tomcat服务
4,搭建商城网站
[[email protected] ~]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
[[email protected] ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[[email protected] classes]# vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.13.100\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
##修改地址为虚拟ip
uname=root
password=abc123 ##授权的用户名和密码
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
[[email protected] classes]# tomcatdown ##关闭开启tomca服务
[[email protected] classes]# tomcatup
5,部署redis主从和群集(在主备上配置)
[[email protected] ~]# yum install -y epel-release
[[email protected] ~]# yum install redis -y
[[email protected] ~]# cat /etc/redis.conf | grep -v "^#" | grep -v "^$"
[[email protected] ~]# vim /etc/redis.conf
bind 0.0.0.0
##从服务器上266行多如下一行配置
slaveof 192.168.200.128 6379 ##主服务器的IP不是虚拟IP
[[email protected] ~]# systemctl start redis.service ##开启redis服务
[[email protected] ~]# netstat -natp | grep 6379
[[email protected] ~]# redis-cli -h 192.168.13.128 -p 6379 ##在主上设置键值
192.168.13.128:6379> set name test
OK
192.168.13.128:6379> get name
"test"
[[email protected] ~]# redis-cli -h 192.168.13.129 -p 6379 ##在从上查看,显示主从同步
192.168.13.129:6379> get name
"test"
6,在tomcat服务器上配置(配置项目商城中连接redis的参数)
[[email protected] classes]# vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
47 <constructor-arg value="192.168.13.100"/> ##虚拟地址
48 <constructor-arg value="6379"/>
[[email protected] classes]# tomcatdown ##关闭开启tomca服务
[[email protected] classes]# tomcatup
7,在主备服务器上配置(测试Redis缓存效果)
//登录商城,然后反复点击需要数据库参与的操作页面,再回来检查keyspace_hits或者keyspace_misses: 值变化
//keyspace_hits: 或者 keyspace_misses://关注这个值,命中数和未命中数
[[email protected] ~]# redis-cli -h 192.168.13.100 -p 6379
192.168.13.100:6379> info
expired_keys:0
evicted_keys:0
keyspace_hits:2 ##命中,说明redis参与工作了
keyspace_misses:0
pubsub_channels:0
8,配置redis集群主从切换(只在主服务器上操作)
[[email protected] ~]# redis-cli -h 192.168.13.128 info Replication ##获取当前服务器的角色
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.13.129,port=6379,state=online,offset=10800,lag=0
master_repl_offset:10800
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:10799
[[email protected] ~]# vim /etc/redis-sentinel.conf ##修改redis哨兵配置文件
17 protected-mode no
69 sentinel monitor mymaster 192.168.200.128 6379 1 ##1表示1台从 注意:修改
98 sentinel down-after-milliseconds mymaster 3000 ##故障切换时间单位是毫秒
[[email protected] ~]# service redis-sentinel start ##启动集群
[[email protected] ~]# netstat -anpt | grep 26379 ##查看端口号
[[email protected] ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.13.128:6379,slaves=1,sentinels=1
//验证主从切换//
[[email protected] ~]# systemctl stop redis.service ##关闭主的redis服务
[[email protected] ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.13.129:6379,slaves=1,sentinels=1
##此时切换到129备服务器上
[[email protected] ~]# systemctl start redis.service ##重启开启主的redis服务
9,在主备服务器上配置(配置mysql主从复制)
//主服务器上配置//
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[[email protected] ~]# systemctl restart mariadb.service
[[email protected] ~]# netstat -natp | grep 3306
[[email protected] ~]# mysql -uroot
MariaDB [(none)]> show master status; ##查看主数据库状态信息
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to ‘rep‘@‘192.168.13.%‘ identified by ‘123456‘;
##授权复制权限
MariaDB [(none)]> flush privileges;
//备服务器上配置//
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2 ##修改id为2
log_slave_updates=true
sync_binlog=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[[email protected] ~]# systemctl restart mariadb.service
[[email protected] ~]# mysql -uroot
MariaDB [(none)]> change master to master_host=‘192.168.13.128‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;
##复制主服务器上的二进制日志文件及位置
MariaDB [(none)]> start slave; ##开启同步
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G ##查看从同步状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
10,关闭master主服务器,验证架构
谢谢阅读!!!
原文地址:https://blog.51cto.com/14080162/2478985
时间: 2024-10-05 11:25:54