• Members 72194帖子
    Oct. 12, 2021, 6:44 a.m.

    Cassandra 多DC:集群的模式异地容灾

    Cassandra具有对多数据中心集群的内置支持,这使其在操作单个DC时一样透明。

    通常采用多DC的原因有:

    灾难恢复/增加弹性
    与客户进行地域共处以降低延迟
    添加高延迟数据中心以进行搜索或分析
    

    Cassandra 数据中心(DC)和机架(RACK)

    Docker 部署cassandra 集群

    version: '2.4' # 2.4 is the last version that supports depends_on conditions for service health
    networks:
      cassandra:  # docker network where all cassandra nodes will be put in
    services:
      cass1:
        image: cassandra:3.11.8   # better to use a specific version, if you want to control upgrades 
        container_name: cass1
        hostname: cass1
        mem_limit: 2g  # It's not strictly required, but it's better to have some memory limit
        healthcheck:
            test: ["CMD", "cqlsh", "-e", "describe keyspaces" ]
            interval: 5s
            timeout: 5s
            retries: 60
        networks:
          - cassandra
        ports:
          - "9042:9042"  # Expose native binary CQL port for your apps
        volumes:
          - ./data/cass1:/var/lib/cassandra    # This is the volume that will persist data for cass1 node
          - ./etc/cass1:/etc/cassandra   # Use your own config files for full control
        environment: &environment    # Declare and save environments variables into "environment"
            CASSANDRA_SEEDS: "cass1,cass2"    # The first two nodes will be seeds
            CASSANDRA_CLUSTER_NAME: SolarSystem
            CASSANDRA_DC: Mars  **#指定数据中心    必须在 thingsboard 里指定对应的  CASSANDRA_LOCAL_DATACENTER 否则 默认值 无法匹配**
            CASSANDRA_RACK: West #指定机架
            CASSANDRA_ENDPOINT_SNITCH: GossipingPropertyFileSnitch
            CASSANDRA_NUM_TOKENS: 128
    
      cass2:
        image: cassandra:3.11.8
        container_name: cass2
        hostname: cass2
        mem_limit: 2g
        healthcheck:
            test: ["CMD", "cqlsh", "-e", "describe keyspaces" ]
            interval: 5s
            timeout: 5s
            retries: 60
        networks:
          - cassandra
        ports:
          - "9043:9042"  # Expose native binary CQL port for your apps
        volumes:
          - ./data/cass2:/var/lib/cassandra    # This is the volume that will persist data for cass2 node
          - ./etc/cass2:/etc/cassandra    # Use your own config files for full control
        environment: *environment    # point to "environment" to use the same environment variables as cass1
        depends_on:
          cass1:    # start cass2 only after cass1 is healthy
            condition: service_healthy
    
      cass3:
        image: cassandra:3.11.8
        container_name: cass3
        hostname: cass3
        mem_limit: 2g
        healthcheck:
            test: ["CMD", "cqlsh", "-e", "describe keyspaces" ]
            interval: 5s
            timeout: 5s
            retries: 60
        networks:
          - cassandra
        ports:
          - "9044:9042"  # Expose native binary CQL port for your apps
        volumes:
          - ./data/cass3:/var/lib/cassandra    # This is the volume that will persist data for cass3 node
          - ./etc/cass3:/etc/cassandra    # Use your own config files for full control
        environment: *environment    # point to "environment" to use the same environment variables as cass1
        depends_on:
          cass2:    # start cass3 only after cass1 is healthy
            condition: service_healthy
    
        CASSANDRA_DC: Mars # 必须在 thingsboard 里指定对应的  CASSANDRA_LOCAL_DATACENTER 否则 默认值 无法匹配
        CASSANDRA_RACK: West
    

    Thingsboard conf

    # You can optionally add the following parameters to reconfigure your ThingsBoard instance to connect to external Cassandra nodes:
    export CASSANDRA_CLUSTER_NAME=cassandra-cluster
    export CASSANDRA_KEYSPACE_NAME=thingsboard
    export CASSANDRA_URL=127.0.0.1:9042
    export CASSANDRA_USE_CREDENTIALS=false 
    export CASSANDRA_USERNAME=
    export CASSANDRA_PASSWORD=
    export CASSANDRA_LOCAL_DATACENTER=Mars  # 默认无此项配置, 需手动指定👈
    

    参考:
    Containerized Cassandra Cluster
    NoNodeAvailableException: No node was available to execute the query