【问题标题】:Terraform: ElastiCache Redis cluster with specified availability zones?Terraform:具有指定可用区的 ElastiCache Redis 集群?
【发布时间】:2020-03-30 08:24:34
【问题描述】:

我使用这个 Terraform 示例来创建一个 ElastiCache Redis 集群(启用集群模式): https://www.terraform.io/docs/providers/aws/r/elasticache_replication_group.html#redis-cluster-mode-enabled

resource "aws_elasticache_replication_group" "example" {
  replication_group_id = "example-group"
  engine_version = "5.0.5"
  node_type = "cache.r5.large"
  port = 6379
  automatic_failover_enabled = true

  cluster_mode {
    replicas_per_node_group = 1
    num_node_groups = 6
  }
}

但是如何为集群和副本指定可用性节点?可以通过 AWS 控制台进行。我希望添加 availability_zones = ["us-east-1a", "us-east-1c"] 以指定所有主节点必须在 us-east-1a 中,所有副本都在 us-east-1c 中,但是得到了 Error creating Elasticache Replication Group: InvalidParameterCombination: PreferredCacheClusterAZs can only be specified for one node group.

我使用 Terraform v0.12.17 和 aws provider v2.34.0。

【问题讨论】:

    标签: amazon-web-services terraform amazon-elasticache


    【解决方案1】:

    在我看来,这对于当前的 Terraform aws 提供者来说是不可能的 (https://github.com/terraform-providers/terraform-provider-aws/issues/5104)

    但是,我发现了一个有用的解决方法(它不允许像 AWS 控制台那样为每个特定节点设置任意 AZ,但它涵盖了最常见的用例): 如果您通过subnet_group_name 键为您的复制组指定 VPC 子网,则将在这些子网的可用区中创建缓存实例(子网组中子网的顺序事项) .

    Terraform 配置示例:

    resource "aws_elasticache_subnet_group" "redis_subnet_group" {
      name       = "example-subnet-group"
      subnet_ids = ["subnet-123", "subnet-456"]
    }        
    
    resource "aws_elasticache_replication_group" "redis_replication_group" {
      replication_group_id          = "example-replication-group"
      engine_version                = "5.0.5"
      node_type                     = "cache.r5.large"
      port                          = 6379
      automatic_failover_enabled    = true
      subnet_group_name             = aws_elasticache_subnet_group.redis_subnet_group.name
    
      cluster_mode {
        replicas_per_node_group = 1
        num_node_groups         = 6
      }
    }
    

    结果:我得到了一个 6 分片集群,其中所有主节点位于子网 123 的 AZ 中,所有副本位于子网 456 的 AZ 中。我没有用每个节点组超过一个副本对其进行测试。

    【讨论】:

      【解决方案2】:

      在here提到的问题中添加描述。

      发生这种情况的原因是因为availability_zones 参数与 Redis Cluster Mode Enabled 复制不兼容 超过 1 个分片的组。

      在 Elasticache SDK 中,这是 availability_zones 设置的参数:

      // A list of EC2 Availability Zones in which the replication group's clusters
      // are created. The order of the Availability Zones in the list is the order
      // in which clusters are allocated. The primary cluster is created in the first
      // AZ in the list.
      //
      // This parameter is not used if there is more than one node group (shard).
      // You should use NodeGroupConfiguration instead.
      //
      // If you are creating your replication group in an Amazon VPC (recommended),
      // you can only locate clusters in Availability Zones associated with the subnets
      // in the selected subnet group.
      //
      // The number of Availability Zones listed must equal the value of NumCacheClusters.
      //
      // Default: system chosen Availability Zones.
      PreferredCacheClusterAZs []*string `locationNameList:"AvailabilityZone" type:"list"`
      

      所以要显式配置availability_zones Redis Cluster Mode Disabled 或多次可用区 单个分片复制组,属性确实需要 迁移类似于 preferred_availability_zones 用于 aws_elasticache_cluster 资源。

      对于启用 Redis 集群模式的复制组(例如,当使用 在 Terraform 中的 cluster_mode),我们目前没有能力 通过NodeGroupConfiguration 参数设置可用区, 这可能需要更改 cluster_mode 参数。

      【讨论】:

        猜你喜欢
        • 2019-05-04
        • 2019-07-13
        • 1970-01-01
        • 1970-01-01
        • 2020-12-09
        • 2018-03-21
        • 2018-05-03
        • 2021-02-05
        • 2022-10-24
        相关资源
        最近更新 更多