在CentOS上配置Redis集群,你需要遵循以下步骤:
确保所有节点上都安装了相同版本的Redis。
sudo yum install redis
在每个节点上编辑Redis配置文件(通常是/etc/redis.conf
),进行以下修改:
cluster-enabled yes
以启用集群模式。cluster-config-file nodes.conf
以指定集群配置文件。cluster-node-timeout 5000
以设置节点超时时间(毫秒)。appendonly yes
以启用AOF持久化(可选但推荐)。例如:
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
在每个节点上启动Redis服务:
sudo systemctl start redis
sudo systemctl enable redis
使用redis-cli
工具创建集群。假设你有6个节点,每个节点运行一个Redis实例,你可以这样创建集群:
redis-cli --cluster create \
192.168.1.1:6379 \
192.168.1.2:6379 \
192.168.1.3:6379 \
192.168.1.4:6379 \
192.168.1.5:6379 \
192.168.1.6:6379 \
--cluster-replicas 1
--cluster create
:指定要创建的集群节点。--cluster-replicas 1
:每个主节点有一个从节点。使用redis-cli
连接到集群并检查集群状态:
redis-cli -c -p 6379
192.168.1.1:6379> cluster info
192.168.1.1:6379> cluster nodes
你可以测试集群的基本功能,例如设置和获取键值对:
192.168.1.1:6379> set key1 value1
OK
192.168.1.1:6379> get key1
"value1"
通过以上步骤,你应该能够在CentOS上成功配置一个Redis集群。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:centos redis如何实现自动扩容