article/redis/redis-3.0.3,哨兵集群.md
2023-06-04 15:59:11 +08:00

214 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## redis-v3.0.3哨兵集群搭建文档
### 准备工作
### 主机
准备三台全新 centos linux 服务器
固定IP
```
192.168.1.11
192.168.1.12
192.168.1.13
```
**以下命令如无特殊说明 在三台主机中均要执行**
准备环境
```bash
yum install make gcc wget
```
创建用户
```bash
useradd redis
```
创建相关目录
```bash
mkdir -p /opt/{app/redis,applog}
```
授权
```bash
chown -R redis:redis /opt/app/redis/
```
```bash
chown -R redis:redis /opt/applog/redis/
```
#### 编译源码
切换用户
```bash
su - redis
```
下载源码
```bash
cd /opt/app/redis/
wget https://download.redis.io/releases/redis-3.0.3.tar.gz
```
解压源码
```bash
tar -zxvf redis-3.0.3.tar.gz
```
编译源码 这里为了和生产一致 使用libc 内存分配器
```bash
cd redis-3.0.3
make MALLOC=libc
```
安装
```bash
make PREFIX=/opt/app/redis/ install
```
配置 path
```bash
echo 'export PATH=$PATH:/opt/app/redis/bin' >> ~/.bashrc
source ~/.bashrc
```
### 配置redis
复制配置文件
```bash
cp /opt/app/redis/redis-3.0.3/src/redis.conf /opt/app/redis/
cp /opt/app/redis/redis-3.0.3/src/sentinel.conf /opt/app/redis/
```
编辑配置文件 redis.conf
```bash
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's|^logfile ""$|logfile "/opt/applog/redis/redis.log"|g' redis.conf
sed -i 's|^dir "./"$|dir "/opt/app/redis/"|g' redis.conf
sed -i 's/appendonly no/appendonly yes/g' redis.conf
sed -i 's/^# cluster-node-timeout 15000$/cluster-node-timeout 5000/g' redis.conf
#设置主从复制密码
sed -i 's/^# masterauth <master-password>$/masterauth dUw~7a)6/g' redis.conf
#设置 节点密码
sed -i 's/^# requirepass foobared$/requirepass dUw~7a)6/g' redis.conf
```
配置主从节点
在 192.168.1.12192.168.1.13 两台机子中执行
```bash
echo "slaveof 10.23.101.3 6379" >> redis.conf
```
编辑配置文件 sentinel.conf
```bash
sed -i 's|^\(sentinel monitor mymaster\) 127.0.0.1|\1 181.168.1.11|' sentinel.conf
#设置哨兵密码
sed -i 's/^#sentinel auth-pass <master-name> <password>/sentinel auth-pass mymaster HmpYZ2KB/g' sentinel.conf
echo "daemonize yes" >> sentinel.conf
```
编写动脚本
```bash
echo "bin/redis-server redis.config">/opt/app/redis/start.sh
chmod 775 start.sh
echo "bin/redis-sentinel sentinel.config">/opt/app/redis/start-sentinel.sh
chmod 775 start.sh
```
指定用户启动
```bash
#!/bin/bash
su -s /bin/bash -c "$(pwd)/bin/redis-sentinel $(pwd)/sentinel.conf " redis
```
### 启动
先启动三个redis节点
```bash
bash start.sh
```
再启动 sentinel j节点
```bash
bash start-sentinel.sh
```
### 验证
登录三个redis节点 分别写入key
192.168.1.11 成功写入 并同步至 192.168.1.12192.168.1.13
192.168.1.12 写入失败
192.168.1.13 写入失败
登录哨兵节点 查看哨兵信息
```bash
redis-cli -p 26379
sentinel sentinels mymaster
```
杀死 主节点
分别查看 另外两台redis 的info 信息 是否发生切换