Home Redis Cluster 구축 (Mac, Rocky Linux)
Post
Cancel

Redis Cluster 구축 (Mac, Rocky Linux)

공식문서

레디스 공식문서 - 클러스터링

Docker Compose [MAC]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
services:
  redis-master-1:
    container_name: redis-master-1
    image: redis:7.2
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always
    ports:
      - 6379:6379
      - 6479:6479
      - 6579:6579
      - 6380:6380
      - 6480:6480
      - 6580:6580

  redis-master-2:
    network_mode: "service:redis-master-1"
    container_name: redis-master-2
    image: redis:latest
    command: ["redis-server", "--port", "6479", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always

  redis-master-3:
    network_mode: "service:redis-master-1"
    container_name: redis-master-3
    image: redis:latest
    command: ["redis-server", "--port", "6579", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always

  redis-slave-1:
    network_mode: "service:redis-master-1"
    container_name: redis-slave-1
    image: redis:latest
    command: ["redis-server", "--port", "6380", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always

  redis-slave-2:
    network_mode: "service:redis-master-1"
    container_name: redis-slave-2
    image: redis:latest
    command: ["redis-server", "--port", "6480", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always

  redis-slave-3:
    network_mode: "service:redis-master-1"
    container_name: redis-slave-3
    image: redis:latest
    command: ["redis-server", "--port", "6580", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes"]
    restart: always

  redis_cluster_entry:
    network_mode: "service:redis-master-1"
    image: redis:latest
    container_name: redis_cluster_entry
    command: redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6479 127.0.0.1:6579 127.0.0.1:6380 127.0.0.1:6480 127.0.0.1:6580 --cluster-yes
    depends_on:
      - redis-master-1
      - redis-master-2
      - redis-master-3
      - redis-slave-1
      - redis-slave-2
      - redis-slave-3

Docker Compose [Linux]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
services:
    redis-master-1:
        network_mode: "host"
        container_name: redis-master-1
        image: redis:latest
        command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-master-1-data:/data

    redis-master-2:
        network_mode: "host"
        container_name: redis-master-2
        image: redis:latest
        command: redis-server --port 6479 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-master-2-data:/data

    redis-master-3:
        network_mode: "host"
        container_name: redis-master-3
        image: redis:latest
        command: redis-server --port 6579 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-master-3-data:/data

    redis-slave-1:
        network_mode: "host"
        container_name: redis-slave-1
        image: redis:latest
        command: redis-server --port 6380 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-slave-1-data:/data

    redis-slave-2:
        network_mode: "host"
        container_name: redis-slave-2
        image: redis:latest
        command: redis-server --port 6480 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-slave-2-data:/data

    redis-slave-3:
        network_mode: "host"
        container_name: redis-slave-3
        image: redis:latest
        command: redis-server --port 6580 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
        volumes:
            - redis-slave-3-data:/data

    redis_cluster_entry:
        network_mode: "host"
        image: redis:latest
        container_name: redis_cluster_entry
        command: >
            bash -c "
              sleep 10 &&
              redis-cli --cluster create {serverIPAddress}:6379 {serverIPAddress}:6479 {serverIPAddress}:6579 {serverIPAddress}:6380 {serverIPAddress}:6480 {serverIPAddress}:6580 --cluster-replicas 1 --cluster-yes
            "
        depends_on:
            - redis-master-1
            - redis-master-2
            - redis-master-3
            - redis-slave-1
            - redis-slave-2
            - redis-slave-3

volumes:
    redis-master-1-data:
    redis-master-2-data:
    redis-master-3-data:
    redis-slave-1-data:
    redis-slave-2-data:
    redis-slave-3-data:

Spring Boot에서 클러스터 접속

Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@RequiredArgsConstructor
public class RedisConfig {

    private final RedisInfo redisInfo;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisClusterConfiguration(redisInfo.getNodes()));
    }
}

@Data
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis.cluster")
public class RedisInfo {
    private List<String> nodes;
}

yml

1
2
3
4
5
6
7
8
9
10
11
spring:
  data:
    redis:
      cluster:
        nodes:
          - localhost:6379
          - localhost:6479
          - localhost:6579
          - localhost:6380
          - localhost:6480
          - localhost:6580
This post is licensed under CC BY 4.0 by the author.