安装redis
Installation
From source code
Download, extract and compile Redis with:
$ wget https://download.redis.io/releases/redis-6.2.6.tar.gz
$ tar xzf redis-6.2.6.tar.gz
$ cd redis-6.2.6
$ make
The binaries that are now compiled are available in the src directory. Run Redis with:
$ src/redis-server & (一般要指定配置文件)
修改配置文件
关闭注释requirepass 并配置密码 requirepass 123456
下面两个修改是为了远程登录
注释 # bind 127.0.0.1 -::1
注释 # protected-mode yes
停止redis
./src/redis-cli shutdown
如果设置了密要关闭时要
./src/redis-cli -a ‘密码’ shutdown (如果密码有特殊符号,整个密码要用单引号或双引号包裹起来)
### 带配置文件的启动
./src/redis-server ./redis.conf &
You can interact with Redis using the built-in client:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
docker搭建redis sentinel集群
一:启动redis主从
- 下载镜像
docker pull redis
2.启动三个redis镜像 作为一主二从
docker run -d --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379:/data --name redis6379 redis redis-server --requirepass '1234!@#$' --masterauth '1234!@#$'
进入容器redis6379查看ip
cat /etc/hosts
我的时 172.17.0.4
然后启动从节点
docker run -d --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380:/data --name redis6380 redis redis-server --requirepass '1234!@#$' --slaveof 172.17.0.4 6379 --masterauth '1234!@#$'
docker run -d --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381:/data --name redis6381 redis redis-server --requirepass '1234!@#$' --slaveof 172.17.0.4 6379 --masterauth '1234!@#$'
另一种方式:
通过配置文件启动
bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
# 有可能变为从slave 所以要配置连接主节点时的主节点密码
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6379
从节点
bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6380
# 表明从哪里复制 replicaof是新版slaveof是旧版写法 replicaof和sentinel.conf中的monitor marster节点要么都配公网要么都配内网 否则failover会失败
replicaof 121.4.63.94 6379
bind 0.0.0.0
port 6379
# 指定数据库个数
databases 9
# redis的工作目录
dir "/data/data"
logfile "/data/logs/redis.log"
requirepass 1234!@#$
masterauth 1234!@#$
# 表明自身身份信息
replica-announce-ip 121.4.63.94
replica-announce-port 6381
# 表明从哪里复制 replicaof是新版slaveof是旧版写法 replicaof和sentinel.conf中的monitor marster节点要么都配公网要么都配内网 否则failover会失败
replicaof 121.4.63.94 6379
启动redis 镜像
docker run -d --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379/data:/data/data -v /opt/services/redis/redis6379/logs:/data/logs -v /opt/services/redis/redis6379/conf:/etc/redis --name redis6379 redis redis-server /etc/redis/redis.conf
docker run -d --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380/data:/data/data -v /opt/services/redis/redis6380/logs:/data/logs -v /opt/services/redis/redis6380/conf:/etc/redis --name redis6380 redis redis-server /etc/redis/redis.conf
docker run -d --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381/data:/data/data -v /opt/services/redis/redis6381/logs:/data/logs -v /opt/services/redis/redis6381/conf:/etc/redis --name redis6381 redis redis-server /etc/redis/redis.conf
二.启动sentinel哨兵集群
新建 /opt/services/redis/sentinel26379/sentinel.conf
内容为,此内容sentinel启动后会重写
bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26379
bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26380
bind 0.0.0.0
port 26379
requirepass "1234!@#$"
# 哨兵sentinel的工作目录
dir "/data/data"
logfile "/data/logs/sentinel.log"
# daemonize yes打开后sentinel启动自动退出
# daemonize yes
sentinel monitor redismaster 121.4.63.94 6379 2
# 当在redis实例中开启了requirepass授权密码 这样所有连接redis实例的客户端都要提供密码
# 设置哨兵sentinel连接主从的密码 注意必须为主从设置一样的验证密码
sentinel auth-pass redismaster 1234!@#$
sentinel down-after-milliseconds redismaster 5000
# 表明自身身份
sentinel announce-ip 121.4.63.94
sentinel announce-port 26381
然后以redis-sentinel命令启动redis镜像
指定sentinel.conf配置文件映射到容器内时直接使用文件映射, 这么做有可能导致哨兵没有写入配置文件的权限, 表现为WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy. 解决方案:使用文件夹映射
docker run -d --env TZ=Asia/Shanghai -p 26379:26379 -v /opt/services/redis/sentinel26379/data:/data/data -v /opt/services/redis/sentinel26379/logs:/data/logs -v /opt/services/redis/sentinel26379/conf:/etc/redis-sentinel --name sentinel26379 redis redis-sentinel /etc/redis-sentinel/sentinel.conf
docker run -d --env TZ=Asia/Shanghai -p 26380:26379 -v /opt/services/redis/sentinel26380/data:/data/data -v /opt/services/redis/sentinel26380/logs:/data/logs -v /opt/services/redis/sentinel26380/conf:/etc/redis-sentinel --name sentinel26380 redis redis-sentinel /etc/redis-sentinel/sentinel.conf
docker run -d --env TZ=Asia/Shanghai -p 26381:26379 -v /opt/services/redis/sentinel26381/data:/data/data -v /opt/services/redis/sentinel26381/logs:/data/logs -v /opt/services/redis/sentinel26381/conf:/etc/redis-sentinel --name sentinel26381 redis redis-sentinel /etc/redis-sentinel/sentinel.conf
7.0.4版本
参考:https://hub.docker.com/_/redis
配置文件完全没变,只是redis启动加了两个参数
docker pull redis:7.0.4-alpine
启动redis 镜像
--privileged=true 容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6379:6379 -v /opt/services/redis/redis6379/data:/data/data -v /opt/services/redis/redis6379/logs:/data/logs -v /opt/services/redis/redis6379/conf:/etc/redis --name redis6379 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6380:6379 -v /opt/services/redis/redis6380/data:/data/data -v /opt/services/redis/redis6380/logs:/data/logs -v /opt/services/redis/redis6380/conf:/etc/redis --name redis6380 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1
docker run -d --privileged=true --env TZ=Asia/Shanghai -p 6381:6379 -v /opt/services/redis/redis6381/data:/data/data -v /opt/services/redis/redis6381/logs:/data/logs -v /opt/services/redis/redis6381/conf:/etc/redis --name redis6381 redis:7.0.4-alpine redis-server /etc/redis/redis.conf --save 60 1
sentinel启动和6.2.6版本没变化,只是镜像版本号变化了
docker run -d --env TZ=Asia/Shanghai -p 26379:26379 -v /opt/services/redis/sentinel26379/data:/data/data -v /opt/services/redis/sentinel26379/logs:/data/logs -v /opt/services/redis/sentinel26379/conf:/etc/redis-sentinel --name sentinel26379 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf
docker run -d --env TZ=Asia/Shanghai -p 26380:26379 -v /opt/services/redis/sentinel26380/data:/data/data -v /opt/services/redis/sentinel26380/logs:/data/logs -v /opt/services/redis/sentinel26380/conf:/etc/redis-sentinel --name sentinel26380 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf
docker run -d --env TZ=Asia/Shanghai -p 26381:26379 -v /opt/services/redis/sentinel26381/data:/data/data -v /opt/services/redis/sentinel26381/logs:/data/logs -v /opt/services/redis/sentinel26381/conf:/etc/redis-sentinel --name sentinel26381 redis:7.0.4-alpine redis-sentinel /etc/redis-sentinel/sentinel.conf