在 AWS 上使用 ElastiCache 和 ElasticBeanstalk 配置 Redis
Posted
技术标签:
【中文标题】在 AWS 上使用 ElastiCache 和 ElasticBeanstalk 配置 Redis【英文标题】:Configuring Redis with ElastiCache and ElasticBeanstalk on AWS 【发布时间】:2015-10-18 06:03:12 【问题描述】:我有一个在 Elastic Beanstalk 上运行的 Tomcat + Redis 应用程序。在我的代码中,我使用
创建了一个 Redis 客户端pool = new JedisPool(new JedisPoolConfig(), "localhost");
当然,在生产中,我不能使用“localhost”,我已经阅读了Customizing the Beanstalk Environment 以及关于让自动发现与您的代码一起工作的内容,但这似乎只适用于 Memcached。
是否有人获得了配置了 Beanstalk 的 Redis ElastiCache 集群。您从哪里获得要使用的 DNS 地址而不是“localhost”?
【问题讨论】:
【参考方案1】:恐怕您无法轻松获取 AWS ElastiCache 端点(例如:环境变量)。
从Deploying an Express Application with Clustering to Elastic Beanstalk 上的 AWS 文档中,我们可以遵循 NodeJS + ElastiCache Memcache 解决方案。在您的根项目中创建以下两个.ebextensions
(阅读更多here)脚本:
.ebextenstions/elasticache-iam-with-script.config
##
# This sample adds an ElastiCache cluster to the environment.
# It creates an IAM user with the permisisons required to discover the elasticache nodes.
# It provides a cfn-hup responder if the cache changes to rewrite the file
# It writes a file out to: /var/www/html/nodelist
# containing the cache nodes on startup and when the cache changes (through a cfn/eb update)
#
# Customers would generally not edit this file.
# Instead, they would have another file sitting in the same directory (or anywhere) with
# an option-settings section such as the following (all of these are showing the default value)
#
# option-settings:
# "aws:elasticbeanstalk:customoption" :
# CacheNodeType : cache.m1.small
# NumCacheNodes : 1
# Engine : memcached
# NodeListPath : /var/www/html/nodelist
#
# Issues:
# Requires ElastiCache CLI latest URL to point to version 1.7
##
Resources:
MyElastiCache:
Type: AWS::ElastiCache::CacheCluster
Properties:
CacheNodeType:
Fn::GetOptionSetting:
OptionName : CacheNodeType
DefaultValue: cache.m1.small
NumCacheNodes:
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
Engine:
Fn::GetOptionSetting:
OptionName : Engine
DefaultValue: memcached
CacheSecurityGroupNames:
- Ref: MyCacheSecurityGroup
MyCacheSecurityGroup:
Type: AWS::ElastiCache::SecurityGroup
Properties:
Description: "Lock cache down to webserver access only"
MyCacheSecurityGroupIngress:
Type: AWS::ElastiCache::SecurityGroupIngress
Properties:
CacheSecurityGroupName:
Ref: MyCacheSecurityGroup
EC2SecurityGroupName:
Ref: AWSEBSecurityGroup
AWSEBAutoScalingGroup :
Metadata :
ElastiCacheConfig :
CacheName :
Ref : MyElastiCache
CacheSize :
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
WebServerUser :
Type : AWS::IAM::User
Properties :
Path : "/"
Policies:
-
PolicyName: root
PolicyDocument :
Statement :
-
Effect : Allow
Action :
- cloudformation:DescribeStackResource
- cloudformation:ListStackResources
- elasticache:DescribeCacheClusters
Resource : "*"
WebServerKeys :
Type : AWS::IAM::AccessKey
Properties :
UserName :
Ref: WebServerUser
Outputs:
WebsiteURL:
Description: sample output only here to show inline string function parsing
Value: |
http://` "Fn::GetAtt" : [ "AWSEBLoadBalancer", "DNSName" ] `
MyElastiCacheName:
Description: Name of the elasticache
Value:
Ref : MyElastiCache
NumCacheNodes:
Description: Number of cache nodes in MyElastiCache
Value:
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
files:
"/etc/cfn/cfn-credentials" :
content : |
AWSAccessKeyId=` "Ref" : "WebServerKeys" `
AWSSecretKey=` "Fn::GetAtt" : ["WebServerKeys", "SecretAccessKey"] `
mode : "000400"
owner : root
group : root
"/etc/cfn/get-cache-nodes" :
content : |
# Define environment variables for command line tools
export AWS_ELASTICACHE_HOME="/home/ec2-user/elasticache/$(ls /home/ec2-user/elasticache/)"
export AWS_CLOUDFORMATION_HOME=/opt/aws/apitools/cfn
export PATH=$AWS_CLOUDFORMATION_HOME/bin:$AWS_ELASTICACHE_HOME/bin:$PATH
export AWS_CREDENTIAL_FILE=/etc/cfn/cfn-credentials
export JAVA_HOME=/usr/lib/jvm/jre
# Grab the Cache node names and configure the php page
cfn-list-stack-resources ` "Ref" : "AWS::StackName" ` --region ` "Ref" : "AWS::Region" ` | grep MyElastiCache | awk 'print $3' | xargs -I elasticache-describe-cache-clusters --region ` "Ref" : "AWS::Region" ` --show-cache-node-info | grep CACHENODE | awk 'print $4 ":" $5' > ` "Fn::GetOptionSetting" : "OptionName" : "NodeListPath", "DefaultValue" : "/var/www/html/nodelist" `
mode : "000500"
owner : root
group : root
"/etc/cfn/hooks.d/cfn-cache-change.conf" :
"content": |
[cfn-cache-size-change]
triggers=post.update
path=Resources.AWSEBAutoScalingGroup.Metadata.ElastiCacheConfig
action=/etc/cfn/get-cache-nodes
runas=root
sources :
"/home/ec2-user/elasticache" : "https://s3.amazonaws.com/elasticache-downloads/AmazonElastiCacheCli-latest.zip"
commands:
make-elasticache-executable:
command: chmod -R ugo+x /home/ec2-user/elasticache/*/bin/*
packages :
"yum" :
"aws-apitools-cfn" : []
container_commands:
initial_cache_nodes:
command: /etc/cfn/get-cache-nodes
.ebextensions/elasticache-settings.config
option_settings:
"aws:elasticbeanstalk:customoption" :
CacheNodeType : cache.t2.micro
NumCacheNodes : 1
Engine : redis
NodeListPath : /var/nodelist
第一个脚本 (elasticache-iam-with-script.config) 用于创建附加到 Elastic Beanstalk 环境的 ElastiCache 资源。
第二个脚本 (elasticache-settings.config) 是第一个脚本的自定义配置文件。您可以根据需要更改配置。
第一个脚本将在 /var/nodelist 中生成一个文件。文件内容如下:
aws-my-xxxxxxxxxxxxx.xxxxxx.0001.use1.cache.amazonaws.com:us-east-1b
这是一个端点和 AZ 对,用冒号 (<endpoint>:<AZ>
) 分隔。您的 Java 可能会解析 /var/nodelist 并将endpoint
放入JedisPool
。
【讨论】:
是的,很好,我也遇到了这个解决方案。最终,我只是在代码中硬编码端点 DNS 名称,然后让我的 redis 客户端处理分片。但这将是一种动态获取名称的方法。以上是关于在 AWS 上使用 ElastiCache 和 ElasticBeanstalk 配置 Redis的主要内容,如果未能解决你的问题,请参考以下文章
如何知道我是不是需要使用 AWS Elasticache 和 AWS Elastic Load Balancing?
在本地测试 Elasticache 和无服务器 AWS Lambda
使用 Predis 使用 Laravel-5.4.32 配置 AWS Elasticache redis Cluster-3.2.4