2014/10/24 の機能追加により、Redis はマルチAZ構成でノードを配置し、自動フェイルオーバーに対応した。
Multi-AZ Support / Auto Failover for Amazon ElastiCache for Redis
http://aws.amazon.com/blogs/aws/elasticache-redis-multi-az/
自分が知るかぎり、この機能が追加される前は異なる AZ に自前でクラスター配置しないとマルチAZにならなかったけど、機能追加後はマネージメントコンソールからボタンポチでマルチAZできるようになった。
このマネージメントコンソールの機能追加はコマンドライン aws cli (というかAPI)にも反映されているので、高可用性な Redis クラスターをサクッと構築してみる。
自前でゴリゴリやる手順は過去に書いた。
AWS CLIを使ってマルチAZなElastiCache Redisのリードレプリカを作成する
https://siguniang.wordpress.com/2014/09/27/create-elasticache-redis-multi-az-read-replica/
作業の流れ
- VPC & サブネットの作成
- ElastiCache向けサブネットの作成
- レプリケーショングループの構築
サブネットの作成
VPC に ElastiCache 用の異なる AZ のサブネットを作成。
これらサブネットを Cache Sbunet Group としてまとめる。
まとめたサブネットグループが foosubnet
$ aws elasticache describe-cache-subnet-groups --cache-subnet-group-name foosubnet { "CacheSubnetGroups": [ { "VpcId": "vpc-zxcv1234", "CacheSubnetGroupDescription": "Redis Subnet Group", "Subnets": [ { "SubnetIdentifier": "subnet-asdf", "SubnetAvailabilityZone": { "Name": "ap-northeast-1c" } }, { "SubnetIdentifier": "subnet-qwer", "SubnetAvailabilityZone": { "Name": "ap-northeast-1b" } } ], "CacheSubnetGroupName": "foosubnet" } ] }
2 つあるサブネットの SubnetAvailabilityZone
が 1b
と 1c
に分かれている。
マルチAZかつ自動フェイルオーバーに対応したクラスター群を作成
次に本題。3台構成のクラスターを構築する。
$ aws elasticache create-replication-group
コマンドを叩くだけでクラスターの出来上がり。
事前にプライマリー/スレーヴとなるべきクラスターを作成しなくても、自動フェイルオーバーオプションを有効にしてクラスター台数を指定すると、指定したサブネット内で master-slave なクラスターを組んでくれる。
$ aws elasticache create-replication-group \ --replication-group-id foo \ --replication-group-description 'desc of foo' \ --automatic-failover-enabled \ --num-cache-clusters 3 \ --cache-node-type cache.m3.medium \ --engine redis \ --engine-version 2.8.6 \ --cache-parameter-group-name default.redis2.8 \ --cache-subnet-group-name foosubnet \ --security-group-ids sg-01234567 \ --port 6379 { "ReplicationGroup": { "Status": "creating", "Description": "desc of foo", "ReplicationGroupId": "foo", "AutomaticFailover": "enabling", "MemberClusters": [ "foo-001", "foo-002", "foo-003" ], "PendingModifiedValues": {} } }
数分待つと、レプリケーショングループの構築が完了する。
# http://docs.aws.amazon.com/cli/latest/reference/elasticache/describe-replication-groups.html $ aws elasticache describe-replication-groups --replication-group-id foo { "ReplicationGroups": [ { "Status": "available", "Description": "desc of foo", "NodeGroups": [ { "Status": "available", "NodeGroupMembers": [ { "CurrentRole": "primary", "PreferredAvailabilityZone": "ap-northeast-1b", "CacheNodeId": "0001", "ReadEndpoint": { "Port": 6379, "Address": "foo-001.abcdef.0001.apne1.cache.amazonaws.com" }, "CacheClusterId": "foo-001" }, { "CurrentRole": "replica", "PreferredAvailabilityZone": "ap-northeast-1c", "CacheNodeId": "0001", "ReadEndpoint": { "Port": 6379, "Address": "foo-002.abcdef.0001.apne1.cache.amazonaws.com" }, "CacheClusterId": "foo-002" }, { "CurrentRole": "replica", "PreferredAvailabilityZone": "ap-northeast-1c", "CacheNodeId": "0001", "ReadEndpoint": { "Port": 6379, "Address": "foo-003.abcdef.0001.apne1.cache.amazonaws.com" }, "CacheClusterId": "foo-003" } ], "NodeGroupId": "0001", "PrimaryEndpoint": { "Port": 6379, "Address": "foo.abcdef.ng.0001.apne1.cache.amazonaws.com" } } ], "ReplicationGroupId": "foo", "AutomaticFailover": "enabled", "MemberClusters": [ "foo-001", "foo-002", "foo-003" ], "PendingModifiedValues": {} } ] }
AZ もほどよく分かれている。
primary endpoint に接続
primary endpoint の DNS を確認すると CNAME で foo-001 のクラスターを向いていることがわかる。
$ dig +noall +answer foo.abcdef.ng.0001.apne1.cache.amazonaws.com foo.abcdef.ng.0001.apne1.cache.amazonaws.com. 46 IN CNAME foo-001.abcdef.0001.apne1.cache.amazonaws.com. foo-001.abcdef.0001.apne1.cache.amazonaws.com. 60 IN A 10.0.10.202
primary endpoint は当然レプリケーションのマスター
$ telnet foo.abcdef.ng.0001.apne1.cache.amazonaws.com 6379 Trying 10.0.10.202... Connected to foo.abcdef.ng.0001.apne1.cache.amazonaws.com. Escape character is '^]'. info replication $336 # Replication role:master connected_slaves:2 slave0:ip=172.16.1.170,port=6379,state=online,offset=69509135,lag=1 slave1:ip=172.16.1.215,port=6379,state=online,offset=69509223,lag=0 master_repl_offset:69509223 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:68460648 repl_backlog_histlen:1048576
更新処理を確認
set foo bar +OK get foo $3 bar
マスターなので、当然更新できる。
slave endpoint に接続
スレーヴクラスターのエンドポイントに接続してみる。
$ telnet foo-002.abcdef.0001.apne1.cache.amazonaws.com 6379 Trying 10.0.12.67... Connected to foo-002.abcdef.0001.apne1.cache.amazonaws.com. Escape character is '^]'. info replication $427 # Replication role:slave master_host:foo.abcdef.0001.internal.apne1.cache.amazonaws.com master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:69652853 repl_sync_enabled:1 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
role:slave
master_host:foo.abcdef.0001.internal.apne1.cache.amazonaws.com
のあたりが特に重要。
Redis の更新を試みる
get foo $3 baz set foo baz -READONLY You can't write against a read only slave.
スレーヴなので、更新しようとすると怒られる。
レプリケーショングループ作成時のオプションについて
肝となるオプションは次の2つ
--automatic-failover-enabled
オプション
自動フェイルオーバーを有効にする。
意図的に無効化したいときは --no-automatic-failover-enabled
とする。
--num-cache-clusters
オプション
クラスターの数。
自動フェイルオーバーを有効にする場合の最小値は 2
注意点
aws cli のマニュアルには “When you create a replication group, you must specify an existing cache cluster that is in the primary role.” や “This cache cluster must already exist and have a status of available.” というように、さも--primary-cluster-id
オプションが必須であるかのような記述がある。
ただ、同時に “This parameter is not required if NumCacheClusters is specified.” とも書かれている。
じゃあ --primary-cluster-id
オプションを省くとどうなるかというと、答えは Amazon ElastiCache の User Guide にある。
If you don’t have an active Redis cache cluster to use as the primary cluster, you can create the replication group and its associated cache clusters with a single command. The following example creates the Multi-AZ enabled replication group my-rg with 3 cache clusters; the primary cluster my-rg001 and two replicas, my-rg002 and my-rg003.
https://elasticache.us-east-1.amazonaws.com/ ?Action=CreateReplicationGroup &AutomaticFailover=true &CacheNodeType=cache.m3.large &Engine=redis &NumCacheNodes=3 &ReplicationGroupId=my-rg &Version=2014-09-30 &SignatureVersion=4 &SignatureMethod=HmacSHA256 &Timestamp=20140401T192317Z &X-Amz-Credential=<credential>
When you create the clusters and replication group as shown in this example, the names of the clusters are created by adding a three digit sequential number starting at 001 to the end of the replication group’s names.
http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/AutoFailover.Enable.html
この裏?仕様を使うと、マネージメントコンソールと同程度にお手軽に、 Multi-AZサポート/自動フェイルオーバーな Redis クラスターを API から作れる。
Leave a Reply