先去服务器宿主机任意目录创建如下目录 建议弄个文件夹专门放挂载数据的目录
每个文件夹内部都创建如下目录
conf放redis.conf 文件
data挂载redis数据
logs挂载日志
配置防火墙和安全组 开启这些对应的端口号
主机master conf配置
requirepass password 需要的登录密码
masterauth password 配置哨兵的话主机也需要这个 建议所有主从机密码都一致或者不配置密码
这两个全部写一个密码或者都不配置
建议实际开发不配置密码 通过bind设置指定ip和安全组防火墙来设置登录
# bind 192.168.1.100 10.0.0.1 记得注释掉
# bind 127.0.0.1 ::1 记得注释掉
bind 0.0.0.0protected-mode no
port 6379
tcp-backlog 511
requirepass password #记得改你的密码
masterauth password #记得改你的密码timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 30
always-show-logo yes
save 900 1
save 300 10
save 60 10000stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush noappendonly yes
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mbaof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100activerehashing yes
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
从机 两个从机的配置一样就行
# bind 172.17.0.5 127.0.0.1
# bind 127.0.0.1 ::1
#bind 127.0.0.1slaveof 172.17.0.10 6379 #主机的服务器容器地址和端口号
protected-mode no
port 6379
tcp-backlog 511
requirepass password #记得改你的密码 这是从机自己的密码
masterauth password #记得改你的密码 这是主机的密码timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 30
always-show-logo yes
save 900 1
save 300 10
save 60 10000stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes #只读模式
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush noappendonly yes
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mbaof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100activerehashing yes
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
docker部署主机master
docker run -it -p 7011:6379 -p 26379:26379 \
--name docker-redis-master \
--privileged=true \
--restart=always \
-v /usr/local/app/redis/7011/conf/redis.conf:/etc/redis/redis.conf \
-v /usr/local/app/redis/7011/data:/data \
-v /etc/localtime:/etc/localtime \
-e TZ="Asia/Shanghai" \
-d redis redis-server /etc/redis/redis.conf
docker部署从机slave 部署两个 注意修改下端口号映射 挂载目录即可
从机2 slave
docker run -it -p 7013:6379 -p 26381:26379 \
--name docker-redis-slave2 \
--privileged=true \
--restart=always \
-v /usr/local/app/redis/7013/conf/redis.conf:/etc/redis/redis.conf \
-v /usr/local/app/redis/7013/data:/data \
-v /etc/localtime:/etc/localtime \
-e TZ="Asia/Shanghai" \
-d redis redis-server /etc/redis/redis.conf
查看容器
登录主机 redis-cli 然后输入auth 密码
info replication 查看集群状态 可以看到这个容器是master主机 连接上了两个slave从机
再切换到slave从机 输入info replication 可以看到连接到了主机
从机只可以读取 不可以写入
主机可以读写