Redis主从集群部署
- 安装依赖
yum install -y gcc tcl
-
上传 redis 压缩包到 centos 中
-
解压缩
-c:创建新的 tar 文件(打包) -x:解压 tar 文件 -f:指定 tar 文件的名称 -v:显示详细信息,通常用于显示正在执行的操作 -t:显示 tar 文件中的内容列表 -z:使用 gzip 压缩/解压缩(通常与 -c、-x 和 -f 一起使用) -j:使用 bzip2 压缩/解压缩(通常与 -c、-x 和 -f 一起使用) -C:指定解压缩的目录(可用于指定解压缩后文件的存放位置) -r:向现有的 tar 文件中添加文件
解压缩到根目录 opt 文件夹下
tar -zxf redis-6.2.4.tar.gz -C /opt/
显示详细信息的解压缩
tar -xvf redis-6.2.4.tar.gz
-
进入 redis 解压缩后的目录,对其进行编译
make && make install
-
修改 redis 目录下的 redis.conf 文件 (先备份一份)
327 行:databases 16 修改为 databases 1
75 行:注释 bind 127.0.0.1 -::1;在下一行添加
bind 0.0.0.0
-
保存后,在 redis 目录下重新加载 redis 数据库
redis-server redis.conf
-
启动 redis 的窗口不要关闭,
打开新的窗口,创建 redis 同级目录 7001 7002 7003
-
开启 rdb
将 redis.conf 中 382-384 行注释取消
save 3600 1 save 300 100 save 60 10000
-
关闭 aof
redis.conf 中 1253 行
appendonly no
默认关闭,不用管 -
将 redis 目录下的 redis.conf 文件拷贝到 7001,7002,7003 文件中
cp redis.conf 7001
-
修改端口
使用 sed 将 7001 目录下的 redis.conf 文件中的 6379 替换为 7001,将路径 ./ 替换为 /opt/7001
端口号:99 行
路径:455
7002,7003 同理
sed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/opt\/7001\//g' 7001/redis.conf sed -i -e 's/6379/7002/g' -e 's/dir .\//dir \/opt\/7002\//g' 7002/redis.conf sed -i -e 's/6379/7003/g' -e 's/dir .\//dir \/opt\/7003\//g' 7003/redis.conf
若不管用,使用下述命令:
sed -i '1a replica-announce-ip 192.168.199.112' 7001/redis.conf sed -i '1a replica-announce-ip 192.168.199.112' 7002/redis.conf sed -i '1a replica-announce-ip 192.168.199.112' 7003/redis.conf
-
关闭第一个打开的窗口(正在运行 redis,关闭)
新打开三个窗口,顺序运行 7001,7002,7003下的 redis.conf 文件,三个不能一起运行
redis-server redis.conf
-
开启主从
连接 7002,告诉从节点主节点的编号
redis-cli -p 7002
slaveof 192.168.18.112 7001
连接 7003 操作如上
-
查看集群状态
连接 7001 :
redis-cli -p 7001
查看集群状态:
info replication
-
主从测试,拿到数据证明成功
[root@localhost]#redis-cli -p 7001 127.0.0.1:7001> set nums 123 [root@localhost]#redis-cli -p 7002 127.0.0.1:7002> get nums [root@localhost]#redis-cli -p 7003 127.0.0.1:7003> get nums
-
关闭服务器:
关闭 7001 的服务器,其他服务器关闭方式相同
或直接关闭进程
[root@localhost]#redis-cli -p 7001
127.0.0.1:7001> shutdown
查看进程
查看进程:
ps aux | grep redis
关闭进程:
kill 进程号
批量关闭进程:
ps -ef | grep redis | awk ‘{print $2}’ | xargs kill
虽然报错,但确实把进程删除了
redis哨兵集群部署
-
主从集群部署好
-
在 /opt 目录下创建 s1, s2, s3 文件夹
mkdir s1 s2 s3
-
在 /s1 目录下创建文件 sentinel.conf
写入内容:
port 27001 sentinel announce-ip IP sentinel monitor mymaster IP 7001 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 dir "/opt/s1"
-
将 /s1 目录下的 sentinel.conf 复制到 /s2 /s3 目录下
复制:
[root@localhost opt]# cp s1/sentinel.conf s2/
[root@localhost opt]# cp s1/sentinel.conf s3/修改文件第一行和最后一行
s2 目录下的文件
port 27002
dir “/opt/s2”
s3 目录下的文件
port 27003
dir “/opt/s3”
-
开启三个窗口,分别开启 7001 7002 7003 目录下的 redis.conf
redis-server redis.conf
-
进行主从分配
[root@localhost opt]# redis-cli -p 7002 127.0.0.1:7002> slaveof IP 7001 OK 127.0.0.1:7002> exit [root@localhost opt]# redis-cli -p 7003 127.0.0.1:7003> slaveof IP 7001 OK 127.0.0.1:7003> exit
-
新开三个窗口,分别开启 s1 s2 s3 目录下的 sentinel.conf
进入 s1 s2 s3 目录下
redis-sentinel sentinel.conf
-
测试哨兵机制:主节点挂掉自动推举
将主节点挂掉:
[root@localhost opt]# redis-cli -p 7001
127.0.0.1:7001> shutdown
not connected> exit查看打开的6个窗口,发现 7002 被哨兵机制推为 主节点
使用命令查看:
[root@localhost opt]# redis-cli -p 7003 127.0.0.1:7003> info replication [root@localhost opt]# redis-cli -p 7002 127.0.0.1:7002> info replication
-
查看进程的命令:
查询的列数不一样
ps -ef | grep redis
ps -aux | grep redis
-ef:UID , PID , PPID , C , STIME , TTY , TIME , CMD
PPID:父进程ID
-aux:USER , PID , %CPU , %MEM , VSZ , RSS , TTY , STAT , START , TIME , COMMAND
-
关闭进程
kill 进程id
Redis分片集群部署
Redis 5.X 需要安装依赖
-
删除 7001 7002 7003 文件夹及其中文件
rm -rf :强制删除,一般不要使用
rm -rf 7001 7002 7003
-
创建 7001 7002 7003 8001 8002 8003 文件夹
mkdir 7001 7002 7003 8001 8002 8003
-
在 /opt 目录下创建 redis.conf 文件,并写入内容
# 端口 port 6379 #开启集群功能 cluster-enabled yes #集群的名称不需要我们创建由redis自行进行维护 cluster-config-file /opt/6379/nodes.conf #节点之间心跳的超时时间为5秒 cluster-node-timeout 5000 #持久化目录 dir /opt/6379 #绑定地址 bind 0.0.0.0 #redis后台运行 daemonize yes #注册实例ip地址 replica-announce-ip IP #关闭保护模式 protected-mode no #数据库的数量 databases 1 #日志 logfile /opt/6379/run.log
-
将文件 redis.conf 复制到多个目录下
echo 7001 7002 7003 8001 8002 8003 | xargs -t -n 1 cp redis.conf
-
将多个目录下的 redis.conf 文件中的 端口改为对应的端口号(| 之前)
printf ‘%s\n’ 7001 7002 7003 8001 8002 8003 | xargs -I{} -t sed -i ‘s/6379/{}/g’ {}/redis.conf
-
运行多个目录下的 redis.conf 文件
printf ‘%s\n’ 7001 7002 7003 8001 8002 8003 | xargs -I{} -t redis-server {}/redis.conf
-
查看进程,判断分片是否部署出错
ps -ef | grep redis
若六个进程后都有 [cluster] 则未配置错误
-
创建 Redis 集群
redis-cli
:用于与 Redis 服务器进行交互。--cluster create
:表示要创建一个 Redis 集群。--cluster-replicas 1
:指定了集群中每个主节点的副本数量。在这里,设置为 1 表示每个主节点都有一个副本[root@localhost opt]# redis-cli --cluster create --cluster-replicas 1 IP:7001 IP:7002 IP:7003 IP:8001 IP:8002 IP:8003
Can I set the above configuration? (type ‘yes’ to accept):
一定要输入 yes,而不是 y
-
查看节点
redis-cli -p 7001 cluster nodes
700x 是主节点,800x 是从节点
-
进入 7001 服务器测试
redis-cli -c -p 7001
选项
-c
的作用是启用集群模式。当使用该选项时,
redis-cli
会自动检测到 Redis 集群,并将命令发送到正确的节点。这样,在与 Redis 集群进行交互时,不需要手动指定节点的地址和端口号,而是由redis-cli
自动处理
Nginx 安装部署
- 上传 apache-tomcat-7.0.78.tar.gz,jdk-8u161-linux-x64.tar.gz,nginx-1.12.2.tar.gz,pcre-8.37.tar.gz 到虚拟机
jdk 部署
-
安装jdk,在 /opt 目录下创建 java 文件夹
-
将 jdk 解压缩到 /opt/java 目录下
tar -zxf /root/jdk-8u161-linux-x64.tar.gz -C /opt/java
- 配置 java
在 /etc/profile 中添加内容:
export JAVA_HOME=/opt/java/jdk1.8.0_161
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JRE_HOME=$JAVA_HOME/jre
- 刷新文件使命令生效
source /etc/profile
- 使用
java -version
检测 java 配置是否成功
出现版本号为成功
pcre 部署
- 解压缩 pcre-8.37 到 /opt/nginx 目录下
tar -xvf pcre-8.37.tar.gz -C /opt/nginx/
- 添加依赖
> yum update
> yum install gcc gcc-c++ autoconf automake
> yum install gcc build-essential
make
命令用于执行 Makefile 文件中定义的规则,用来编译源代码并生成可执行文件或库文件
make install
命令用于安装已经编译好的软件。它会将可执行文件、库文件、头文件以及其他必要的资源复制到系统指定的目录中,使得软件可以被系统和其他应用程序所使用
- 执行目录下的 configure 文件
./configure
是一个常见的用于配置源代码以便后续编译的脚本或命令
[root@localhost pcre-8.37]# ./configure
-
编译
> make > make insatll
-
下载(不确定需不需要)
yum -y install make zlib zlib-devel gcc-c++ libtool opssl openssl-devel
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
-
查看版本
[root@localhost pcre-8.37]# pcre-config --version
8.37
nginx 部署
-
解压缩到 /opt/nginx 目录下
tar -zxf /root/nginx-1.12.2.tar.gz -C /opt/nginx/
-
进入解压后文件目录,进行编译检查
[root@localhost nginx-1.12.2]# ./configure
[root@localhost nginx-1.12.2]# make && make install
-
进入 nginx 启动目录
[root@localhost sbin]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls /usr/local/nginx/sbin/
nginx -
启动 nginx
[root@localhost sbin]# ./nginx
-
查看进程
ps -ef | grep nginx
-
关闭防火墙,在本地浏览器中访问虚拟机的 IP
部署成功则弹出
Welcome to nginx!
的界面
(防火墙在最后) -
查看 nginx 的版本
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.12.2 -
关闭 nginx
[root@localhost sbin]# ./nginx -s stop
-
刷新 nginx
[root@localhost sbin]# ./nginx -s reload
tomcat 部署
-
解压缩到 /opt/tomcat
[root@localhost ~]# tar -zxf apache-tomcat-7.0.78.tar.gz -C /opt/tomcat/
-
进入 tomcat 的 bin 目录
[root@localhost ~]# cd /opt/tomcat/apache-tomcat-7.0.78/bin/
-
启动 tomcat
[root@localhost bin]# ./startup.sh
-
关闭防火墙,在本地浏览器上打开网页:http:// IP :8080/
出现 Tomcat 页面,即为成功
反向代理
-
jdk,pcre,nginx,tomcat 全部打开
-
修改本机电脑配置文件
修改
C:\Windows\System32\drivers\etc
下的 host在文件末尾添加
ip www.abcd.com
-
修改 nginx 的配置文件 nginx.conf (先备份一份)
备份:
[root@localhost ~]# cd /opt/nginx/nginx-1.12.2/conf
[root@localhost conf]# cp nginx.conf nginx.conf.backup修改(分号一定要加):
37 行 修改(或者注释后再加一行)
其中 server_name 对应地址为 hosts 中配置地址
server_name localhost; //37行
server_name www.abcd.com;
45行后添加反向代理地址(Tomcat地址):
root html; //45行
proxy_pass http://127.0.0.1:8080;
-
刷新 nginx
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -s reload -
对目录
/usr/local/nginx/conf/
下的 nginx.conf 文件重复第三步骤 -
重新刷新 nginx
-
在本地浏览器访问网址
www.abcd.com
得到 Tomcat 页面实现反向代理
防火墙
关闭防火墙
systemctl stop firewalld
查看防火墙状态
systemctl status firewalld
查看当前系统防火墙配置
包括已经定义的服务、端口、源地址、目标地址等信息
firewall-cmd --list-all
将 HTTP 服务添加到防火墙规则中,并且设置为永久生效
firewall-cmd --add-service=http --permanen
开放指定的端口(例如,开放 TCP 端口 8080
firewall-cmd --add-port=8086/tcp --permanen
重启防火墙
firewall-cmd --reload