一、Windown 下安装
1.1 下载: https://github.com/MSOpenTech/redis/releases
2. 解压:
3. 加入环境变量(就不需要到解压目录运行命令)
//redis-server.exe运行程序
//redis.windows.conf配置文件, 可以不加,会使用默认配置
redis-server.exe redis.windows.conf
二、Linux 下安装
2.1 下载 安装及编译
//放到特定位置
//这里放到/usr/local/
cd /usr/local/
//注意权限问题,需要权限,则加上sudo 运行
//下载
wget http://download.redis.io/releases/redis-6.0.0.tar.gz
//解压
tar xzf redis-6.0.0.tar.gz
//切换到解压目录
cd redis-6.0.0
//编译
make
- 问题
linux 安装
sudo apt remove apparmor
解决:
Could not create server TCP listening socket 0.0.0.0:6379: unable to bind socket, errno: 13
2.2 启动
src/redis-server
三、Docker 安装(推荐)
3.1 创建文件夹
mkdir -p $PWD/redis/{data,conf}
3.2 拷贝配置文件
#先启动一个redis 服务
docker run -p 6379:6379 --name redis -d redis:6.0
#拷贝配置文件(可能找不到)
docker cp redis:/etc/redis/redis.conf $PWD/conf
#到官网下载
#https://redis.io/download
#https://github.com/antirez/redis/blob/6.0/redis.conf
wget https://raw.githubusercontent.com/antirez/redis/6.0/redis.conf
3.3 配置(进入配置文件修改以下属性)
port 6379
requirepass 123456 #设置密码
protected-mode no #保护模式(yes|no)
bind 127.0.0.1 #绑定访问的地址,如果想任何地址都能访问,加上注释
3.4 启动
docker container stop redis
docker container rm redis
docker run -d -p 6379:6379 \
--name redis \
-v $PWD/redis/conf/redis.conf:/conf/redis.conf \
-v $PWD/redis/data:/data \
--restart=always \
--privileged=true \
redis:6.0 redis-server /conf/redis.conf #指定配置文件启动
四、docker搭建主从
4.1 修改配置
获取配置
wget http://download.redis.io/redis-stable/redis.conf
master 配置
port 6123 #这里修改了默认端口6379
protected-mode no #关闭保护模式
bind 0.0.0.0 #测试,开放给所有ip 都能访问, 生产环境注意绑定特定ip
requirepass test #密码
slave1
masterauth test #主节点验证密码
port 6124 #这里修改了默认端口6379
protected-mode no #关闭保护模式
bind 0.0.0.0 #测试,开放给所有ip 都能访问, 生产环境注意绑定特定ip
requirepass test #密码
#指定主节点
replicaof redis-master 6123
slave2
masterauth test #主节点验证密码
port 6125 #这里修改了默认端口6379
protected-mode no #关闭保护模式
bind 0.0.0.0 #测试,开放给所有ip 都能访问, 生产环境注意绑定特定ip
requirepass test #密码
#指定主节点
replicaof redis-master 6123
4.2 docker-compose.yml
```shell version: ‘3’ services: redis-master: image: ‘redis:6.0’ container_name: ‘redis-server-master’ restart: always privileged: true expose:
- 6123
environment:
TZ: "Asia/Shanghai" #时区,保证时间正确
volumes:
- '$PWD/redis/master/conf/redis.conf:/usr/local/etc/redis/redis.conf'
- '$PWD/redis/master/data:/data'
networks:
- ms-redis-netword
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”] redis-slave-1: image: ‘redis:6.0’ container_name: ‘redis-server-slave-1’ restart: always privileged: true expose:
- 6124
environment:
TZ: "Asia/Shanghai" #时区,保证时间正确
volumes:
- '$PWD/redis/slave1/conf/redis.conf:/usr/local/etc/redis/redis.conf'
- '$PWD/redis/slave1/data:/data'
networks:
- ms-redis-netword
depends_on:
- redis-master
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”] redis-slave-2: image: ‘redis:6.0’ container_name: ‘redis-server-slave-2’ restart: always privileged: true expose:
- 6125
environment:
TZ: "Asia/Shanghai" #时区,保证时间正确
volumes:
- '$PWD/redis/slave2/conf/redis.conf:/usr/local/etc/redis/redis.conf'
- '$PWD/redis/slave2/data:/data'
networks:
- ms-redis-netword
depends_on:
- redis-master
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
networks: ms-redis-netword: driver: bridge
<a name="eR4ex"></a>
#### 4.3 启动验证
```shell
#连接master
docker exec -it redis-server-master bash
#连接redis
redis-cli -p 6123
#验证
127.0.0.1:6123> auth test
OK
#查看分片情况
127.0.0.1:6123> info replication
# Replication
role:master
connected_slaves:2 #有两个从节点
slave0:ip=192.168.48.3,port=6125,state=online,offset=826,lag=0
slave1:ip=192.168.48.4,port=6124,state=online,offset=826,lag=0
master_replid:b540d4dbee52d6284bda72186b55bec1b8064ab0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:826
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:826
# 设置值
127.0.0.1:6123> set test1 java
## 登录从节点,看是否有同步
docker exec -it redis-server-slave-1 bash
#连接redis
redis-cli -p 6124
#验证
127.0.0.1:6124> auth test
OK
127.0.0.1:6124> get test1
"java"
4.4、配置 Sentinel 哨兵
Sentinel(哨岗、哨兵)是Redis的高可用性(high availability)解决方案:由一个或多个Sentinel实例(instance)组成的Sentinel系统(system)可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器,然后由新的主服务器代替已下线的主服务器继续处理命令请求。
4.4.1、Sentinel系统监视服务器过程
- 首先,Sentinel系统会挑选 server1 属下的其中一个从服务器,并将这个被选中的从服务器升级为新的主服务器。
- 之后,Sentinel系统会向 server1 属下的所有从服务器发送新的复制指令,让它们成为新的主服务器的从服务器,当所有从服务器都开始复制新的主服务器时,故障转移操作执行完毕。
- 另外,Sentinel还会继续监视已下线的server1,并在它重新上线时,将它设置为新的主服务器的从服务器。
- 之后,如果server1重新上线的话,它将被Sentinel系统降级为server2的从服务器
4.4.2、搭建启动 Sentinel 哨兵 docker-compose.yml
注意: 外部访问,目前使用 host 模式
network_mode: host
version: "3"
networks:
redis-replication:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
services:
master:
image: redis
container_name: redis-master
ports:
- "6380:6379"
volumes:
- "./master/redis.conf:/etc/redis.conf"
- "./master/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.101
slave1:
image: redis
container_name: redis-slave-1
ports:
- "6381:6379"
volumes:
- "./slave1/redis.conf:/etc/redis.conf"
- "./slave1/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.102
slave2:
image: redis
container_name: redis-slave-2
ports:
- "6382:6379"
volumes:
- "./slave2/redis.conf:/etc/redis.conf"
- "./slave2/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.103
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- "26380:26379"
volumes:
- "./sentinel1/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.201
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- "26381:26379"
volumes:
- "./sentinel2/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.202
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- "26382:26379"
volumes:
- "./sentinel3/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.203
4.4.3、Redis 配置文件
- master
```java port 6379 pidfile /var/run/redis_6379.pid protected-mode no timeout 0 tcp-keepalive 300 loglevel notice
########################### REPLICATION
slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no
############################### RDB
dbfilename dump.rdb save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dir ./
############################### AOF
appendonly yes appendfilename “appendonly.aof” appendfsync everysec no-appendfsync-on-rewrite no aof-load-truncated yes aof-use-rdb-preamble no
- Slave1:
```shell
################################# REPLICATION #################################
slaveof 172.25.0.101 6379
Slave2:
################################# REPLICATION #################################
slaveof 172.25.0.101 6379
sentinel.conf ```shell
所有哨兵端口都一致,因为使用 Docker 桥接网络映射
port 26379
哨兵设置,所有哨兵皆一致,都指向 Master
sentinel monitor mymaster 172.25.0.101 6379 2 sentinel parallel-syncs mymaster 1 sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 180000
bind 0.0.0.0 protected-mode no daemonize no pidfile /var/run/redis-sentinel.pid logfile “” dir /tmp
<a name="OKcO6"></a>
##### 4.4.4、启动测试
- 启动
```shell
docker-compose up -d
暂停master ,查看哨兵日志
#模拟master 宕机
docker stop redis-master
# 查看哨兵日志
docker logs --tail 100 redis-sentinel-1
- 哨兵 sdown 主观判断 Master 失活,接着所有哨兵 odown 客观判断 Master 失活。此时 +new-epoch 1 进入第一轮选举。
- 接着 +selected-slave slave 172.25.0.102:6379 选举出 102,即 Slave2 作为新的 Master,再 +promoted-slave 推举 Slave2 为新的 Master。
- 最终依旧会去看 101 是否回来了,+sdown slave 172.25.0.101:6379 172.25.0.101 6379 @ mymaster 172.25.0.102 6379 发现 101 还没上线。
重新启动 master ```shell docker restart redis-master
进入master
docker exec -it redis-master bash
查看是否可以写
redis-cli
127.0.0.1:6379> set test “111” (error) READONLY You can’t write against a read only replica.
- 把 redis-master 的 down 状态取消掉
- 然后把 redis-master 作为 Slave2 的从机
<a name="ZhR70"></a>
### 五、搭建简单的集群
<a name="Cy55Q"></a>
#### 5.1 redis配置文件模板redis.cluster.template
```shell
#关闭保护模式
protected-mode no
#端口
port ${PORT}
#后台运行
daemonize no
#配置,数据文件路径
dir ./
#是否开启持久化
appendonly yes
#是否开启集群
cluster-enabled yes
#集群配置文件
cluster-config-file nodes.conf
#节点连接超时时间
cluster-node-timeout 15000
5.2 初始化配置文件脚本 init_redis_cluster.sh
#!/bin/bash
#删除创建的文件
rm -rf $PWD/master $PWD/slave
#master 和 slave 文件夹
for port in `seq 7000 7005`; do
ms="master"
if [ $port -ge 7003 ]; then
ms="slave"
fi
mkdir -p $PWD/$ms/$port/conf \
&& mkdir -vp $PWD/$ms/$port/data \
&& PORT=$port envsubst < ./redis.cluster.template > $PWD/$ms/$port/conf/redis.conf;
done
5.3 创建redis容器脚本(create_redis_cluster.sh)
#!/bin/bash
#运行docker redis 的 master 和 slave 实例
for port in `seq 7000 7005`; do
ms="master"
if [ $port -ge 7003 ]; then
ms="slave"
fi
if [ "$(docker ps -q -f name=redis-cluster-$ms-$port)" ]; then
#stop
docker container stop redis-cluster-$ms-$port
fi
if [ "$(docker ps -aq -f status=exited -f name=redis-cluster-$ms-$port)" ]; then
#cleanup
docker container rm redis-cluster-$ms-$port
fi
# run your container
docker run -d -p $port:$port -p 1$port:1$port \
-v $PWD/$ms/$port/conf:/conf \
-v $PWD/$ms/$port/data:/data \
--privileged=true --restart always \
--name redis-cluster-$ms-$port \
--net network-redis-cluster \
redis redis-server /conf/redis.conf;
done
5.4 执行脚本,启动容器
#注意,模板和脚本要在同一文件目录下
sudo sh ./init_redis_cluster.sh
sudo sh ./create_redis_cluster.sh
#查看容器是否正常启动
docker ps -a --filter "network=network-redis-cluster"
5.5 分配启动集群
#!/bin/bash
#拼接masters : slaves 节点参数
matches=""
for port in `seq 7000 7005`; do
ms="master"
if [ $port -ge 7003 ]; then
ms="slave"
fi
matches=$matches$(docker inspect --format '{{(index .NetworkSettings.Networks "network-redis-cluster").IPAddress}}' "redis-cluster-$ms-${port}"):${port}" ";
done
# 创建docker-cluster
# redis 4 or 3版本
#需要redis-trib 镜像
#docker run -it --rm --net redis-cluster-net redis-trib ruby redis-trib.rb create --replicas 1 $matches
#redis 5 版本
docker run -it --rm --net network-redis-cluster --privileged=true redis redis-cli --cluster create $matches --cluster-replicas 1
#如果权限不够,则添加一下参数
#--privileged=true
5.6 测试
#登录7001redis
docker exec -it redis-cluster-master-7001 bash
#连接
root@a70b1d940dae:/usr/local# redis-cli -c -p 7001
127.0.0.1:7001> set foo bar
-> Redirected to slot [12182] located at 172.20.0.5:7002
OK
172.20.0.5:7002> set hello world
-> Redirected to slot [866] located at 172.20.0.4:7000
OK
172.20.0.4:7000> get foo
-> Redirected to slot [12182] located at 172.20.0.5:7002
"bar"
172.20.0.5:7002> get hello
-> Redirected to slot [866] located at 172.20.0.4:7000
"world"
172.20.0.4:7000>