Docker 搭建redis集群
in DevOps with 0 comment

Docker 搭建redis集群

in DevOps with 0 comment

Docker 搭建redis集群

a

六个Redis实例的配置文件:
redis.conf

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
12345
/opt/redis
		/7000/redis.conf
		/7001/redis.conf
		/7002/redis.conf
		/7003/redis.conf
		/7004/redis.conf
		/7005/redis.conf
1234567
mkdir /opt/redis
cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005

cat <<EOF >7000/redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF



先启动6个redis实例

docker run -d --name redis7000 \
-v /opt/redis/7000/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7001 \
-v /opt/redis/7001/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7002 \
-v /opt/redis/7002/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7003 \
-v /opt/redis/7003/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7004 \
-v /opt/redis/7004/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

docker run -d --name redis7005 \
-v /opt/redis/7005/redis.conf:/redis.conf \
--net=host \
--restart=always \
redis \
redis-server /redis.conf

执行命令配置集群

# 进入容器执行集群配置命令

docker exec -it redis7000 \
redis-cli --cluster create \
192.168.64.150:7000 192.168.64.150:7001 \
192.168.64.150:7002 192.168.64.150:7003 \
192.168.64.150:7004 192.168.64.150:7005 \
--cluster-replicas 1

查看集群信息

docker exec -it redis7000 redis-cli -c -p 7000

cluster info

cluster nodes

存数据

docker exec -it redis7000 redis-cli -c -p 7000
# 7002
set a aaaaaaaaaaaaa
# 7000
set b bbbbbbbbbbb
# 7001
set c cccccccccccccccc

spring redis api

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        
spring:
  redis:
    cluster:
      nodes:
        - 192.168.64.150:7000
        - 192.168.64.150:7001
        - 192.168.64.150:7002
        - 192.168.64.150:7003
        - 192.168.64.150:7004
        - 192.168.64.150:7005
        
        
    @Autowired
    private RedisConnectionFactory connectionFactory;

    public void test() {
        RedisClusterConnection c = connectionFactory.getClusterConnection();
        for (int i = 0; i < 100; i++) {
            c.set(("k"+i).getBytes(StandardCharsets.UTF_8),
                    ("v"+i).getBytes(StandardCharsets.UTF_8));
        }
    }
    
    
    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate t;

    public void test() {
        ValueOperations<String, Object> o = t.opsForValue();
        o.set("hhhh", "vvvv");
        o.set("nnnn", "gggg");
    }