使用docker搭建redis-cluster集群
目录
1.拉取redis镜像
2.创建network
3.创建redis配置文件
4.创建redis容器
5.创建Redis Cluster集群
6.测试
1.拉取redis镜像
docker pull redis
2.创建network
docker容器创建的应用会默认使用 bridge,但是每次重启docker此网络的ip会动态变更,因此需要我们自己手动创建一个固定的network
#创建虚拟网卡 docker network create myredis #查看虚拟网卡 docker network ls
其它虚拟网卡相关命令
#查看虚拟网卡详细信息 docker network inspect myredis #删除虚拟网卡 docker network rm myredis
3.创建redis配置文件
需要创建多个redis.conf配置文件,采用shell脚本的方式创建
createConf.sh 脚本内容如下所示:
#!/bin/sh for port in $(seq 6380 6385); do mkdir -p /home/ap/chenby/redisCluster/node-${port}/conf touch /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf cat << EOF > /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf #节点端口 port ${port} #添加访问认证 requirepass 1234 #如果主节点开启了访问认证,从节点访问主节点需要认证 masterauth 1234 #保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问 protected-mode no #bind 0.0.0.0 #是否以守护线程的方式启动(后台启动),默认 no daemonize no #是否开启 AOF 持久化模式,默认 no appendonly yes #是否开启集群模式,默认 no cluster-enabled yes #集群节点信息文件 cluster-config-file nodes.conf #群节点连接超时时间 cluster-node-timeout 5000 #集群节点 IP cluster-announce-ip 192.168.137.103 #集群节点映射端口 cluster-announce-port ${port} #集群节点总线端口 cluster-announce-bus-port 1${port} EOF done
执行脚本,运行结果如下:
4.创建redis容器
因为要创建多个容器,使用shell脚本的方式创建
redis.sh脚本内容:
#!/bin/sh ## 首次创建并运行redis容器 if [ "$1" = "create" ]; then for port in $(seq 6380 6385); do docker run -di --restart always --name redis-${port} --net myredis -p ${port}:${port} -p 1${port}:1${port} -v /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /home/ap/chenby/redisCluster/node-${port}/data:/data redis redis-server /usr/local/etc/redis/redis.conf done fi ##停止redis容器 if [ "$1" = "stop" ]; then for port in $(seq 6380 6385); do docker stop redis-${port} done fi ##启动已有的redis容器 if [ "$1" = "start" ]; then for port in $(seq 6380 6385); do docker start redis-${port} done fi ## 删除redis容器 if [ "$1" = "rm" ]; then for port in $(seq 6380 6385); do docker rm redis-${port} done fi
执行命令 : sh redis.sh create
结果如下图所示,证明创建成功
执行命令 docker ps -a 查看容器,如下图所示,证明创建并全部启动成功
5.创建Redis Cluster集群
5.1 任意选择一个redis节点进入,创建redis集群
进入redis-6380容器:
docker exec -it redis-6380 /bin/bash
5.2 创建集群
redis-cli -a 1234 --cluster create 192.168.137.103:6380 192.168.137.103:6381 192.168.137.103:6382 192.168.137.103:6383 192.168.137.103:6384 192.168.137.103:6385 --cluster-replicas 1
根据出现的信息,输入yes
创建成功如下:
root@c81e4c88ef59:/data# redis-cli -a 1234 --cluster create 192.168.137.103:6380 192.168.137.103:6381 192.168.137.103:6382 192.168.137.103:6383 192.168.137.103:6384 192.168.137.103:6385 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> 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.137.103:6384 to 192.168.137.103:6380 Adding replica 192.168.137.103:6385 to 192.168.137.103:6381 Adding replica 192.168.137.103:6383 to 192.168.137.103:6382 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380 slots:[0-5460] (5461 slots) master M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381 slots:[5461-10922] (5462 slots) master M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382 slots:[10923-16383] (5461 slots) master S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383 replicates 5de830891133b5621d06901d970ef84c026a6bd1 S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384 replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0 S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385 replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 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.137.103:6380) M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384 slots: (0 slots) slave replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0 S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385 slots: (0 slots) slave replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383 slots: (0 slots) slave replicates 5de830891133b5621d06901d970ef84c026a6bd1 M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382 slots:[10923-16383] (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
5.3 查看节点相关信息
## 检查集群状态 redis-cli -a 1234 --cluster check 192.168.137.103:6382 ## 连接集群某个节点 redis-cli -c -a 1234 -h 192.168.137.103 -p 6382 ## 查看集群信息 cluster info ## 查看集群结点信息 cluster nodes
如下所示:
root@c81e4c88ef59:/data# redis-cli -a 1234 --cluster check 192.168.137.103:6382 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.137.103:6382 (30c40c7e...) -> 0 keys | 5461 slots | 1 slaves. 192.168.137.103:6380 (5de83089...) -> 0 keys | 5461 slots | 1 slaves. 192.168.137.103:6381 (39e4e1aa...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 192.168.137.103:6382) M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385 slots: (0 slots) slave replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384 slots: (0 slots) slave replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0 M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383 slots: (0 slots) slave replicates 5de830891133b5621d06901d970ef84c026a6bd1 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6382 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.137.103:6382> 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:3 cluster_stats_messages_ping_sent:1150 cluster_stats_messages_pong_sent:1149 cluster_stats_messages_meet_sent:1 cluster_stats_messages_sent:2300 cluster_stats_messages_ping_received:1149 cluster_stats_messages_pong_received:1151 cluster_stats_messages_received:2300 192.168.137.103:6382> cluster nodes 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382@16382 myself,master - 0 1668000952000 3 connected 10923-16383 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380@16380 master - 0 1668000953660 1 connected 0-5460 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385@16385 slave 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 0 1668000953559 3 connected 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384@16384 slave 39e4e1aa64ab168f0d875c0f187343382d7aadc0 0 1668000954662 2 connected 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381@16381 master - 0 1668000954000 2 connected 5461-10922 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383@16383 slave 5de830891133b5621d06901d970ef84c026a6bd1 0 1668000954000 1 connected 192.168.137.103:6382> root@c81e4c88ef59:/data#
6.测试
6.1 进入6382节点,并使用set/get读写数据
root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6382 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.137.103:6382> set name helloWord -> Redirected to slot [5798] located at 192.168.137.103:6381 OK 192.168.137.103:6381> set test 123456 OK 192.168.137.103:6381> get test "123456" 192.168.137.103:6381> get name "helloWord"
6.2 然后再进入6385节点,读取刚才写入的数据文章来源:https://www.toymoban.com/news/detail-433286.html
root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6385 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.137.103:6385> get test -> Redirected to slot [6918] located at 192.168.137.103:6381 "123456" 192.168.137.103:6381> get name "helloWord" 192.168.137.103:6381>
读到数据,证明Redis Cluster集群创建成功文章来源地址https://www.toymoban.com/news/detail-433286.html
到了这里,关于使用docker搭建 redis cluster 集群的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!