环境准备
为了让环境更加真实,本文使用多机环境
IP | 操作系统 | 内存大小 | 数据淘汰策略 | 内存淘汰策略 |
---|---|---|---|---|
192.168.168.143 | CentOS Linux release 7.9.2009 (Core) | 最大内存*3/4 | ||
192.168.168.144 | CentOS Linux release 7.9.2009 (Core) | |||
192.168.168.145 | CentOS Linux release 7.9.2009 (Core) | |||
192.168.168.146 | CentOS Linux release 7.9.2009 (Core) | |||
192.168.168.147 | CentOS Linux release 7.9.2009 (Core) | |||
192.168.168.148 | CentOS Linux release 7.9.2009 (Core) |
创建Redis容器
整体搭建步骤主要分为以下几步:
- 下载 Redis 镜像(可以省略,创建容器时如果本地镜像不存在就会去远程拉取)
- 编写 Redis 配置文件
- 创建 Redis 容器
-
Step1:编写Redis配置文件
获取
redis.conf
修改默认配置 bind 127.0.0.1
# 修改为0.0.0.0
或指定IP,127.0.0.1
限制Redis 只能本地访问protected-mode no
# 默认yes
,开启保护模式限制为本地访问daemonize no
# 默认no
,改为yes
意为以守护进程方式启动可后台运行,改为yes
会使配置文件方式启动redis失败dir ./
# 输入本地Redis
数据库存放文件夹(可选)appendonly yes
#Redis
持久化(可选)-
Step2:创建并启动Redis容器
docker run -d --net host --name redis -v /usr/confs/redis.conf:/etc/redis/redis.conf -v /usr/redisdata:/data redis redis-server /etc/redis/redis.conf --appendonly yes
命令解释:
--name redis
指定容器名称-v
挂载目录,前表示主机部分/后表示容器部分-d
表示后台启动Redis
redis-server /etc/redis/redis.conf
以配置文件启动Redis
,加载容器内的conf
文件,最终找到的是/usr/confs/redis.conf
appendonly yes
开启Redis
持久化--requirepass
“如果有密码”
创建Redis集群
[root@centos-01 ~]# docker exec -it redis redis-cli --cluster create 192.168.168.143:6379 192.168.168.144:6379 192.168.168.145:6379 192.168.168.146:6379 192.168.168.147:6379 192.168.168.148:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.168.147:6379 to 192.168.168.143:6379
Adding replica 192.168.168.148:6379 to 192.168.168.144:6379
Adding replica 192.168.168.146:6379 to 192.168.168.145:6379
M: 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379
slots:[0-5460] (5461 slots) master
M: fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379
slots:[5461-10922] (5462 slots) master
M: 00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379
slots:[10923-16383] (5461 slots) master
S: 8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379
replicates 00067aa1ac68cb803cf903223f663a8b5832fd51
S: 1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379
replicates 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f
S: 5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379
replicates fe8889c921fb602fa080a0604cb127d610d160d7
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
>>> Performing Cluster Check (using node 192.168.168.143:6379)
M: 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379
slots: (0 slots) slave
replicates 00067aa1ac68cb803cf903223f663a8b5832fd51
S: 5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379
slots: (0 slots) slave
replicates fe8889c921fb602fa080a0604cb127d610d160d7
M: 00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379
slots: (0 slots) slave
replicates 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看结果
[root@centos-01 ~]# docker exec -it redis /bin/bash
root@centos-01:/data# redis-cli
127.0.0.1:6379> CLUSTER nodes
00067aa1ac68cb803cf903223f663a8b5832fd51 192.168.168.145:6379@16379 master - 0 1625646473525 3 connected 10923-16383
4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 192.168.168.143:6379@16379 master - 0 1625646471506 1 connected 0-5460
8438ede3355fda20d2e944cc64d7a70619425e38 192.168.168.146:6379@16379 slave 00067aa1ac68cb803cf903223f663a8b5832fd51 0 1625646471000 3 connected
5d2ba31f6b0445037e2819939d1c262f535bd3ff 192.168.168.148:6379@16379 slave fe8889c921fb602fa080a0604cb127d610d160d7 0 1625646472517 2 connected
fe8889c921fb602fa080a0604cb127d610d160d7 192.168.168.144:6379@16379 myself,master - 0 1625646470000 2 connected 5461-10922
1ad0517208ac2eb1b9aeceb48ed942d0f36e174c 192.168.168.147:6379@16379 slave 4c10ce08fcf23e58c1397a3b515232bae6e8ce0f 0 1625646472000 1 connected
127.0.0.1:6379> CLUSTER info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:166
cluster_stats_messages_pong_sent:189
cluster_stats_messages_meet_sent:1