簡體   English   中英

Terraform:具有指定可用區的 ElastiCache Redis 集群?

[英]Terraform: ElastiCache Redis cluster with specified availability zones?

我使用這個 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。

在我看來,這對於當前的 Terraform aws 提供商( https://github.com/terraform-providers/terraform-provider-aws/issues/5104 )來說是不可能的

但是,我發現了一個有用的解決方法(它不允許像 AWS 控制台那樣為每個特定節點設置任意可用區,但它涵蓋了最常見的用例):如果您通過subnet_group_name鍵為復制組指定 VPC 子網,緩存實例將在這些子網的 AZ 中創建(子網組中子網的順序重要)。

示例 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 中。 我沒有用每個節點組一個以上的副本對其進行測試。

這里提到的問題添加描述。

發生這種availability_zones的原因是availability_zones參數與具有超過 1 個分片的 Redis Cluster Mode Enabled 復制組不兼容。

在 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"`

因此,要為 Redis Cluster Mode Disabled 或單分片復制組為同一可用區多次顯式配置availability_zones ,確實需要遷移該屬性,類似於對aws_elasticache_cluster資源的preferred_availability_zones遷移方式。

對於 Redis Cluster Mode Enabled 復制組(例如在 Terraform 中使用 cluster_mode 時),我們目前無法通過NodeGroupConfiguration參數設置可用區,這可能需要更改cluster_mode參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM