docker 部署redis集群
时间: 2023-12-06 12:38:47 浏览: 134
以下是使用Docker部署Redis集群的步骤:
1.拉取Redis官方镜像:
```shell
docker pull redis
```
2.创建一个自定义的网络:
```shell
docker network create redis-net
```
3.创建6个Redis容器,3个作为主节点,3个作为从节点:
```shell
docker run -d --name redis-1 --net redis-net redis redis-server --appendonly yes
docker run -d --name redis-2 --net redis-net redis redis-server --appendonly yes
docker run -d --name redis-3 --net redis-net redis redis-server --appendonly yes
docker run -d --name redis-4 --net redis-net redis redis-server --appendonly yes
docker run -d --name redis-5 --net redis-net redis redis-server --appendonly yes
docker run -d --name redis-6 --net redis-net redis redis-server --appendonly yes
```
4.将容器分配到不同的主节点和从节点:
```shell
docker exec -it redis-1 redis-cli cluster meet <ip-address-of-redis-2> 6379
docker exec -it redis-1 redis-cli cluster meet <ip-address-of-redis-3> 6379
docker exec -it redis-4 redis-cli cluster meet <ip-address-of-redis-5> 6379
docker exec -it redis-4 redis-cli cluster meet <ip-address-of-redis-6> 6379
docker exec -it redis-1 redis-cli cluster replicate <node-id-of-redis-2>
docker exec -it redis-1 redis-cli cluster replicate <node-id-of-redis-3>
docker exec -it redis-4 redis-cli cluster replicate <node-id-of-redis-5>
docker exec -it redis-4 redis-cli cluster replicate <node-id-of-redis-6>
```
5.检查集群状态:
```shell
docker exec -it redis-1 redis-cli cluster info
```
6.在集群中添加键值对:
```shell
docker exec -it redis-1 redis-cli set mykey "Hello World"
docker exec -it redis-4 redis-cli get mykey
```
阅读全文